Skip to content

Commit eac4041

Browse files
authored
fix(boolean-contains): treat an equal two-vertex line as contained (#3080)
1 parent 263d357 commit eac4041

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

packages/turf-boolean-contains/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
MultiPolygon,
88
Point,
99
Polygon,
10+
Position,
1011
} from "geojson";
1112
import { bbox as calcBbox } from "@turf/bbox";
1213
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
@@ -176,7 +177,9 @@ function isMultiPointInPoly(polygon: Polygon, multiPoint: MultiPoint) {
176177

177178
function isLineOnLine(lineString1: LineString, lineString2: LineString) {
178179
let haveFoundInteriorPoint = false;
179-
for (const coords of lineString2.coordinates) {
180+
const coordinates = lineString2.coordinates;
181+
for (let i = 0; i < coordinates.length; i++) {
182+
const coords = coordinates[i];
180183
if (
181184
isPointOnLine({ type: "Point", coordinates: coords }, lineString1, {
182185
ignoreEndVertices: true,
@@ -191,6 +194,23 @@ function isLineOnLine(lineString1: LineString, lineString2: LineString) {
191194
) {
192195
return false;
193196
}
197+
// A segment whose endpoints are both on lineString1 (e.g. lineString2's
198+
// vertices coincide with lineString1's boundary) still shares interior with
199+
// lineString1. Probe the segment midpoint so an interior overlap is detected
200+
// even when no vertex of lineString2 is strictly interior.
201+
if (!haveFoundInteriorPoint && i > 0) {
202+
const midpoint: Position = [
203+
(coordinates[i - 1][0] + coords[0]) / 2,
204+
(coordinates[i - 1][1] + coords[1]) / 2,
205+
];
206+
if (
207+
isPointOnLine({ type: "Point", coordinates: midpoint }, lineString1, {
208+
ignoreEndVertices: true,
209+
})
210+
) {
211+
haveFoundInteriorPoint = true;
212+
}
213+
}
194214
}
195215
return haveFoundInteriorPoint;
196216
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[0, 0],
11+
[10, 0]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[0, 0],
22+
[10, 0]
23+
]
24+
}
25+
}
26+
]
27+
}

0 commit comments

Comments
 (0)