Skip to content

Commit

Permalink
Actually Polygons with empty coordinates arrays are fine, stick with …
Browse files Browse the repository at this point in the history
…that.
  • Loading branch information
stevage committed Jan 22, 2025
1 parent 6a05a9f commit 0ff7cf7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
33 changes: 21 additions & 12 deletions packages/turf-bbox-clip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->

Expand Down
13 changes: 4 additions & 9 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point|MultiPoint|LineString|MultiLineString|Polygon|MultiPolygon>} feature feature to clip to the bbox
Expand Down Expand Up @@ -62,7 +62,6 @@ function bboxClip<
G extends Polygon | MultiPolygon,
P extends GeoJsonProperties = GeoJsonProperties,
>(feature: Feature<G, P> | G, bbox: BBox): Feature<Polygon | MultiPolygon, P>;

function bboxClip<
G extends GeometryCollection,
P extends GeoJsonProperties = GeoJsonProperties,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"coordinates": []
},
{
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox-clip/test/out/polygon-outside.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Expand Down

0 comments on commit 0ff7cf7

Please sign in to comment.