Skip to content

Commit 3bbd759

Browse files
cmurphy23cmurphy-at-terabase-energysmallsaucepan
authored
Replaced polygon-clipping with polyclip-ts (#2729)
* Replaced polygon-clipping with polyclip-ts --------- Co-authored-by: colin <[email protected]> Co-authored-by: James Beard <[email protected]>
1 parent 8f5c407 commit 3bbd759

36 files changed

+70293
-375
lines changed

packages/turf-boolean-disjoint/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function booleanDisjoint(
6565
function disjoint(geom1: any, geom2: any, ignoreSelfIntersections: boolean) {
6666
switch (geom1.type) {
6767
case "Point":
68-
/* eslint-disable @typescript-eslint/no-unused-vars */
6968
switch (geom2.type) {
7069
case "Point":
7170
return !compareCoords(geom1.coordinates, geom2.coordinates);

packages/turf-difference/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
2-
import polygonClipping, { Geom } from "polygon-clipping";
2+
import * as polyclip from "polyclip-ts";
33
import { polygon, multiPolygon } from "@turf/helpers";
44
import { geomEach } from "@turf/meta";
55

@@ -39,10 +39,10 @@ import { geomEach } from "@turf/meta";
3939
function difference(
4040
features: FeatureCollection<Polygon | MultiPolygon>
4141
): Feature<Polygon | MultiPolygon> | null {
42-
const geoms: Array<Geom> = [];
42+
const geoms: Array<polyclip.Geom> = [];
4343

4444
geomEach(features, (geom) => {
45-
geoms.push(geom.coordinates as Geom);
45+
geoms.push(geom.coordinates as polyclip.Geom);
4646
});
4747

4848
if (geoms.length < 2) {
@@ -51,7 +51,7 @@ function difference(
5151

5252
const properties = features.features[0].properties || {};
5353

54-
const differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
54+
const differenced = polyclip.difference(geoms[0], ...geoms.slice(1));
5555
if (differenced.length === 0) return null;
5656
if (differenced.length === 1) return polygon(differenced[0], properties);
5757
return multiPolygon(differenced, properties);

packages/turf-difference/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"@turf/helpers": "workspace:^",
6666
"@turf/meta": "workspace:^",
6767
"@types/geojson": "^7946.0.10",
68-
"polygon-clipping": "^0.15.3",
68+
"polyclip-ts": "^0.16.8",
6969
"tslib": "^2.8.1"
7070
}
7171
}

packages/turf-difference/test.ts

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,243 @@ test("turf-difference - complete overlap", (t) => {
117117
t.deepEqual(result, null, "difference should be null");
118118
t.end();
119119
});
120+
121+
test("difference - unable to complete output ring - issue 2409", (t) => {
122+
// Test examples copied from https://github.com/Turfjs/turf/issues/2409
123+
const area1 = polygon([
124+
[
125+
[11.79926, 59.307999],
126+
[18.80383, 60.15596],
127+
[18.73765340635914, 60.23951348693759],
128+
[18.58133, 60.34301],
129+
[11.79926, 59.307999],
130+
],
131+
]);
132+
133+
const area1_subtract = polygon([
134+
[
135+
[11.79926, 59.307999],
136+
[18.80383, 60.15596],
137+
[
138+
18.73765340635913, // Subtract 1 to final digit
139+
60.23951348693759,
140+
],
141+
[18.58133, 60.34301],
142+
[11.79926, 59.307999],
143+
],
144+
]);
145+
146+
const area1_add = polygon([
147+
[
148+
[11.79926, 59.307999],
149+
[18.80383, 60.15596],
150+
[
151+
18.73765340635915, // Add 1 to final digit
152+
60.23951348693759,
153+
],
154+
[18.58133, 60.34301],
155+
[11.79926, 59.307999],
156+
],
157+
]);
158+
159+
const area2 = polygon([
160+
[
161+
[18.35554, 60.35768],
162+
[18.58133, 60.34301],
163+
[18.75959, 60.22499],
164+
[18.80383, 60.15596],
165+
[18.35554, 60.35768],
166+
],
167+
]);
168+
169+
t.doesNotThrow(
170+
() => difference(featureCollection([area1, area2])),
171+
"[area1, area2] should not throw"
172+
);
173+
t.doesNotThrow(
174+
() => difference(featureCollection([area1_subtract, area2])),
175+
"[area1_subtract, area2] should not throw"
176+
);
177+
t.doesNotThrow(
178+
() => difference(featureCollection([area1_add, area2])),
179+
"[area1_add, area2] should not throw"
180+
);
181+
182+
t.end();
183+
});
184+
185+
test("difference - unable to complete output ring - issue 2277", (t) => {
186+
// Test example copied from https://github.com/Turfjs/turf/issues/2277
187+
const poly1 = polygon([
188+
[
189+
[54.56658645236534, 24.445194105819738],
190+
[54.56658654953498, 24.441605817571325],
191+
[54.57000000000001, 24.43981171174874],
192+
[54.57341345046501, 24.441605817571325],
193+
[54.573413547634665, 24.445194105819738],
194+
[54.57000000000001, 24.44698828825126],
195+
[54.56658645236534, 24.445194105819738],
196+
],
197+
[
198+
[54.56795530519258, 24.44447467409078],
199+
[54.57000000000001, 24.4455493756693],
200+
[54.57204469480743, 24.44447467409078],
201+
[54.57204465994316, 24.442325298422087],
202+
[54.57000000000001, 24.441250624330703],
203+
[54.56795534005685, 24.442325298422087],
204+
[54.56795530519258, 24.44447467409078],
205+
],
206+
]);
207+
208+
const poly2 = polygon([
209+
[
210+
[54.569778932416476, 24.441366817541834],
211+
[54.56977894449294, 24.441074136738756],
212+
[54.57000000000001, 24.441190327160086],
213+
[54.57084694057397, 24.440745161222193],
214+
[54.57084693745136, 24.44028034081218],
215+
[54.571147760242575, 24.44043845608456],
216+
[54.57114771720956, 24.441853864959285],
217+
[54.57080496898934, 24.4416737163564],
218+
[54.57080502276297, 24.441026402022757],
219+
[54.57074511559248, 24.441057889217532],
220+
[54.57074509421786, 24.441642246152345],
221+
[54.57000000000001, 24.441250624330703],
222+
[54.569778932416476, 24.441366817541834],
223+
],
224+
]);
225+
226+
t.doesNotThrow(
227+
() => difference(featureCollection([poly1, poly2])),
228+
"[poly1, poly2] should not throw"
229+
);
230+
231+
t.end();
232+
});
233+
234+
test("difference - maximum call stack size exceeded - issue 2479", (t) => {
235+
// Test example copied from https://github.com/Turfjs/turf/issues/2479
236+
const poly1 = polygon([
237+
[
238+
[49.93317115095019, 20.170898437500004],
239+
[49.93927561914192, 20.16278743743897],
240+
[49.941126142253154, 20.165448188781742],
241+
[49.934096538617936, 20.174009799957275],
242+
[49.93317115095019, 20.170898437500004],
243+
],
244+
]);
245+
246+
const poly2 = polygon([
247+
[
248+
[49.93317115095019, 20.170898437500004],
249+
[49.933680058500165, 20.170222252607346],
250+
[49.933758735535065, 20.170222252607346],
251+
[49.933803934349285, 20.170152112841606],
252+
[49.933803934349285, 20.170057658905122],
253+
[49.934306839656294, 20.169389449185992],
254+
[49.93434632011994, 20.16945071518421],
255+
[49.93434632011994, 20.16959099471569],
256+
[49.93448191656261, 20.16980141401291],
257+
[49.93457231419105, 20.16980141401291],
258+
[49.93466271181949, 20.16966113448143],
259+
[49.93484350707638, 20.16966113448143],
260+
[49.93520509759015, 20.169100016355515],
261+
[49.93529549521859, 20.169100016355515],
262+
[49.935928278617695, 20.168118059635166],
263+
[49.936018676246135, 20.168118059635166],
264+
[49.93660626083101, 20.167206242680553],
265+
[49.93660626083101, 20.167065963149074],
266+
[49.9367870560879, 20.166785404086117],
267+
[49.9367870560879, 20.166645124554638],
268+
[49.93714864660167, 20.166084006428722],
269+
[49.93714864660167, 20.165613543475054],
270+
[49.93742361679489, 20.16524819088677],
271+
[49.93755543592966, 20.165452748537067],
272+
[49.938504611028314, 20.16397981345654],
273+
[49.938504611028314, 20.163811875057462],
274+
[49.93927561914192, 20.16278743743897],
275+
[49.941126142253154, 20.165448188781742],
276+
[49.93988364085967, 20.16696147663808],
277+
[49.93981537664073, 20.166855543851856],
278+
[49.93958938256963, 20.167206242680553],
279+
[49.93958938256963, 20.167319864563666],
280+
[49.934096538617936, 20.174009799957275],
281+
[49.93317115095019, 20.170898437500004],
282+
],
283+
]);
284+
285+
t.doesNotThrow(
286+
() => difference(featureCollection([poly1, poly2])),
287+
"[poly1, poly2] should not throw"
288+
);
289+
290+
t.end();
291+
});
292+
293+
test("difference - unable to find segment - issue 2306", (t) => {
294+
// Test example copied from https://github.com/Turfjs/turf/issues/2306
295+
const poly1 = polygon([
296+
[
297+
[10.299138347373786, 48.460352133145804],
298+
[10.299142854373908, 48.46034913552448],
299+
[10.299142854373908, 48.460339214732976],
300+
[10.299225267807545, 48.46033958029495],
301+
[10.29927355737747, 48.46034017606536],
302+
[10.29927355737747, 48.46034613790316],
303+
[10.299266796877285, 48.46035363195647],
304+
[10.299257782877039, 48.46035363195647],
305+
[10.299251022376856, 48.46035812838846],
306+
[10.299251022376856, 48.46036507625937],
307+
[10.299195961284822, 48.4603647734974],
308+
[10.299201445375504, 48.46036112600978],
309+
[10.29919468487532, 48.460356629577795],
310+
[10.299187924375136, 48.46035812838846],
311+
[10.299187924375136, 48.46036112600978],
312+
[10.299193387184792, 48.460364759343314],
313+
[10.299138347373786, 48.460364456698365],
314+
[10.299138347373786, 48.460352133145804],
315+
],
316+
[
317+
[10.299142854373908, 48.46035812838846],
318+
[10.299145107873969, 48.46035962719912],
319+
[10.2991608823744, 48.46035812838846],
320+
[10.299158628874338, 48.46035363195647],
321+
[10.299149614874093, 48.46035363195647],
322+
[10.299142854373908, 48.46035812838846],
323+
],
324+
]);
325+
326+
const poly2 = polygon([
327+
[
328+
[10.299138347373786, 48.46036049139952],
329+
[10.299187924375136, 48.46036028454162],
330+
[10.299187924375136, 48.46036112600978],
331+
[10.29919468487532, 48.46036562244176],
332+
[10.299201445375504, 48.46036112600978],
333+
[10.299200103800315, 48.46036023372349],
334+
[10.299208777179729, 48.46036019753419],
335+
[10.299209806197382, 48.46031596598246],
336+
[10.299250947482829, 48.460316111877646],
337+
[10.299251022376856, 48.460316161689924],
338+
[10.299251022376856, 48.46031915931125],
339+
[10.299257677970186, 48.46032358596932],
340+
[10.299256707063519, 48.46035434748375],
341+
[10.299251022376856, 48.46035812838846],
342+
[10.299251022376856, 48.4603761141164],
343+
[10.299246515376732, 48.46037911173772],
344+
[10.299246515376732, 48.4603872476338],
345+
[10.29919425093356, 48.460374326689575],
346+
[10.299190177875197, 48.460371617684416],
347+
[10.299188312254989, 48.46037285851485],
348+
[10.299138347373786, 48.460360506074],
349+
[10.299138347373786, 48.46036049139952],
350+
],
351+
]);
352+
353+
t.doesNotThrow(
354+
() => difference(featureCollection([poly1, poly2])),
355+
"[poly1, poly2] should not throw"
356+
);
357+
358+
t.end();
359+
});

packages/turf-difference/test/out/issue-#721-inverse.geojson

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@
9292
]
9393
]
9494
}
95+
},
96+
{
97+
"type": "Feature",
98+
"properties": {
99+
"fill-opacity": 1,
100+
"fill": "#0F0"
101+
},
102+
"geometry": {
103+
"type": "MultiPolygon",
104+
"coordinates": [
105+
[
106+
[
107+
[-0.6462588068806041, 44.80608667910215],
108+
[-0.6462588068796641, 44.80608667910137],
109+
[-0.6461987, 44.8060367],
110+
[-0.6462588068806041, 44.80608667910215]
111+
]
112+
],
113+
[
114+
[
115+
[-0.6461042, 44.8059451],
116+
[-0.6461041344154723, 44.805944903377586],
117+
[-0.6461041344154722, 44.805944903377586],
118+
[-0.6461042, 44.8059451]
119+
]
120+
],
121+
[
122+
[
123+
[-0.6460471709105139, 44.80564743856641],
124+
[-0.6460421, 44.8056729],
125+
[-0.6460471709104708, 44.80564743856663],
126+
[-0.6460471709105139, 44.80564743856641]
127+
]
128+
]
129+
]
130+
}
95131
}
96132
]
97133
}

packages/turf-difference/test/out/issue-#721.geojson

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,16 @@
113113
[-0.6460168, 44.8047948],
114114
[-0.6460827, 44.8050017],
115115
[-0.6461218, 44.8051741],
116-
[-0.6461158838109077, 44.80524109574131],
117-
[-0.646107941865921, 44.80533857879061],
118-
[-0.646093673696575, 44.80541109474097],
116+
[-0.6461073, 44.8053383],
119117
[-0.6460848, 44.8054585],
120-
[-0.6460471709105139, 44.80564743856641],
118+
[-0.6460471709104708, 44.80564743856663],
121119
[-0.6460422277450562, 44.805672561504906],
122120
[-0.6460542976856232, 44.80579530680288],
123121
[-0.646069093721612, 44.80583985137739],
124-
[-0.6461041344154722, 44.805944903377586],
122+
[-0.6460542, 44.8057952],
123+
[-0.6461041344154723, 44.805944903377586],
125124
[-0.6461991369724274, 44.80603699057963],
126-
[-0.6462588068806041, 44.80608667910215],
125+
[-0.6462588068796641, 44.80608667910137],
127126
[-0.6463809, 44.8061882],
128127
[-0.646437, 44.806159],
129128
[-0.64649, 44.80611],
@@ -138,6 +137,13 @@
138137
[-0.6465049, 44.8055554],
139138
[-0.6471907, 44.8053946],
140139
[-0.6474134, 44.805326]
140+
],
141+
[
142+
[-0.6461073, 44.8053383],
143+
[-0.6461158838109077, 44.80524109574131],
144+
[-0.646107941865921, 44.80533857879061],
145+
[-0.646093673696575, 44.80541109474097],
146+
[-0.6461073, 44.8053383]
141147
]
142148
]
143149
}

0 commit comments

Comments
 (0)