From 1d0a239ec5d581c3f7637a6833d1e88a9766ae1f Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Wed, 2 Aug 2017 17:10:23 +1000 Subject: [PATCH] Fix bbox-clip point intersection handling Ensure a polygon that has a single point touching the bounding box will be handled without an error. --- packages/turf-bbox-clip/index.js | 4 +- .../in/polygon-point-intersection.geojson | 83 ++++++++++++++ .../out/polygon-point-intersection.geojson | 106 ++++++++++++++++++ 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 packages/turf-bbox-clip/test/in/polygon-point-intersection.geojson create mode 100644 packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson diff --git a/packages/turf-bbox-clip/index.js b/packages/turf-bbox-clip/index.js index a4b5898bc7..b63136f310 100644 --- a/packages/turf-bbox-clip/index.js +++ b/packages/turf-bbox-clip/index.js @@ -57,7 +57,9 @@ function clipPolygon(rings, bbox) { if (clipped[0][0] !== clipped[clipped.length - 1][0] || clipped[0][1] !== clipped[clipped.length - 1][1]) { clipped.push(clipped[0]); } - outRings.push(clipped); + if (clipped.length >= 4) { + outRings.push(clipped); + } } } return outRings; diff --git a/packages/turf-bbox-clip/test/in/polygon-point-intersection.geojson b/packages/turf-bbox-clip/test/in/polygon-point-intersection.geojson new file mode 100644 index 0000000000..0b5002a451 --- /dev/null +++ b/packages/turf-bbox-clip/test/in/polygon-point-intersection.geojson @@ -0,0 +1,83 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 115.872872384, + -31.977789227500004 + ], + [ + 115.87413347200001, + -31.978896027 + ], + [ + 115.874207392, + -31.978955634 + ], + [ + 115.874293312, + -31.978886666 + ], + [ + 115.875, + -31.9782948325 + ], + [ + 115.873511744, + -31.977027638 + ], + [ + 115.8731464, + -31.977071372 + ], + [ + 115.872185504, + -31.9771863865 + ], + [ + 115.872872384, + -31.977789227500004 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 115.875, + -31.98 + ], + [ + 115.880, + -31.98 + ], + [ + 115.880, + -31.975 + ], + [ + 115.875, + -31.975 + ], + [ + 115.875, + -31.98 + ] + ] + ] + } + } + ] +} diff --git a/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson new file mode 100644 index 0000000000..d9fb5742d3 --- /dev/null +++ b/packages/turf-bbox-clip/test/out/polygon-point-intersection.geojson @@ -0,0 +1,106 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#080", + "fill": "#080", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 115.872872384, + -31.977789227500004 + ], + [ + 115.87413347200001, + -31.978896027 + ], + [ + 115.874207392, + -31.978955634 + ], + [ + 115.874293312, + -31.978886666 + ], + [ + 115.875, + -31.9782948325 + ], + [ + 115.873511744, + -31.977027638 + ], + [ + 115.8731464, + -31.977071372 + ], + [ + 115.872185504, + -31.9771863865 + ], + [ + 115.872872384, + -31.977789227500004 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 6, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "fill-opacity": 0.1 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + 115.875, + -31.98 + ], + [ + 115.880, + -31.98 + ], + [ + 115.880, + -31.975 + ], + [ + 115.875, + -31.975 + ], + [ + 115.875, + -31.98 + ] + ] + ] + } + } + ] +}