Skip to content

Commit

Permalink
Update doc to describe (probably incorrect) current behaviour. See #2816
Browse files Browse the repository at this point in the history
  • Loading branch information
stevage committed Jan 29, 2025
1 parent 3c91ee1 commit 52a08d8
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point|MultiPoint|LineString|MultiLineString|Polygon|MultiPolygon>} 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<Point|MultiPoint|LineString|MultiLineString|Polygon|MultiPolygon>} 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]]]);
Expand Down Expand Up @@ -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 =
Expand Down
33 changes: 33 additions & 0 deletions packages/turf-bbox-clip/test/in/linestring-two-pieces.geojson
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
37 changes: 37 additions & 0 deletions packages/turf-bbox-clip/test/in/polygon-two-pieces.geojson
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
68 changes: 68 additions & 0 deletions packages/turf-bbox-clip/test/out/linestring-two-pieces.geojson
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
73 changes: 73 additions & 0 deletions packages/turf-bbox-clip/test/out/polygon-two-pieces.geojson
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}

0 comments on commit 52a08d8

Please sign in to comment.