diff --git a/packages/turf-line-overlap/index.ts b/packages/turf-line-overlap/index.ts index 6b1d1f8561..0e8bd96cf2 100644 --- a/packages/turf-line-overlap/index.ts +++ b/packages/turf-line-overlap/index.ts @@ -4,6 +4,7 @@ import { nearestPointOnLine } from "@turf/nearest-point-on-line"; import { booleanPointOnLine } from "@turf/boolean-point-on-line"; import { getCoords } from "@turf/invariant"; import { featureEach, segmentEach } from "@turf/meta"; +import { bbox } from "@turf/bbox"; import { FeatureCollection, Feature, @@ -69,6 +70,11 @@ function lineOverlap< return; } + // Expand segment bounding box by the tolerance to create overlap in horizontal/vertical segments + var bb = bbox(segment); + bb = [bb[0]-tolerance, bb[1]-tolerance, bb[2]+tolerance, bb[3]+tolerance]; + segment.bbox = bb; + // Iterate over each segments which falls within the same bounds featureEach(tree.search(segment), function (match) { if (doesOverlaps === false) { diff --git a/packages/turf-line-overlap/package.json b/packages/turf-line-overlap/package.json index 6b96d4c0bc..904419af72 100644 --- a/packages/turf-line-overlap/package.json +++ b/packages/turf-line-overlap/package.json @@ -4,7 +4,7 @@ "description": "Takes any LineString or Polygon and returns the overlapping lines between both features.", "author": "Turf Authors", "contributors": [ - "Denis Carriere <@DenisCarriere>" + "Denis Carriere <@DenisCarriere>", "Johannes Novotny <@jonovotny>" ], "license": "MIT", "bugs": { @@ -66,6 +66,7 @@ "write-json-file": "^6.0.0" }, "dependencies": { + "@turf/bbox": "workspace:*", "@turf/boolean-point-on-line": "workspace:*", "@turf/geojson-rbush": "workspace:*", "@turf/helpers": "workspace:*", diff --git a/packages/turf-line-overlap/test/in/issue-#2582.geojson b/packages/turf-line-overlap/test/in/issue-#2582.geojson new file mode 100644 index 0000000000..daa3c61919 --- /dev/null +++ b/packages/turf-line-overlap/test/in/issue-#2582.geojson @@ -0,0 +1,56 @@ +{ + "type": "FeatureCollection", + "properties": { + "tolerance": 0.5 + }, + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 10, + "stroke-opacity": 1, + "fill-opacity": 0.1, + "description": "triangle with one horizontal and one vertical side" + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [-75.17098005568795, 25.347140710069592], + [-74.06346334926921, 25.347140710069592], + [-74.06346334926921, 26.453856979730748], + [-75.17098005568795, 25.347140710069592] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "stroke-opacity": 1, + "fill-opacity": 0.1, + "description": "polygon with segments parallel to the horizontal and vertical sides of the triangle, offset within overlap tolerance." + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [-75.77433328502653, 24.801355223074808], + [-75.09510282010693, 25.344703228173987], + [-74.22891563805884, 25.344703228173987], + [-74.05974780288312, 25.19753021415865], + [-74.05974780288312, 26.642828790428396], + [-73.31725580493922, 27.286000671432706], + [-73.31725580493922, 24.80135522307481], + [-75.77433328502653, 24.801355223074808] + ] + ] + } + } + ] +} diff --git a/packages/turf-line-overlap/test/out/issue-#2582.geojson b/packages/turf-line-overlap/test/out/issue-#2582.geojson new file mode 100644 index 0000000000..3ec1ec3a04 --- /dev/null +++ b/packages/turf-line-overlap/test/out/issue-#2582.geojson @@ -0,0 +1,144 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#0F0", + "fill": "#0F0", + "stroke-width": 25 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -74.22891563805884, + 25.344703228173987 + ], + [ + -75.09510282010693, + 25.344703228173987 + ] + ] + }, + "bbox": [ + -75.59510282010693, + 24.844703228173987, + -73.72891563805884, + 25.844703228173987 + ] + }, + { + "type": "Feature", + "properties": { + "stroke": "#0F0", + "fill": "#0F0", + "stroke-width": 25 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -74.06346334926921, + 25.347140710069592 + ], + [ + -74.06346334926921, + 26.453856979730748 + ] + ] + }, + "bbox": [ + -74.06346334926921, + 25.347140710069592, + -74.06346334926921, + 26.453856979730748 + ], + "id": 1 + }, + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "stroke-width": 10, + "stroke-opacity": 1, + "fill-opacity": 0.1, + "description": "triangle with one horizontal and one vertical side" + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -75.17098005568795, + 25.347140710069592 + ], + [ + -74.06346334926921, + 25.347140710069592 + ], + [ + -74.06346334926921, + 26.453856979730748 + ], + [ + -75.17098005568795, + 25.347140710069592 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "stroke-width": 3, + "stroke-opacity": 1, + "fill-opacity": 0.1, + "description": "polygon with segments parallel to the horizontal and vertical sides of the triangle, offset within overlap tolerance." + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -75.77433328502653, + 24.801355223074808 + ], + [ + -75.09510282010693, + 25.344703228173987 + ], + [ + -74.22891563805884, + 25.344703228173987 + ], + [ + -74.05974780288312, + 25.19753021415865 + ], + [ + -74.05974780288312, + 26.642828790428396 + ], + [ + -73.31725580493922, + 27.286000671432706 + ], + [ + -73.31725580493922, + 24.80135522307481 + ], + [ + -75.77433328502653, + 24.801355223074808 + ] + ] + ] + } + } + ] +}