Skip to content

Commit 05494d8

Browse files
Merge pull request #2460 from hanneshdc/issue-1232
Changed lineSplit to use bbox from geojson-rbush rather than buggy turf-square
2 parents 519d66e + 4b15924 commit 05494d8

15 files changed

+303
-34
lines changed

packages/turf-line-split/index.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { geojsonRbush as rbush } from "@turf/geojson-rbush";
2-
import { square } from "@turf/square";
3-
import { bbox } from "@turf/bbox";
42
import { truncate } from "@turf/truncate";
53
import { lineSegment } from "@turf/line-segment";
64
import { lineIntersect } from "@turf/line-intersect";
@@ -83,11 +81,6 @@ function splitLineWithPoints(line, splitter) {
8381
// First Point - doesn't need to handle any previous line results
8482
if (!results.length) {
8583
results = splitLineWithPoint(line, point).features;
86-
87-
// Add Square BBox to each feature for GeoJSON-RBush
88-
results.forEach(function (feature) {
89-
if (!feature.bbox) feature.bbox = square(bbox(feature));
90-
});
9184
tree.load(featureCollection(results));
9285
// Split with remaining points - lines might needed to be split multiple times
9386
} else {

packages/turf-line-split/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"@turf/line-segment": "workspace:*",
7575
"@turf/meta": "workspace:*",
7676
"@turf/nearest-point-on-line": "workspace:*",
77-
"@turf/square": "workspace:*",
7877
"@turf/truncate": "workspace:*",
7978
"@types/geojson": "^7946.0.10"
8079
}

packages/turf-line-split/test.ts

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ import {
99
point,
1010
lineString,
1111
multiPoint,
12+
polygon,
1213
featureCollection,
1314
round,
1415
} from "@turf/helpers";
1516
import { getCoords } from "@turf/invariant";
1617
import { lineSplit } from "./index.js";
18+
import type {
19+
Feature,
20+
FeatureCollection,
21+
LineString,
22+
MultiLineString,
23+
MultiPoint,
24+
MultiPolygon,
25+
Point,
26+
Polygon,
27+
} from "geojson";
1728

1829
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1930

@@ -26,15 +37,17 @@ let fixtures = fs.readdirSync(directories.in).map((filename) => {
2637
return {
2738
filename,
2839
name: path.parse(filename).name,
29-
geojson: loadJsonFileSync(directories.in + filename),
40+
geojson: loadJsonFileSync(directories.in + filename) as FeatureCollection,
3041
};
3142
});
32-
// fixtures = fixtures.filter(name => name === 'issue-#1075')
43+
// fixtures = fixtures.filter(({ name }) => name === "issue-#1075");
3344

3445
test("turf-line-split", (t) => {
3546
for (const { filename, name, geojson } of fixtures) {
36-
const line = geojson.features[0];
37-
const splitter = geojson.features[1];
47+
const line = geojson.features[0] as Feature<LineString>;
48+
const splitter = geojson.features[1] as Feature<
49+
Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon
50+
>;
3851
const results = colorize(lineSplit(line, splitter));
3952
featureEach(geojson, (feature) => results.features.push(feature));
4053

@@ -82,10 +95,14 @@ test("turf-line-split -- throws", (t) => {
8295
[9, 50],
8396
]);
8497

98+
// @ts-expect-error passing null for line
8599
t.throws(() => lineSplit(null, pt), "<geojson> is required");
100+
// @ts-expect-error passing null for splitter
86101
t.throws(() => lineSplit(line, null), "<geojson> is required");
102+
// @ts-expect-error passing wrong type for line
87103
t.throws(() => lineSplit(pt, pt), "<line> must be LineString");
88104
t.throws(
105+
// @ts-expect-error passing wrong type for splitter
89106
() => lineSplit(line, featureCollection([pt, line])),
90107
"<splitter> cannot be a FeatureCollection"
91108
);
@@ -200,14 +217,132 @@ test("turf-line-split -- issue #1075", (t) => {
200217
t.end();
201218
});
202219

220+
test("lineSplit - incorrect split - issue #1075", (t) => {
221+
let line, splitter, split;
222+
223+
// Example sourced from https://github.com/Turfjs/turf/issues/1232#issue-290515769
224+
line = lineString([
225+
[13.8716, 56.2783],
226+
[13.8715, 56.2785],
227+
[13.8743, 56.2794],
228+
[13.8796, 56.2746],
229+
]);
230+
231+
splitter = polygon([
232+
[
233+
[13.8726, 56.2786],
234+
[13.8716, 56.2786],
235+
[13.8713, 56.2784],
236+
[13.8726, 56.2786],
237+
],
238+
]);
239+
240+
split = lineSplit(line, splitter);
241+
t.equal(split.features.length, 3, "Line split into 3 pieces example 1");
242+
243+
// Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-361970429
244+
line = lineString([
245+
[10.424716, 50.024888],
246+
[10.417643, 50.029512],
247+
]);
248+
249+
splitter = polygon([
250+
[
251+
[10.41993839, 50.0301184],
252+
[10.42587086, 50.02630702],
253+
[10.41993839, 50.02249594],
254+
[10.41400592, 50.02630702],
255+
[10.41993839, 50.0301184],
256+
],
257+
]);
258+
259+
split = lineSplit(line, splitter);
260+
t.equal(split.features.length, 3, "Line split into 3 pieces example 2");
261+
262+
// Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-2033181347
263+
line = lineString([
264+
[-111.570323, 49.587462],
265+
[-111.570824, 49.587462],
266+
[-111.571218, 49.587212],
267+
[-111.571075, 49.587212],
268+
[-111.566432, 49.584359],
269+
]);
270+
271+
splitter = lineString([
272+
[-111.57071881384209, 49.58746705929172],
273+
[-111.57072743959235, 49.587462],
274+
[-111.57106608768505, 49.58726337153985],
275+
[-111.57109709453526, 49.587212],
276+
[-111.5711022620136, 49.58720343862349],
277+
]);
278+
279+
split = lineSplit(line, splitter);
280+
t.equal(split.features.length, 3, "Line split into 3 pieces example 3");
281+
282+
// Example sourced from https://github.com/Turfjs/turf/issues/1232#issuecomment-2231554080
283+
line = lineString([
284+
[0, 44],
285+
[25, 38],
286+
[27, 40],
287+
[0, 62],
288+
]);
289+
290+
splitter = polygon([
291+
[
292+
[0, 20],
293+
[42, 20],
294+
[42, 39],
295+
[28, 39],
296+
[0, 52],
297+
[0, 20],
298+
],
299+
]);
300+
301+
split = lineSplit(line, splitter);
302+
303+
t.equal(split.features.length, 2, "Line split into 2 pieces example 4");
304+
t.end();
305+
});
306+
307+
test("lineSplit - wavy lines - issue #2288", (t) => {
308+
// Example sourced from https://github.com/Turfjs/turf/issues/2288#issuecomment-1125555752
309+
const line = lineString([
310+
[-122.7779211637529, 38.46929673131614],
311+
[-122.78647173567292, 38.46208580491829],
312+
]);
313+
314+
const splitter = polygon([
315+
[
316+
[-122.784210824, 38.46577859000006, 0],
317+
[-122.783497375, 38.46577665500005, 0],
318+
[-122.78349775899989, 38.46574340700003, 0],
319+
[-122.78337219700002, 38.46574404700004, 0],
320+
[-122.7833724819999, 38.46570876200008, 0],
321+
[-122.783186362, 38.46571185100002, 0],
322+
[-122.78318417300001, 38.46569283300004, 0],
323+
[-122.782172859, 38.46568571000005, 0],
324+
[-122.7821622549999, 38.46587288700003, 0],
325+
[-122.783391339, 38.465877436, 0],
326+
[-122.78339171499998, 38.46589273900003, 0],
327+
[-122.78420487999992, 38.46590174800005, 0],
328+
[-122.784210824, 38.46577859000006, 0],
329+
],
330+
]);
331+
332+
const split = lineSplit(line, splitter);
333+
334+
t.equal(split.features.length, 3, "Line split into 3 pieces");
335+
t.end();
336+
});
337+
203338
/**
204339
* Colorize FeatureCollection
205340
*
206341
* @param {FeatureCollection|Feature<any>} geojson Feature or FeatureCollection
207342
* @returns {FeatureCollection<any>} colorized FeatureCollection
208343
*/
209-
function colorize(geojson) {
210-
const results = [];
344+
function colorize(geojson: FeatureCollection) {
345+
const results: Feature[] = [];
211346
featureEach(geojson, (feature, index) => {
212347
const r = index % 2 === 0 ? "F" : "0";
213348
const g = "0";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"geometry": {
7+
"type": "LineString",
8+
"coordinates": [
9+
[13.8716, 56.2783],
10+
[13.8715, 56.2785],
11+
[13.8743, 56.2794],
12+
[13.8796, 56.2746]
13+
]
14+
},
15+
"properties": {}
16+
},
17+
{
18+
"type": "Feature",
19+
"geometry": {
20+
"type": "Polygon",
21+
"coordinates": [
22+
[
23+
[13.8726, 56.2786],
24+
[13.8716, 56.2786],
25+
[13.8713, 56.2784],
26+
[13.8726, 56.2786]
27+
]
28+
]
29+
},
30+
"properties": {}
31+
}
32+
]
33+
}

packages/turf-line-split/test/out/issue-#1075-1.geojson

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[-87.168446, 37.947929]
1515
]
1616
},
17-
"bbox": [-87.1693575, 37.946093, -87.1675215, 37.947929],
17+
"bbox": [-87.168446, 37.946093, -87.168433, 37.947929],
1818
"id": 0
1919
},
2020
{
@@ -30,7 +30,7 @@
3030
[-87.16851, 37.960085]
3131
]
3232
},
33-
"bbox": [-87.174556, 37.947929, -87.16239999999999, 37.960085],
33+
"bbox": [-87.16851, 37.947929, -87.168446, 37.960085],
3434
"id": 1
3535
},
3636
{

packages/turf-line-split/test/out/issue-#1075-2.geojson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[-87.1684432, 37.9479288]
1515
]
1616
},
17-
"bbox": [-87.16935600000001, 37.946093, -87.1675202, 37.9479288],
17+
"bbox": [-87.1684432, 37.946093, -87.168433, 37.9479288],
1818
"id": 0
1919
},
2020
{

packages/turf-line-split/test/out/issue-#1075-3.geojson

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[-87.1684432, 37.9479288]
1515
]
1616
},
17-
"bbox": [-87.16935600000001, 37.946093, -87.1675202, 37.9479288],
17+
"bbox": [-87.1684432, 37.946093, -87.168433, 37.9479288],
1818
"id": 0
1919
},
2020
{
@@ -30,7 +30,7 @@
3030
[-87.16851, 37.960085]
3131
]
3232
},
33-
"bbox": [-87.17455469999999, 37.9479288, -87.1623985, 37.960085],
33+
"bbox": [-87.16851, 37.9479288, -87.1684432, 37.960085],
3434
"id": 1
3535
},
3636
{
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"properties": {
7+
"stroke": "#F00",
8+
"stroke-width": 10
9+
},
10+
"geometry": {
11+
"type": "LineString",
12+
"coordinates": [
13+
[13.8716, 56.2783],
14+
[13.871532142857145, 56.27843571428571]
15+
]
16+
},
17+
"bbox": [13.871532142857145, 56.2783, 13.8716, 56.27843571428571],
18+
"id": 0
19+
},
20+
{
21+
"type": "Feature",
22+
"properties": {
23+
"stroke": "#00F",
24+
"stroke-width": 10
25+
},
26+
"geometry": {
27+
"type": "LineString",
28+
"coordinates": [
29+
[13.871532142857145, 56.27843571428571],
30+
[13.8715, 56.2785],
31+
[13.871811111111098, 56.2786]
32+
]
33+
},
34+
"bbox": [13.8715, 56.27843571428571, 13.871811111111098, 56.2786]
35+
},
36+
{
37+
"type": "Feature",
38+
"properties": {
39+
"stroke": "#F00",
40+
"stroke-width": 10
41+
},
42+
"geometry": {
43+
"type": "LineString",
44+
"coordinates": [
45+
[13.871811111111098, 56.2786],
46+
[13.8743, 56.2794],
47+
[13.8796, 56.2746]
48+
]
49+
},
50+
"bbox": [13.871811111111098, 56.2746, 13.8796, 56.2794]
51+
},
52+
{
53+
"type": "Feature",
54+
"geometry": {
55+
"type": "LineString",
56+
"coordinates": [
57+
[13.8716, 56.2783],
58+
[13.8715, 56.2785],
59+
[13.8743, 56.2794],
60+
[13.8796, 56.2746]
61+
]
62+
},
63+
"properties": {}
64+
},
65+
{
66+
"type": "Feature",
67+
"geometry": {
68+
"type": "Polygon",
69+
"coordinates": [
70+
[
71+
[13.8726, 56.2786],
72+
[13.8716, 56.2786],
73+
[13.8713, 56.2784],
74+
[13.8726, 56.2786]
75+
]
76+
]
77+
},
78+
"properties": {}
79+
}
80+
]
81+
}

packages/turf-line-split/test/out/linestrings.geojson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[118.6090049242457, -27.06938170764152]
1515
]
1616
},
17-
"bbox": [118.6090049242457, -34.00655617152344, 126.5625, -26.05306109576914],
17+
"bbox": [118.6090049242457, -32.99023555965107, 126.5625, -27.06938170764152],
1818
"id": 0
1919
},
2020
{

0 commit comments

Comments
 (0)