Skip to content

Commit 0b95a74

Browse files
authored
Merge branch 'master' into mf/line-chunk-ts
2 parents 0c143e0 + 6717ef5 commit 0b95a74

14 files changed

Lines changed: 397 additions & 20 deletions

packages/turf-boolean-crosses/index.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Feature, Geometry, Polygon, LineString, MultiPoint } from "geojson";
22
import { lineIntersect } from "@turf/line-intersect";
33
import { polygonToLine } from "@turf/polygon-to-line";
4+
import { booleanEqual } from "@turf/boolean-equal";
45
import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
56
import { getGeom } from "@turf/invariant";
67
import { point } from "@turf/helpers";
@@ -101,27 +102,37 @@ function doMultiPointAndLineStringCross(
101102
}
102103

103104
function doLineStringsCross(lineString1: LineString, lineString2: LineString) {
104-
var doLinesIntersect = lineIntersect(lineString1, lineString2);
105-
if (doLinesIntersect.features.length > 0) {
106-
for (var i = 0; i < lineString1.coordinates.length - 1; i++) {
107-
for (var i2 = 0; i2 < lineString2.coordinates.length - 1; i2++) {
108-
var incEndVertices = true;
109-
if (i2 === 0 || i2 === lineString2.coordinates.length - 2) {
110-
incEndVertices = false;
111-
}
112-
if (
113-
isPointOnLineSegment(
114-
lineString1.coordinates[i],
115-
lineString1.coordinates[i + 1],
116-
lineString2.coordinates[i2],
117-
incEndVertices
118-
)
119-
) {
120-
return true;
121-
}
122-
}
105+
// See if there are any intersection points between these two lines.
106+
const doLinesIntersect = lineIntersect(lineString1, lineString2);
107+
if (doLinesIntersect.features.length === 0) return false;
108+
109+
// As soon as we find any intersect point that doesn't lie on the start or
110+
// end point of either input lineString, that's considered a crossing.
111+
for (const intersectPoint of doLinesIntersect.features) {
112+
if (
113+
!booleanEqual(
114+
intersectPoint.geometry,
115+
point(lineString1.coordinates[0])
116+
) &&
117+
!booleanEqual(
118+
intersectPoint.geometry,
119+
point(lineString1.coordinates[lineString1.coordinates.length - 1])
120+
) &&
121+
!booleanEqual(
122+
intersectPoint.geometry,
123+
point(lineString2.coordinates[0])
124+
) &&
125+
!booleanEqual(
126+
intersectPoint.geometry,
127+
point(lineString2.coordinates[lineString2.coordinates.length - 1])
128+
)
129+
) {
130+
return true;
123131
}
124132
}
133+
134+
// Every intersection point we tried fell on an end point (considered the
135+
// boundary) of one of the input lines. Not considered a crossing.
125136
return false;
126137
}
127138

packages/turf-boolean-crosses/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"author": "Turf Authors",
66
"contributors": [
77
"Rowan Winsemius <@rowanwins>",
8-
"Denis Carriere <@DenisCarriere>"
8+
"Denis Carriere <@DenisCarriere>",
9+
"James Beard <@smallsaucepan>"
910
],
1011
"license": "MIT",
1112
"bugs": {
@@ -67,6 +68,7 @@
6768
"typescript": "^5.8.3"
6869
},
6970
"dependencies": {
71+
"@turf/boolean-equal": "workspace:*",
7072
"@turf/boolean-point-in-polygon": "workspace:*",
7173
"@turf/helpers": "workspace:*",
7274
"@turf/invariant": "workspace:*",
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+
[5, 0]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[1, 0],
22+
[6, 0]
23+
]
24+
}
25+
}
26+
]
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[-2, 2],
11+
[1, 2.4]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[1, 1],
22+
[1, 2],
23+
[1, 3],
24+
[1, 4]
25+
]
26+
}
27+
}
28+
]
29+
}

packages/turf-boolean-crosses/test/false/LineString/LineString/LineDoesNotCrossButTouches.geojson renamed to packages/turf-boolean-crosses/test/false/LineString/LineString/LineTouchesLineOnEndVertex.geojson

File renamed without changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[-2, 2],
11+
[1, 3]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[1, 1],
22+
[1, 2],
23+
[1, 3],
24+
[1, 4]
25+
]
26+
}
27+
}
28+
]
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[-2, 2],
11+
[4, 2]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[1, 1],
22+
[1, 2.2],
23+
[1, 3],
24+
[1, 4]
25+
]
26+
}
27+
}
28+
]
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[-2, 1],
11+
[4, 1]
12+
]
13+
}
14+
},
15+
{
16+
"type": "Feature",
17+
"properties": {},
18+
"geometry": {
19+
"type": "LineString",
20+
"coordinates": [
21+
[1, 1],
22+
[1, 2],
23+
[1, 3],
24+
[1, 4]
25+
]
26+
}
27+
}
28+
]
29+
}

packages/turf-boolean-crosses/test/true/LineString/LineString/LineCrossesLine.geojson renamed to packages/turf-boolean-crosses/test/true/LineString/LineString/LineCrossesLineOnInternalVertex.geojson

File renamed without changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {},
7+
"geometry": {
8+
"type": "LineString",
9+
"coordinates": [
10+
[-70.6433177, 42.6868265],
11+
[-70.6432566, 42.6868154],
12+
[-70.6431987, 42.6868074],
13+
[-70.6431823, 42.686827],
14+
[-70.6431638, 42.686834],
15+
[-70.6431554, 42.6868565],
16+
[-70.6431197, 42.6868921],
17+
[-70.6430858, 42.6869429],
18+
[-70.6430718, 42.6870095],
19+
[-70.6430576, 42.6870561],
20+
[-70.6430143, 42.6871279],
21+
[-70.6429803, 42.6872049],
22+
[-70.6429879, 42.6872361],
23+
[-70.6430069, 42.687294],
24+
[-70.6429886, 42.6873373],
25+
[-70.6429655, 42.6873761],
26+
[-70.6429517, 42.6874289],
27+
[-70.6429176, 42.6874653],
28+
[-70.6430169, 42.6874724],
29+
[-70.6430173, 42.6875046],
30+
[-70.6429568, 42.6875301],
31+
[-70.6429351, 42.6875707],
32+
[-70.6429639, 42.6876026],
33+
[-70.6429618, 42.6876273],
34+
[-70.6429205, 42.6876437],
35+
[-70.642979, 42.6876995],
36+
[-70.643058, 42.6876823],
37+
[-70.6431052, 42.6876416],
38+
[-70.6431347, 42.6876132],
39+
[-70.6431633, 42.6875763],
40+
[-70.6432075, 42.6875091],
41+
[-70.6432276, 42.6875191],
42+
[-70.643243, 42.6875865],
43+
[-70.6432592, 42.6876215],
44+
[-70.6432361, 42.6876818],
45+
[-70.6432436, 42.6877306],
46+
[-70.6432404, 42.6877815],
47+
[-70.6432299, 42.6878144],
48+
[-70.6432724, 42.687847],
49+
[-70.6433394, 42.6878478],
50+
[-70.6433343, 42.6879072],
51+
[-70.6433207, 42.6879654],
52+
[-70.6433571, 42.6880021],
53+
[-70.643407, 42.6880716],
54+
[-70.6434145, 42.6881248],
55+
[-70.6434053, 42.6882038],
56+
[-70.6433689, 42.688232],
57+
[-70.6433801, 42.6882777],
58+
[-70.6434366, 42.6882993],
59+
[-70.6434638, 42.6883448],
60+
[-70.6435474, 42.6883564],
61+
[-70.6436326, 42.6883874],
62+
[-70.6436274, 42.6884498],
63+
[-70.6436621, 42.6884967],
64+
[-70.6436683, 42.6885168]
65+
]
66+
}
67+
},
68+
{
69+
"type": "Feature",
70+
"properties": {},
71+
"geometry": {
72+
"type": "LineString",
73+
"coordinates": [
74+
[-70.63436881703247, 42.68738824218277],
75+
[-70.65233503703247, 42.68738824218277]
76+
]
77+
}
78+
}
79+
]
80+
}

0 commit comments

Comments
 (0)