Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (Multi)Point,FeatureCollection and GeometryCollection in bboxClip #2814

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Actually Polygons with empty coordinates arrays are fine, stick with …
…that.
stevage committed Jan 22, 2025
commit 0ff7cf71c6500fc810ff01b64ddcee71875415a0
33 changes: 21 additions & 12 deletions packages/turf-bbox-clip/README.md
Original file line number Diff line number Diff line change
@@ -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

<!-- 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. -->

13 changes: 4 additions & 9 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
@@ -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(
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@
"coordinates": []
},
{
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
]
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
@@ -31,7 +31,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@
"fill-opacity": 0.1
},
"geometry": {
"type": "MultiPolygon",
"type": "Polygon",
"coordinates": []
}
},
Loading