From a607e7730ae72aaf4193497e54ececf629d1b261 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 15 Jan 2025 19:08:30 +1100 Subject: [PATCH 1/8] Support (Multi)Point,FeatureCollection and GeometryCollection in bboxClip. Return Multi* objects with no coordinates instead of defective Polygon and LineString objects. --- .gitignore | 1 + packages/turf-bbox-clip/index.ts | 80 ++++++- packages/turf-bbox-clip/test.ts | 36 +-- .../test/in/feature-collection.geojson | 94 ++++++++ .../test/in/geometry-collection.geojson | 78 +++++++ .../test/in/linestring-outside.geojson | 32 +++ .../test/in/multipoint-inside-outside.json | 32 +++ .../test/in/point-inside.geojson | 29 +++ .../test/in/point-outside.geojson | 29 +++ .../test/in/polygon-outside.geojson | 37 +++ .../test/out/feature-collection.geojson | 217 ++++++++++++++++++ .../test/out/geometry-collection.geojson | 137 +++++++++++ .../test/out/linestring-outside.geojson | 55 +++++ .../test/out/multipoint-inside-outside.json | 55 +++++ .../test/out/point-inside.geojson | 52 +++++ .../test/out/point-outside.geojson | 52 +++++ .../test/out/polygon-outside.geojson | 60 +++++ .../out/polygon-point-intersection.geojson | 2 +- .../test/out/featurecollection.geojson | 3 - 19 files changed, 1054 insertions(+), 27 deletions(-) create mode 100644 packages/turf-bbox-clip/test/in/feature-collection.geojson create mode 100644 packages/turf-bbox-clip/test/in/geometry-collection.geojson create mode 100644 packages/turf-bbox-clip/test/in/linestring-outside.geojson create mode 100644 packages/turf-bbox-clip/test/in/multipoint-inside-outside.json create mode 100644 packages/turf-bbox-clip/test/in/point-inside.geojson create mode 100644 packages/turf-bbox-clip/test/in/point-outside.geojson create mode 100644 packages/turf-bbox-clip/test/in/polygon-outside.geojson create mode 100644 packages/turf-bbox-clip/test/out/feature-collection.geojson create mode 100644 packages/turf-bbox-clip/test/out/geometry-collection.geojson create mode 100644 packages/turf-bbox-clip/test/out/linestring-outside.geojson create mode 100644 packages/turf-bbox-clip/test/out/multipoint-inside-outside.json create mode 100644 packages/turf-bbox-clip/test/out/point-inside.geojson create mode 100644 packages/turf-bbox-clip/test/out/point-outside.geojson create mode 100644 packages/turf-bbox-clip/test/out/polygon-outside.geojson diff --git a/.gitignore b/.gitignore index 145fdda4a..f4a35b12f 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ node_modules .vscode .nx/ +.history/ \ No newline at end of file diff --git a/packages/turf-bbox-clip/index.ts b/packages/turf-bbox-clip/index.ts index 765dc3942..2095aacfe 100644 --- a/packages/turf-bbox-clip/index.ts +++ b/packages/turf-bbox-clip/index.ts @@ -6,26 +6,45 @@ import { MultiPolygon, GeoJsonProperties, Polygon, + Point, + MultiPoint, + Position, + FeatureCollection, + Geometry, + GeometryCollection, } from "geojson"; import { + featureCollection, + geometryCollection, lineString, multiLineString, + multiPoint, multiPolygon, + point, polygon, } from "@turf/helpers"; import { getGeom } from "@turf/invariant"; import { lineclip, polygonclip } from "./lib/lineclip.js"; +type OneGeometry = + | Point + | MultiPoint + | LineString + | MultiLineString + | Polygon + | MultiPolygon; + /** - * Takes a {@link Feature} and a bbox and clips the feature to the bbox using + * Takes a {@link Feature}, {@link Geometry} or {@link FeatureCollection} and a bbox and clips the object to the bbox using * [lineclip](https://github.com/mapbox/lineclip). - * May result in degenerate edges when clipping Polygons. + * If a geometry is entirely outside the bbox, a Multi-geometry with no coordinates is returned. + * LineString and Polygon geometries may also become Multi-geometry if the clipping process cuts them into several pieces. * * @function - * @param {Feature} feature feature to clip to the bbox + * @param {Feature} feature feature to clip to the bbox * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order - * @returns {Feature} clipped Feature + * @returns {Feature} clipped Feature * @example * var bbox = [0, 0, 10, 10]; * var poly = turf.polygon([[[2, 2], [8, 4], [12, 8], [3, 7], [2, 2]]]); @@ -36,12 +55,33 @@ import { lineclip, polygonclip } from "./lib/lineclip.js"; * var addToMap = [bbox, poly, clipped] */ function bboxClip< - G extends Polygon | MultiPolygon | LineString | MultiLineString, + G extends Geometry, P extends GeoJsonProperties = GeoJsonProperties, ->(feature: Feature | G, bbox: BBox) { +>( + feature: Feature | G | FeatureCollection, + bbox: BBox +): Feature | FeatureCollection { + if (feature.type === "FeatureCollection") { + return featureCollection( + feature.features.map((f: Feature) => bboxClip(f, bbox) as Feature) + ); + } const geom = getGeom(feature); const type = geom.type; const properties = feature.type === "Feature" ? feature.properties : {}; + + if (type === "GeometryCollection") { + const gs = geom.geometries as OneGeometry[]; + const outGs: OneGeometry[] = gs.map( + (g: OneGeometry) => + (bboxClip(g as OneGeometry, bbox) as Feature).geometry + ); + return geometryCollection(outGs, properties) as Feature< + GeometryCollection, + P + >; + } + let coords: any[] = geom.coordinates; switch (type) { @@ -59,8 +99,14 @@ function bboxClip< } return multiLineString(lines, properties); } - case "Polygon": - return polygon(clipPolygon(coords, bbox), properties); + case "Polygon": { + const poly = clipPolygon(coords, bbox); + if (poly.length === 0) { + return multiPolygon([], properties); + } else { + return polygon(poly, properties); + } + } case "MultiPolygon": return multiPolygon( coords.map((poly) => { @@ -68,11 +114,29 @@ function bboxClip< }), properties ); + case "Point": { + const coord = geom.coordinates; + if (checkCoord(coord, bbox)) return point(coord, properties); + return multiPoint([], properties); + } + case "MultiPoint": { + return multiPoint(coords.filter((coord) => checkCoord(coord, bbox))); + } + default: throw new Error("geometry " + type + " not supported"); } } +function checkCoord(coord: Position, bbox: BBox) { + return ( + coord[0] >= bbox[0] && + coord[1] >= bbox[1] && + coord[0] <= bbox[2] && + coord[1] <= bbox[3] + ); +} + function clipPolygon(rings: number[][][], bbox: BBox) { const outRings = []; for (const ring of rings) { diff --git a/packages/turf-bbox-clip/test.ts b/packages/turf-bbox-clip/test.ts index 9bd611648..98e0dec06 100644 --- a/packages/turf-bbox-clip/test.ts +++ b/packages/turf-bbox-clip/test.ts @@ -28,14 +28,28 @@ test("turf-bbox-clip", (t) => { const filename = fixture.filename; const name = fixture.name; const geojson = fixture.geojson; - const feature = geojson.features[0]; - const bbox = turfBBox(geojson.features[1]); + const isFeatureCollection = geojson.features.length > 2; + const feature = isFeatureCollection + ? featureCollection(geojson.features.slice(0, -1)) + : geojson.features[0]; + const clipRegion = geojson.features.slice(-1)[0]; + const bbox = turfBBox(clipRegion); const clipped = bboxClip(feature, bbox); - const results = featureCollection([ - colorize(feature, "#080"), - colorize(clipped, "#F00"), - colorize(geojson.features[1], "#00F", 3), - ]); + + let results; + if (isFeatureCollection) { + results = featureCollection([ + ...feature.features.map((f) => colorize(f, "#080")), + ...clipped.features.map((c) => colorize(c, "#F00")), + colorize(clipRegion, "#00F", 3), + ]); + } else { + results = featureCollection([ + colorize(feature, "#080"), + colorize(clipped, "#F00"), + colorize(clipRegion, "#00F", 3), + ]); + } if (process.env.REGEN) writeJsonFileSync(directories.out + filename, results); @@ -44,14 +58,6 @@ test("turf-bbox-clip", (t) => { t.end(); }); -test("turf-bbox-clip -- throws", (t) => { - t.throws( - () => bboxClip(point([5, 10]), [-180, -90, 180, 90]), - /geometry Point not supported/ - ); - t.end(); -}); - function colorize(feature, color, width) { color = color || "#F00"; width = width || 6; diff --git a/packages/turf-bbox-clip/test/in/feature-collection.geojson b/packages/turf-bbox-clip/test/in/feature-collection.geojson new file mode 100644 index 000000000..099fb80dc --- /dev/null +++ b/packages/turf-bbox-clip/test/in/feature-collection.geojson @@ -0,0 +1,94 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [92.78854443123498, -4.0631420171898185], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [88.80200582619335, -8.780228029258652], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [84.45578448719408, -0.42584674848238535], + [104.00625645806042, -17.30113187521333], + [108.37396514395846, -11.998337187512362] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [101.64270834459677, -9.59572648513172], + [101.64270834459677, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ], + "type": "Polygon" + } + }, + + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [77.58616268346043, -7.556977393000622], + [92.76209967554053, -19.675822211591438], + [84.86920130192482, -25.38820065310992] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.5161586806538, 10.150704513809302], + [84.5161586806538, 3.6873648155754495], + [93.5370745092431, 3.6873648155754495], + [93.5370745092431, 10.150704513809302], + [84.5161586806538, 10.150704513809302] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [91.0644737234989, -0.5254035861065773], + [91.0644737234989, -15.195974906738272], + [98.81078524424902, -15.195974906738272], + [98.81078524424902, -0.5254035861065773], + [91.0644737234989, -0.5254035861065773] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/geometry-collection.geojson b/packages/turf-bbox-clip/test/in/geometry-collection.geojson new file mode 100644 index 000000000..4cb1057bc --- /dev/null +++ b/packages/turf-bbox-clip/test/in/geometry-collection.geojson @@ -0,0 +1,78 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "GeometryCollection", + "geometries": [ + { + "coordinates": [92.78854443123498, -4.0631420171898185], + "type": "Point" + }, + { + "coordinates": [88.80200582619335, -8.780228029258652], + "type": "Point" + }, + { + "coordinates": [ + [84.45578448719408, -0.42584674848238535], + [104.00625645806042, -17.30113187521333], + [108.37396514395846, -11.998337187512362] + ], + "type": "LineString" + }, + { + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [101.64270834459677, -9.59572648513172], + [101.64270834459677, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ], + "type": "Polygon" + }, + { + "coordinates": [ + [77.58616268346043, -7.556977393000622], + [92.76209967554053, -19.675822211591438], + [84.86920130192482, -25.38820065310992] + ], + "type": "LineString" + }, + { + "coordinates": [ + [ + [84.5161586806538, 10.150704513809302], + [84.5161586806538, 3.6873648155754495], + [93.5370745092431, 3.6873648155754495], + [93.5370745092431, 10.150704513809302], + [84.5161586806538, 10.150704513809302] + ] + ], + "type": "Polygon" + } + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [91.0644737234989, -0.5254035861065773], + [91.0644737234989, -15.195974906738272], + [98.81078524424902, -15.195974906738272], + [98.81078524424902, -0.5254035861065773], + [91.0644737234989, -0.5254035861065773] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/linestring-outside.geojson b/packages/turf-bbox-clip/test/in/linestring-outside.geojson new file mode 100644 index 000000000..2dd04b4ab --- /dev/null +++ b/packages/turf-bbox-clip/test/in/linestring-outside.geojson @@ -0,0 +1,32 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [84.64117263947469, 27.768789657473377], + [87.96754133213011, 27.75553407987455] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/multipoint-inside-outside.json b/packages/turf-bbox-clip/test/in/multipoint-inside-outside.json new file mode 100644 index 000000000..9c4883ad0 --- /dev/null +++ b/packages/turf-bbox-clip/test/in/multipoint-inside-outside.json @@ -0,0 +1,32 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [83.1, 24.6], + [87.0, 24.5] + ], + "type": "MultiPoint" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/point-inside.geojson b/packages/turf-bbox-clip/test/in/point-inside.geojson new file mode 100644 index 000000000..04ec1ce74 --- /dev/null +++ b/packages/turf-bbox-clip/test/in/point-inside.geojson @@ -0,0 +1,29 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [86.0, 24.6], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/point-outside.geojson b/packages/turf-bbox-clip/test/in/point-outside.geojson new file mode 100644 index 000000000..01a975314 --- /dev/null +++ b/packages/turf-bbox-clip/test/in/point-outside.geojson @@ -0,0 +1,29 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [83.0, 24.6], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/polygon-outside.geojson b/packages/turf-bbox-clip/test/in/polygon-outside.geojson new file mode 100644 index 000000000..cbf0ca59b --- /dev/null +++ b/packages/turf-bbox-clip/test/in/polygon-outside.geojson @@ -0,0 +1,37 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [88.96243031475143, 20.231448613494848], + [87.72712357729051, 19.25794633598106], + [88.84998703480346, 18.12880335033968], + [89.99249247849463, 18.633616551422037], + [88.96243031475143, 20.231448613494848] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/feature-collection.geojson b/packages/turf-bbox-clip/test/out/feature-collection.geojson new file mode 100644 index 000000000..7a48b79a0 --- /dev/null +++ b/packages/turf-bbox-clip/test/out/feature-collection.geojson @@ -0,0 +1,217 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [92.78854443123498, -4.0631420171898185], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [88.80200582619335, -8.780228029258652], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [84.45578448719408, -0.42584674848238535], + [104.00625645806042, -17.30113187521333], + [108.37396514395846, -11.998337187512362] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [101.64270834459677, -9.59572648513172], + [101.64270834459677, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [77.58616268346043, -7.556977393000622], + [92.76209967554053, -19.675822211591438], + [84.86920130192482, -25.38820065310992] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.5161586806538, 10.150704513809302], + [84.5161586806538, 3.6873648155754495], + [93.5370745092431, 3.6873648155754495], + [93.5370745092431, 10.150704513809302], + [84.5161586806538, 10.150704513809302] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Point", + "coordinates": [92.78854443123498, -4.0631420171898185] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiPoint", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [91.0644737234989, -6.130236665144828], + [98.81078524424902, -12.816582436640095] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [98.81078524424902, -9.59572648513172], + [98.81078524424902, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiPolygon", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [91.0644737234989, -0.5254035861065773], + [91.0644737234989, -15.195974906738272], + [98.81078524424902, -15.195974906738272], + [98.81078524424902, -0.5254035861065773], + [91.0644737234989, -0.5254035861065773] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/geometry-collection.geojson b/packages/turf-bbox-clip/test/out/geometry-collection.geojson new file mode 100644 index 000000000..40b829559 --- /dev/null +++ b/packages/turf-bbox-clip/test/out/geometry-collection.geojson @@ -0,0 +1,137 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "GeometryCollection", + "geometries": [ + { + "coordinates": [92.78854443123498, -4.0631420171898185], + "type": "Point" + }, + { + "coordinates": [88.80200582619335, -8.780228029258652], + "type": "Point" + }, + { + "coordinates": [ + [84.45578448719408, -0.42584674848238535], + [104.00625645806042, -17.30113187521333], + [108.37396514395846, -11.998337187512362] + ], + "type": "LineString" + }, + { + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [101.64270834459677, -9.59572648513172], + [101.64270834459677, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ], + "type": "Polygon" + }, + { + "coordinates": [ + [77.58616268346043, -7.556977393000622], + [92.76209967554053, -19.675822211591438], + [84.86920130192482, -25.38820065310992] + ], + "type": "LineString" + }, + { + "coordinates": [ + [ + [84.5161586806538, 10.150704513809302], + [84.5161586806538, 3.6873648155754495], + [93.5370745092431, 3.6873648155754495], + [93.5370745092431, 10.150704513809302], + [84.5161586806538, 10.150704513809302] + ] + ], + "type": "Polygon" + } + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "GeometryCollection", + "geometries": [ + { + "type": "Point", + "coordinates": [92.78854443123498, -4.0631420171898185] + }, + { + "type": "MultiPoint", + "coordinates": [] + }, + { + "type": "LineString", + "coordinates": [ + [91.0644737234989, -6.130236665144828], + [98.81078524424902, -12.816582436640095] + ] + }, + { + "type": "Polygon", + "coordinates": [ + [ + [95.6755173613135, -5.412355325867296], + [95.6755173613135, -9.59572648513172], + [98.81078524424902, -9.59572648513172], + [98.81078524424902, -5.412355325867296], + [95.6755173613135, -5.412355325867296] + ] + ] + }, + { + "type": "MultiLineString", + "coordinates": [] + }, + { + "type": "MultiPolygon", + "coordinates": [] + } + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [91.0644737234989, -0.5254035861065773], + [91.0644737234989, -15.195974906738272], + [98.81078524424902, -15.195974906738272], + [98.81078524424902, -0.5254035861065773], + [91.0644737234989, -0.5254035861065773] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/linestring-outside.geojson b/packages/turf-bbox-clip/test/out/linestring-outside.geojson new file mode 100644 index 000000000..3b2e4facf --- /dev/null +++ b/packages/turf-bbox-clip/test/out/linestring-outside.geojson @@ -0,0 +1,55 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [84.64117263947469, 27.768789657473377], + [87.96754133213011, 27.75553407987455] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json b/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json new file mode 100644 index 000000000..58e6a8fbb --- /dev/null +++ b/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json @@ -0,0 +1,55 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [83.1, 24.6], + [87, 24.5] + ], + "type": "MultiPoint" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiPoint", + "coordinates": [[87, 24.5]] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/point-inside.geojson b/packages/turf-bbox-clip/test/out/point-inside.geojson new file mode 100644 index 000000000..ac6f7827d --- /dev/null +++ b/packages/turf-bbox-clip/test/out/point-inside.geojson @@ -0,0 +1,52 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [86, 24.6], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Point", + "coordinates": [86, 24.6] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/point-outside.geojson b/packages/turf-bbox-clip/test/out/point-outside.geojson new file mode 100644 index 000000000..71ee5b33f --- /dev/null +++ b/packages/turf-bbox-clip/test/out/point-outside.geojson @@ -0,0 +1,52 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [83, 24.6], + "type": "Point" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiPoint", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/polygon-outside.geojson b/packages/turf-bbox-clip/test/out/polygon-outside.geojson new file mode 100644 index 000000000..adae3358c --- /dev/null +++ b/packages/turf-bbox-clip/test/out/polygon-outside.geojson @@ -0,0 +1,60 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [88.96243031475143, 20.231448613494848], + [87.72712357729051, 19.25794633598106], + [88.84998703480346, 18.12880335033968], + [89.99249247849463, 18.633616551422037], + [88.96243031475143, 20.231448613494848] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "MultiPolygon", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "coordinates": [ + [ + [84.69580856761752, 25.3651953396419], + [84.69580856761752, 22.90862421319129], + [88.38119495093474, 22.90862421319129], + [88.38119495093474, 25.3651953396419], + [84.69580856761752, 25.3651953396419] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson index e1f2e7a72..06e3446ba 100644 --- a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson @@ -35,7 +35,7 @@ "fill-opacity": 0.1 }, "geometry": { - "type": "Polygon", + "type": "MultiPolygon", "coordinates": [] } }, diff --git a/packages/turf-simplify/test/out/featurecollection.geojson b/packages/turf-simplify/test/out/featurecollection.geojson index b763b50d4..073da2fb8 100644 --- a/packages/turf-simplify/test/out/featurecollection.geojson +++ b/packages/turf-simplify/test/out/featurecollection.geojson @@ -1,8 +1,5 @@ { "type": "FeatureCollection", - "properties": { - "tolerance": 0.01 - }, "features": [ { "type": "Feature", From 5de593fd4a9bb52cb3e13ed47e7a8eb45c2c3ae1 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 15 Jan 2025 19:16:02 +1100 Subject: [PATCH 2/8] Update contributors --- packages/turf-bbox-clip/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/turf-bbox-clip/package.json b/packages/turf-bbox-clip/package.json index db1aaa155..148414697 100644 --- a/packages/turf-bbox-clip/package.json +++ b/packages/turf-bbox-clip/package.json @@ -6,7 +6,8 @@ "contributors": [ "Tim Channell <@tcql>", "Vladimir Agafonkin <@mourner>", - "Denis Carriere <@DenisCarriere>" + "Denis Carriere <@DenisCarriere>", + "Steve Bennett <@stevage>" ], "license": "MIT", "bugs": { From f4490cce5db7e1fda947fca5628ced3054eaef28 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 15 Jan 2025 19:23:26 +1100 Subject: [PATCH 3/8] Fix a couple of accidental inclusions in PR. --- .gitignore | 3 +-- packages/turf-simplify/test/out/featurecollection.geojson | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index f4a35b12f..4ced384d8 100644 --- a/.gitignore +++ b/.gitignore @@ -33,5 +33,4 @@ node_modules .vscode -.nx/ -.history/ \ No newline at end of file +.nx/ \ No newline at end of file diff --git a/packages/turf-simplify/test/out/featurecollection.geojson b/packages/turf-simplify/test/out/featurecollection.geojson index 073da2fb8..b763b50d4 100644 --- a/packages/turf-simplify/test/out/featurecollection.geojson +++ b/packages/turf-simplify/test/out/featurecollection.geojson @@ -1,5 +1,8 @@ { "type": "FeatureCollection", + "properties": { + "tolerance": 0.01 + }, "features": [ { "type": "Feature", From 6a05a9fb797631bf13547629645a361ac3124074 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 22 Jan 2025 19:01:07 +1100 Subject: [PATCH 4/8] Use function overloads to be specific about returned geometry types. --- packages/turf-bbox-clip/index.ts | 62 ++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/turf-bbox-clip/index.ts b/packages/turf-bbox-clip/index.ts index 2095aacfe..98b669294 100644 --- a/packages/turf-bbox-clip/index.ts +++ b/packages/turf-bbox-clip/index.ts @@ -27,14 +27,6 @@ import { import { getGeom } from "@turf/invariant"; import { lineclip, polygonclip } from "./lib/lineclip.js"; -type OneGeometry = - | Point - | MultiPoint - | LineString - | MultiLineString - | Polygon - | MultiPolygon; - /** * Takes a {@link Feature}, {@link Geometry} or {@link FeatureCollection} and a bbox and clips the object to the bbox using * [lineclip](https://github.com/mapbox/lineclip). @@ -54,28 +46,63 @@ type OneGeometry = * //addToMap * var addToMap = [bbox, poly, clipped] */ +// pass a specific geometry type or FeatureCollection, get the same type back +function bboxClip< + G extends Point | MultiPoint, + P extends GeoJsonProperties = GeoJsonProperties, +>(feature: Feature | G, bbox: BBox): Feature; +function bboxClip< + G extends LineString | MultiLineString, + P extends GeoJsonProperties = GeoJsonProperties, +>( + feature: Feature | G, + bbox: BBox +): Feature; +function bboxClip< + G extends Polygon | MultiPolygon, + P extends GeoJsonProperties = GeoJsonProperties, +>(feature: Feature | G, bbox: BBox): Feature; + +function bboxClip< + G extends GeometryCollection, + P extends GeoJsonProperties = GeoJsonProperties, +>(feature: Feature | G, bbox: BBox): Feature; +function bboxClip< + G extends FeatureCollection, + P extends GeoJsonProperties = GeoJsonProperties, +>(feature: G, bbox: BBox): FeatureCollection; + +// pass a non-specific geometry type, get a generic feature back +function bboxClip

( + feature: Feature, + bbox: BBox +): Feature; +function bboxClip(feature: Geometry, bbox: BBox): Feature; + +// "implementation signature", can't be called directly function bboxClip< G extends Geometry, P extends GeoJsonProperties = GeoJsonProperties, >( feature: Feature | G | FeatureCollection, bbox: BBox -): Feature | FeatureCollection { +): Feature | FeatureCollection { if (feature.type === "FeatureCollection") { return featureCollection( feature.features.map((f: Feature) => bboxClip(f, bbox) as Feature) ); } + const geom = getGeom(feature); const type = geom.type; - const properties = feature.type === "Feature" ? feature.properties : {}; + const properties: GeoJsonProperties = + feature.type === "Feature" ? feature.properties : {}; if (type === "GeometryCollection") { - const gs = geom.geometries as OneGeometry[]; - const outGs: OneGeometry[] = gs.map( - (g: OneGeometry) => - (bboxClip(g as OneGeometry, bbox) as Feature).geometry - ); + const gs = geom.geometries; + const outGs = gs.map( + (g: Geometry) => (bboxClip(g, bbox) as Feature).geometry + ) as Exclude[]; return geometryCollection(outGs, properties) as Feature< GeometryCollection, P @@ -83,7 +110,6 @@ function bboxClip< } let coords: any[] = geom.coordinates; - switch (type) { case "LineString": case "MultiLineString": { @@ -137,8 +163,8 @@ function checkCoord(coord: Position, bbox: BBox) { ); } -function clipPolygon(rings: number[][][], bbox: BBox) { - const outRings = []; +function clipPolygon(rings: Position[][], bbox: BBox) { + const outRings: Position[][] = []; for (const ring of rings) { const clipped = polygonclip(ring, bbox); if (clipped.length > 0) { From 0ff7cf71c6500fc810ff01b64ddcee71875415a0 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 22 Jan 2025 19:59:46 +1100 Subject: [PATCH 5/8] Actually Polygons with empty coordinates arrays are fine, stick with that. --- packages/turf-bbox-clip/README.md | 33 ++++++++++++------- packages/turf-bbox-clip/index.ts | 13 +++----- .../test/out/feature-collection.geojson | 2 +- .../test/out/geometry-collection.geojson | 2 +- .../test/out/polygon-outside.geojson | 2 +- .../out/polygon-point-intersection.geojson | 2 +- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/turf-bbox-clip/README.md b/packages/turf-bbox-clip/README.md index e9031ca2c..66dbf89e1 100644 --- a/packages/turf-bbox-clip/README.md +++ b/packages/turf-bbox-clip/README.md @@ -4,14 +4,15 @@ ## bboxClip -Takes a [Feature][1] and a bbox and clips the feature to the bbox using -[lineclip][2]. -May result in degenerate edges when clipping Polygons. +Takes a [Feature][1], [Geometry][2] or [FeatureCollection][3] and a bbox and clips the object to the bbox using +[lineclip][4]. +If a Point or LineString geometry is entirely outside the bbox, a [MultiPoint][5] or [MultiLineString][6] with empty `coordinates` array is returned. +LineString and Polygon geometries may also become MultiLineString or [MultiPolygon][7] if the clipping process cuts them into several pieces. ### Parameters -* `feature` **[Feature][1]<([LineString][3] | [MultiLineString][4] | [Polygon][5] | [MultiPolygon][6])>** feature to clip to the bbox -* `bbox` **[BBox][7]** extent in \[minX, minY, maxX, maxY] order +* `feature` **[Feature][1]<([Point][8] | [MultiPoint][5] | [LineString][9] | [MultiLineString][6] | [Polygon][10] | [MultiPolygon][7])>** feature to clip to the bbox +* `bbox` **[BBox][11]** extent in \[minX, minY, maxX, maxY] order ### Examples @@ -25,21 +26,29 @@ var clipped = turf.bboxClip(poly, bbox); var addToMap = [bbox, poly, clipped] ``` -Returns **[Feature][1]<([LineString][3] | [MultiLineString][4] | [Polygon][5] | [MultiPolygon][6])>** clipped Feature +Returns **[Feature][1]<([Point][8] | [MultiPoint][5] | [LineString][9] | [MultiLineString][6] | [Polygon][10] | [MultiPolygon][7])>** clipped Feature [1]: https://tools.ietf.org/html/rfc7946#section-3.2 -[2]: https://github.com/mapbox/lineclip +[2]: https://tools.ietf.org/html/rfc7946#section-3.1 -[3]: https://tools.ietf.org/html/rfc7946#section-3.1.4 +[3]: https://tools.ietf.org/html/rfc7946#section-3.3 -[4]: https://tools.ietf.org/html/rfc7946#section-3.1.5 +[4]: https://github.com/mapbox/lineclip -[5]: https://tools.ietf.org/html/rfc7946#section-3.1.6 +[5]: https://tools.ietf.org/html/rfc7946#section-3.1.3 -[6]: https://tools.ietf.org/html/rfc7946#section-3.1.7 +[6]: https://tools.ietf.org/html/rfc7946#section-3.1.5 -[7]: https://tools.ietf.org/html/rfc7946#section-5 +[7]: https://tools.ietf.org/html/rfc7946#section-3.1.7 + +[8]: https://tools.ietf.org/html/rfc7946#section-3.1.2 + +[9]: https://tools.ietf.org/html/rfc7946#section-3.1.4 + +[10]: https://tools.ietf.org/html/rfc7946#section-3.1.6 + +[11]: https://tools.ietf.org/html/rfc7946#section-5 diff --git a/packages/turf-bbox-clip/index.ts b/packages/turf-bbox-clip/index.ts index 98b669294..dc408efe1 100644 --- a/packages/turf-bbox-clip/index.ts +++ b/packages/turf-bbox-clip/index.ts @@ -30,8 +30,8 @@ import { lineclip, polygonclip } from "./lib/lineclip.js"; /** * Takes a {@link Feature}, {@link Geometry} or {@link FeatureCollection} and a bbox and clips the object to the bbox using * [lineclip](https://github.com/mapbox/lineclip). - * If a geometry is entirely outside the bbox, a Multi-geometry with no coordinates is returned. - * LineString and Polygon geometries may also become Multi-geometry if the clipping process cuts them into several pieces. + * If a Point or LineString geometry is entirely outside the bbox, a {@link MultiPoint} or {@link MultiLineString} with empty `coordinates` array is returned. + * LineString and Polygon geometries may also become MultiLineString or {@link MultiPolygon} if the clipping process cuts them into several pieces. * * @function * @param {Feature} feature feature to clip to the bbox @@ -62,7 +62,6 @@ function bboxClip< G extends Polygon | MultiPolygon, P extends GeoJsonProperties = GeoJsonProperties, >(feature: Feature | G, bbox: BBox): Feature; - function bboxClip< G extends GeometryCollection, P extends GeoJsonProperties = GeoJsonProperties, @@ -120,18 +119,14 @@ function bboxClip< coords.forEach((line) => { lineclip(line, bbox, lines); }); - if (lines.length === 1) { + if (lines.length === 1 && type === "LineString") { return lineString(lines[0], properties); } return multiLineString(lines, properties); } case "Polygon": { const poly = clipPolygon(coords, bbox); - if (poly.length === 0) { - return multiPolygon([], properties); - } else { - return polygon(poly, properties); - } + return polygon(poly, properties); } case "MultiPolygon": return multiPolygon( diff --git a/packages/turf-bbox-clip/test/out/feature-collection.geojson b/packages/turf-bbox-clip/test/out/feature-collection.geojson index 7a48b79a0..78d0e0b0e 100644 --- a/packages/turf-bbox-clip/test/out/feature-collection.geojson +++ b/packages/turf-bbox-clip/test/out/feature-collection.geojson @@ -188,7 +188,7 @@ "fill-opacity": 0.1 }, "geometry": { - "type": "MultiPolygon", + "type": "Polygon", "coordinates": [] } }, diff --git a/packages/turf-bbox-clip/test/out/geometry-collection.geojson b/packages/turf-bbox-clip/test/out/geometry-collection.geojson index 40b829559..2eafa7681 100644 --- a/packages/turf-bbox-clip/test/out/geometry-collection.geojson +++ b/packages/turf-bbox-clip/test/out/geometry-collection.geojson @@ -106,7 +106,7 @@ "coordinates": [] }, { - "type": "MultiPolygon", + "type": "Polygon", "coordinates": [] } ] diff --git a/packages/turf-bbox-clip/test/out/polygon-outside.geojson b/packages/turf-bbox-clip/test/out/polygon-outside.geojson index adae3358c..67e2d9f08 100644 --- a/packages/turf-bbox-clip/test/out/polygon-outside.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-outside.geojson @@ -31,7 +31,7 @@ "fill-opacity": 0.1 }, "geometry": { - "type": "MultiPolygon", + "type": "Polygon", "coordinates": [] } }, diff --git a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson index 06e3446ba..e1f2e7a72 100644 --- a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson @@ -35,7 +35,7 @@ "fill-opacity": 0.1 }, "geometry": { - "type": "MultiPolygon", + "type": "Polygon", "coordinates": [] } }, From 3c91ee113ca6b838845bea67c9e24bb30ca1b7ca Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 29 Jan 2025 11:00:11 +1100 Subject: [PATCH 6/8] Tweak format of outputs for easier checking. --- packages/turf-bbox-clip/test.ts | 15 +++---- .../test/out/feature-collection.geojson | 39 ++++++++++++------- .../test/out/geometry-collection.geojson | 9 +++-- .../test/out/linestring-outside.geojson | 9 +++-- .../test/out/linestring-single-line.geojson | 9 +++-- .../test/out/linestring.geojson | 9 +++-- .../test/out/multi-linestring.geojson | 9 +++-- .../test/out/multi-polygon.geojson | 9 +++-- .../test/out/multipoint-inside-outside.json | 9 +++-- .../test/out/point-inside.geojson | 9 +++-- .../test/out/point-outside.geojson | 9 +++-- .../test/out/polygon-crossing-hole.geojson | 9 +++-- .../test/out/polygon-holes.geojson | 9 +++-- .../test/out/polygon-outside.geojson | 9 +++-- .../out/polygon-point-intersection.geojson | 9 +++-- .../turf-bbox-clip/test/out/polygon.geojson | 9 +++-- 16 files changed, 118 insertions(+), 62 deletions(-) diff --git a/packages/turf-bbox-clip/test.ts b/packages/turf-bbox-clip/test.ts index 98e0dec06..22f6356a7 100644 --- a/packages/turf-bbox-clip/test.ts +++ b/packages/turf-bbox-clip/test.ts @@ -39,15 +39,15 @@ test("turf-bbox-clip", (t) => { let results; if (isFeatureCollection) { results = featureCollection([ - ...feature.features.map((f) => colorize(f, "#080")), - ...clipped.features.map((c) => colorize(c, "#F00")), - colorize(clipRegion, "#00F", 3), + ...feature.features.map((f) => colorize(f, "#080", "input")), + ...clipped.features.map((c) => colorize(c, "#F00", "output")), + colorize(clipRegion, "#00F", "clipping bbox", 3), ]); } else { results = featureCollection([ - colorize(feature, "#080"), - colorize(clipped, "#F00"), - colorize(clipRegion, "#00F", 3), + colorize(feature, "#080", "input"), + colorize(clipped, "#F00", "output"), + colorize(clipRegion, "#00F", "clipping bbox", 3), ]); } @@ -58,7 +58,7 @@ test("turf-bbox-clip", (t) => { t.end(); }); -function colorize(feature, color, width) { +function colorize(feature, color, name, width) { color = color || "#F00"; width = width || 6; feature.properties = { @@ -66,6 +66,7 @@ function colorize(feature, color, width) { fill: color, "stroke-width": width, "fill-opacity": 0.1, + name, }; return feature; } diff --git a/packages/turf-bbox-clip/test/out/feature-collection.geojson b/packages/turf-bbox-clip/test/out/feature-collection.geojson index 78d0e0b0e..e9e5c6723 100644 --- a/packages/turf-bbox-clip/test/out/feature-collection.geojson +++ b/packages/turf-bbox-clip/test/out/feature-collection.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [92.78854443123498, -4.0631420171898185], @@ -20,7 +21,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [88.80200582619335, -8.780228029258652], @@ -33,7 +35,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -50,7 +53,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -71,7 +75,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -88,7 +93,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -109,7 +115,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Point", @@ -122,7 +129,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiPoint", @@ -135,7 +143,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "LineString", @@ -151,7 +160,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -172,7 +182,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiLineString", @@ -185,7 +196,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -198,7 +210,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/geometry-collection.geojson b/packages/turf-bbox-clip/test/out/geometry-collection.geojson index 2eafa7681..b97e1773d 100644 --- a/packages/turf-bbox-clip/test/out/geometry-collection.geojson +++ b/packages/turf-bbox-clip/test/out/geometry-collection.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "type": "GeometryCollection", @@ -69,7 +70,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "GeometryCollection", @@ -118,7 +120,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/linestring-outside.geojson b/packages/turf-bbox-clip/test/out/linestring-outside.geojson index 3b2e4facf..d00c7b2e9 100644 --- a/packages/turf-bbox-clip/test/out/linestring-outside.geojson +++ b/packages/turf-bbox-clip/test/out/linestring-outside.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -23,7 +24,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiLineString", @@ -36,7 +38,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/linestring-single-line.geojson b/packages/turf-bbox-clip/test/out/linestring-single-line.geojson index 58e23f2c9..791ea69cb 100644 --- a/packages/turf-bbox-clip/test/out/linestring-single-line.geojson +++ b/packages/turf-bbox-clip/test/out/linestring-single-line.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "type": "LineString", @@ -24,7 +25,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "LineString", @@ -41,7 +43,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/linestring.geojson b/packages/turf-bbox-clip/test/out/linestring.geojson index e2790b756..4928f55f8 100644 --- a/packages/turf-bbox-clip/test/out/linestring.geojson +++ b/packages/turf-bbox-clip/test/out/linestring.geojson @@ -1080,7 +1080,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "type": "Feature" }, @@ -1090,7 +1091,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiLineString", @@ -1763,7 +1765,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/multi-linestring.geojson b/packages/turf-bbox-clip/test/out/multi-linestring.geojson index 2cf362123..715f8e252 100644 --- a/packages/turf-bbox-clip/test/out/multi-linestring.geojson +++ b/packages/turf-bbox-clip/test/out/multi-linestring.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "type": "MultiLineString", @@ -40,7 +41,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiLineString", @@ -69,7 +71,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/multi-polygon.geojson b/packages/turf-bbox-clip/test/out/multi-polygon.geojson index 4d052ed8e..6f0ab1401 100644 --- a/packages/turf-bbox-clip/test/out/multi-polygon.geojson +++ b/packages/turf-bbox-clip/test/out/multi-polygon.geojson @@ -1097,7 +1097,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "type": "Feature" }, @@ -1107,7 +1108,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiPolygon", @@ -1451,7 +1453,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json b/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json index 58e6a8fbb..8f513bca3 100644 --- a/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json +++ b/packages/turf-bbox-clip/test/out/multipoint-inside-outside.json @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -23,7 +24,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiPoint", @@ -36,7 +38,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/point-inside.geojson b/packages/turf-bbox-clip/test/out/point-inside.geojson index ac6f7827d..2027e440c 100644 --- a/packages/turf-bbox-clip/test/out/point-inside.geojson +++ b/packages/turf-bbox-clip/test/out/point-inside.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [86, 24.6], @@ -20,7 +21,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Point", @@ -33,7 +35,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/point-outside.geojson b/packages/turf-bbox-clip/test/out/point-outside.geojson index 71ee5b33f..e02b46c75 100644 --- a/packages/turf-bbox-clip/test/out/point-outside.geojson +++ b/packages/turf-bbox-clip/test/out/point-outside.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [83, 24.6], @@ -20,7 +21,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "MultiPoint", @@ -33,7 +35,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/polygon-crossing-hole.geojson b/packages/turf-bbox-clip/test/out/polygon-crossing-hole.geojson index ea35c4e00..923c722bd 100644 --- a/packages/turf-bbox-clip/test/out/polygon-crossing-hole.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-crossing-hole.geojson @@ -1090,7 +1090,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "type": "Feature" }, @@ -1100,7 +1101,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -1322,7 +1324,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/polygon-holes.geojson b/packages/turf-bbox-clip/test/out/polygon-holes.geojson index 8f1854352..c992817b1 100644 --- a/packages/turf-bbox-clip/test/out/polygon-holes.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-holes.geojson @@ -1097,7 +1097,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "type": "Feature" }, @@ -1107,7 +1108,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -1396,7 +1398,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/polygon-outside.geojson b/packages/turf-bbox-clip/test/out/polygon-outside.geojson index 67e2d9f08..50ad1933f 100644 --- a/packages/turf-bbox-clip/test/out/polygon-outside.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-outside.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "coordinates": [ @@ -28,7 +29,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -41,7 +43,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "coordinates": [ diff --git a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson index e1f2e7a72..9bb8ab62a 100644 --- a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson +++ b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson @@ -7,7 +7,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "geometry": { "type": "Polygon", @@ -32,7 +33,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -45,7 +47,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", diff --git a/packages/turf-bbox-clip/test/out/polygon.geojson b/packages/turf-bbox-clip/test/out/polygon.geojson index 747fd31ea..c57a11e85 100644 --- a/packages/turf-bbox-clip/test/out/polygon.geojson +++ b/packages/turf-bbox-clip/test/out/polygon.geojson @@ -1083,7 +1083,8 @@ "stroke": "#080", "fill": "#080", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "input" }, "type": "Feature" }, @@ -1093,7 +1094,8 @@ "stroke": "#F00", "fill": "#F00", "stroke-width": 6, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "output" }, "geometry": { "type": "Polygon", @@ -1638,7 +1640,8 @@ "stroke": "#00F", "fill": "#00F", "stroke-width": 3, - "fill-opacity": 0.1 + "fill-opacity": 0.1, + "name": "clipping bbox" }, "geometry": { "type": "Polygon", From 52a08d84494b3bff320198a7fe68cdc914d5aaaf Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 29 Jan 2025 11:01:37 +1100 Subject: [PATCH 7/8] Update doc to describe (probably incorrect) current behaviour. See #2816 --- packages/turf-bbox-clip/index.ts | 13 ++-- .../test/in/linestring-two-pieces.geojson | 33 +++++++++ .../test/in/polygon-two-pieces.geojson | 37 ++++++++++ .../test/out/linestring-two-pieces.geojson | 68 +++++++++++++++++ .../test/out/polygon-two-pieces.geojson | 73 +++++++++++++++++++ 5 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 packages/turf-bbox-clip/test/in/linestring-two-pieces.geojson create mode 100644 packages/turf-bbox-clip/test/in/polygon-two-pieces.geojson create mode 100644 packages/turf-bbox-clip/test/out/linestring-two-pieces.geojson create mode 100644 packages/turf-bbox-clip/test/out/polygon-two-pieces.geojson diff --git a/packages/turf-bbox-clip/index.ts b/packages/turf-bbox-clip/index.ts index dc408efe1..9d417f92d 100644 --- a/packages/turf-bbox-clip/index.ts +++ b/packages/turf-bbox-clip/index.ts @@ -28,15 +28,15 @@ import { getGeom } from "@turf/invariant"; import { lineclip, polygonclip } from "./lib/lineclip.js"; /** - * Takes a {@link Feature}, {@link Geometry} or {@link FeatureCollection} and a bbox and clips the object to the bbox using - * [lineclip](https://github.com/mapbox/lineclip). + * Takes a {@link Feature}, {@link Geometry} or {@link FeatureCollection} and a bbox and returns the part of the object within the bbox. + * Lines and polygons are clipped using [lineclip](https://github.com/mapbox/lineclip). * If a Point or LineString geometry is entirely outside the bbox, a {@link MultiPoint} or {@link MultiLineString} with empty `coordinates` array is returned. - * LineString and Polygon geometries may also become MultiLineString or {@link MultiPolygon} if the clipping process cuts them into several pieces. + * LineString geometries may also become MultiLineString if the clipping process cuts them into several pieces. * * @function - * @param {Feature} feature feature to clip to the bbox + * @param {Geometry|Feature|FeatureCollection} feature GeoJSON object to clip to the bbox * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order - * @returns {Feature} clipped Feature + * @returns {Feature|FeatureCollection} clipped GeoJSON object. * @example * var bbox = [0, 0, 10, 10]; * var poly = turf.polygon([[[2, 2], [8, 4], [12, 8], [3, 7], [2, 2]]]); @@ -92,6 +92,9 @@ function bboxClip< ); } + // const test = { type: "Point", coordinates: [0, 0] } as Geometry; + // const out = bboxClip(test, bbox); + const geom = getGeom(feature); const type = geom.type; const properties: GeoJsonProperties = diff --git a/packages/turf-bbox-clip/test/in/linestring-two-pieces.geojson b/packages/turf-bbox-clip/test/in/linestring-two-pieces.geojson new file mode 100644 index 000000000..387ef742f --- /dev/null +++ b/packages/turf-bbox-clip/test/in/linestring-two-pieces.geojson @@ -0,0 +1,33 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [60, 47], + [65, 49], + [70, 46] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [58, 48], + [58, 45], + [71, 45], + [71, 48], + [58, 48] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/in/polygon-two-pieces.geojson b/packages/turf-bbox-clip/test/in/polygon-two-pieces.geojson new file mode 100644 index 000000000..5c38247f6 --- /dev/null +++ b/packages/turf-bbox-clip/test/in/polygon-two-pieces.geojson @@ -0,0 +1,37 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [60, 47], + [65, 49], + [70, 46], + [65, 51], + [60, 47] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "coordinates": [ + [ + [58, 48], + [58, 45], + [71, 45], + [71, 48], + [58, 48] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/linestring-two-pieces.geojson b/packages/turf-bbox-clip/test/out/linestring-two-pieces.geojson new file mode 100644 index 000000000..89c519802 --- /dev/null +++ b/packages/turf-bbox-clip/test/out/linestring-two-pieces.geojson @@ -0,0 +1,68 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1, + "name": "input" + }, + "geometry": { + "coordinates": [ + [60, 47], + [65, 49], + [70, 46] + ], + "type": "LineString" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1, + "name": "output" + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [60, 47], + [62.5, 48] + ], + [ + [66.66666666666667, 48], + [70, 46] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1, + "name": "clipping bbox" + }, + "geometry": { + "coordinates": [ + [ + [58, 48], + [58, 45], + [71, 45], + [71, 48], + [58, 48] + ] + ], + "type": "Polygon" + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/polygon-two-pieces.geojson b/packages/turf-bbox-clip/test/out/polygon-two-pieces.geojson new file mode 100644 index 000000000..eefe89dbb --- /dev/null +++ b/packages/turf-bbox-clip/test/out/polygon-two-pieces.geojson @@ -0,0 +1,73 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1, + "name": "input" + }, + "geometry": { + "coordinates": [ + [ + [60, 47], + [65, 49], + [70, 46], + [65, 51], + [60, 47] + ] + ], + "type": "Polygon" + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1, + "name": "output" + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [60, 47], + [62.5, 48], + [66.66666666666667, 48], + [70, 46], + [68, 48], + [61.25, 48], + [60, 47] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1, + "name": "clipping bbox" + }, + "geometry": { + "coordinates": [ + [ + [58, 48], + [58, 45], + [71, 45], + [71, 48], + [58, 48] + ] + ], + "type": "Polygon" + } + } + ] +} From 001a56e426e564e67158b654ea57cb47d777f29d Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 29 Jan 2025 11:01:58 +1100 Subject: [PATCH 8/8] README --- packages/turf-bbox-clip/README.md | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/turf-bbox-clip/README.md b/packages/turf-bbox-clip/README.md index 66dbf89e1..882da4b62 100644 --- a/packages/turf-bbox-clip/README.md +++ b/packages/turf-bbox-clip/README.md @@ -4,15 +4,15 @@ ## bboxClip -Takes a [Feature][1], [Geometry][2] or [FeatureCollection][3] and a bbox and clips the object to the bbox using -[lineclip][4]. +Takes a [Feature][1], [Geometry][2] or [FeatureCollection][3] and a bbox and returns the part of the object within the bbox. +Lines and polygons are clipped using [lineclip][4]. If a Point or LineString geometry is entirely outside the bbox, a [MultiPoint][5] or [MultiLineString][6] with empty `coordinates` array is returned. -LineString and Polygon geometries may also become MultiLineString or [MultiPolygon][7] if the clipping process cuts them into several pieces. +LineString geometries may also become MultiLineString if the clipping process cuts them into several pieces. ### Parameters -* `feature` **[Feature][1]<([Point][8] | [MultiPoint][5] | [LineString][9] | [MultiLineString][6] | [Polygon][10] | [MultiPolygon][7])>** feature to clip to the bbox -* `bbox` **[BBox][11]** extent in \[minX, minY, maxX, maxY] order +* `feature` **([Geometry][2] | [Feature][1] | [FeatureCollection][3])** GeoJSON object to clip to the bbox +* `bbox` **[BBox][7]** extent in \[minX, minY, maxX, maxY] order ### Examples @@ -26,7 +26,7 @@ var clipped = turf.bboxClip(poly, bbox); var addToMap = [bbox, poly, clipped] ``` -Returns **[Feature][1]<([Point][8] | [MultiPoint][5] | [LineString][9] | [MultiLineString][6] | [Polygon][10] | [MultiPolygon][7])>** clipped Feature +Returns **([Feature][1] | [FeatureCollection][3])** clipped GeoJSON object. [1]: https://tools.ietf.org/html/rfc7946#section-3.2 @@ -40,15 +40,7 @@ Returns **[Feature][1]<([Point][8] | [MultiPoint][5] | [LineString][9] | [MultiL [6]: https://tools.ietf.org/html/rfc7946#section-3.1.5 -[7]: https://tools.ietf.org/html/rfc7946#section-3.1.7 - -[8]: https://tools.ietf.org/html/rfc7946#section-3.1.2 - -[9]: https://tools.ietf.org/html/rfc7946#section-3.1.4 - -[10]: https://tools.ietf.org/html/rfc7946#section-3.1.6 - -[11]: https://tools.ietf.org/html/rfc7946#section-5 +[7]: https://tools.ietf.org/html/rfc7946#section-5