From 0ff7cf71c6500fc810ff01b64ddcee71875415a0 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Wed, 22 Jan 2025 19:59:46 +1100 Subject: [PATCH] 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 e9031ca2c0..66dbf89e12 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 98b6692946..dc408efe14 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 7a48b79a09..78d0e0b0ee 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 40b829559c..2eafa76816 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 adae3358c2..67e2d9f08f 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 06e3446ba4..e1f2e7a726 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": [] } },