Skip to content

Commit

Permalink
Replaced polygon-clipping with polyclip-ts (#2729)
Browse files Browse the repository at this point in the history
* Replaced polygon-clipping with polyclip-ts

---------

Co-authored-by: colin <[email protected]>
Co-authored-by: James Beard <[email protected]>
  • Loading branch information
3 people authored Dec 22, 2024
1 parent 8f5c407 commit 3bbd759
Show file tree
Hide file tree
Showing 36 changed files with 70,293 additions and 375 deletions.
1 change: 0 additions & 1 deletion packages/turf-boolean-disjoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ function booleanDisjoint(
function disjoint(geom1: any, geom2: any, ignoreSelfIntersections: boolean) {
switch (geom1.type) {
case "Point":
/* eslint-disable @typescript-eslint/no-unused-vars */
switch (geom2.type) {
case "Point":
return !compareCoords(geom1.coordinates, geom2.coordinates);
Expand Down
8 changes: 4 additions & 4 deletions packages/turf-difference/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Polygon, MultiPolygon, Feature, FeatureCollection } from "geojson";
import polygonClipping, { Geom } from "polygon-clipping";
import * as polyclip from "polyclip-ts";
import { polygon, multiPolygon } from "@turf/helpers";
import { geomEach } from "@turf/meta";

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

geomEach(features, (geom) => {
geoms.push(geom.coordinates as Geom);
geoms.push(geom.coordinates as polyclip.Geom);
});

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

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

const differenced = polygonClipping.difference(geoms[0], ...geoms.slice(1));
const differenced = polyclip.difference(geoms[0], ...geoms.slice(1));
if (differenced.length === 0) return null;
if (differenced.length === 1) return polygon(differenced[0], properties);
return multiPolygon(differenced, properties);
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-difference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@turf/helpers": "workspace:^",
"@turf/meta": "workspace:^",
"@types/geojson": "^7946.0.10",
"polygon-clipping": "^0.15.3",
"polyclip-ts": "^0.16.8",
"tslib": "^2.8.1"
}
}
240 changes: 240 additions & 0 deletions packages/turf-difference/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,243 @@ test("turf-difference - complete overlap", (t) => {
t.deepEqual(result, null, "difference should be null");
t.end();
});

test("difference - unable to complete output ring - issue 2409", (t) => {
// Test examples copied from https://github.com/Turfjs/turf/issues/2409
const area1 = polygon([
[
[11.79926, 59.307999],
[18.80383, 60.15596],
[18.73765340635914, 60.23951348693759],
[18.58133, 60.34301],
[11.79926, 59.307999],
],
]);

const area1_subtract = polygon([
[
[11.79926, 59.307999],
[18.80383, 60.15596],
[
18.73765340635913, // Subtract 1 to final digit
60.23951348693759,
],
[18.58133, 60.34301],
[11.79926, 59.307999],
],
]);

const area1_add = polygon([
[
[11.79926, 59.307999],
[18.80383, 60.15596],
[
18.73765340635915, // Add 1 to final digit
60.23951348693759,
],
[18.58133, 60.34301],
[11.79926, 59.307999],
],
]);

const area2 = polygon([
[
[18.35554, 60.35768],
[18.58133, 60.34301],
[18.75959, 60.22499],
[18.80383, 60.15596],
[18.35554, 60.35768],
],
]);

t.doesNotThrow(
() => difference(featureCollection([area1, area2])),
"[area1, area2] should not throw"
);
t.doesNotThrow(
() => difference(featureCollection([area1_subtract, area2])),
"[area1_subtract, area2] should not throw"
);
t.doesNotThrow(
() => difference(featureCollection([area1_add, area2])),
"[area1_add, area2] should not throw"
);

t.end();
});

test("difference - unable to complete output ring - issue 2277", (t) => {
// Test example copied from https://github.com/Turfjs/turf/issues/2277
const poly1 = polygon([
[
[54.56658645236534, 24.445194105819738],
[54.56658654953498, 24.441605817571325],
[54.57000000000001, 24.43981171174874],
[54.57341345046501, 24.441605817571325],
[54.573413547634665, 24.445194105819738],
[54.57000000000001, 24.44698828825126],
[54.56658645236534, 24.445194105819738],
],
[
[54.56795530519258, 24.44447467409078],
[54.57000000000001, 24.4455493756693],
[54.57204469480743, 24.44447467409078],
[54.57204465994316, 24.442325298422087],
[54.57000000000001, 24.441250624330703],
[54.56795534005685, 24.442325298422087],
[54.56795530519258, 24.44447467409078],
],
]);

const poly2 = polygon([
[
[54.569778932416476, 24.441366817541834],
[54.56977894449294, 24.441074136738756],
[54.57000000000001, 24.441190327160086],
[54.57084694057397, 24.440745161222193],
[54.57084693745136, 24.44028034081218],
[54.571147760242575, 24.44043845608456],
[54.57114771720956, 24.441853864959285],
[54.57080496898934, 24.4416737163564],
[54.57080502276297, 24.441026402022757],
[54.57074511559248, 24.441057889217532],
[54.57074509421786, 24.441642246152345],
[54.57000000000001, 24.441250624330703],
[54.569778932416476, 24.441366817541834],
],
]);

t.doesNotThrow(
() => difference(featureCollection([poly1, poly2])),
"[poly1, poly2] should not throw"
);

t.end();
});

test("difference - maximum call stack size exceeded - issue 2479", (t) => {
// Test example copied from https://github.com/Turfjs/turf/issues/2479
const poly1 = polygon([
[
[49.93317115095019, 20.170898437500004],
[49.93927561914192, 20.16278743743897],
[49.941126142253154, 20.165448188781742],
[49.934096538617936, 20.174009799957275],
[49.93317115095019, 20.170898437500004],
],
]);

const poly2 = polygon([
[
[49.93317115095019, 20.170898437500004],
[49.933680058500165, 20.170222252607346],
[49.933758735535065, 20.170222252607346],
[49.933803934349285, 20.170152112841606],
[49.933803934349285, 20.170057658905122],
[49.934306839656294, 20.169389449185992],
[49.93434632011994, 20.16945071518421],
[49.93434632011994, 20.16959099471569],
[49.93448191656261, 20.16980141401291],
[49.93457231419105, 20.16980141401291],
[49.93466271181949, 20.16966113448143],
[49.93484350707638, 20.16966113448143],
[49.93520509759015, 20.169100016355515],
[49.93529549521859, 20.169100016355515],
[49.935928278617695, 20.168118059635166],
[49.936018676246135, 20.168118059635166],
[49.93660626083101, 20.167206242680553],
[49.93660626083101, 20.167065963149074],
[49.9367870560879, 20.166785404086117],
[49.9367870560879, 20.166645124554638],
[49.93714864660167, 20.166084006428722],
[49.93714864660167, 20.165613543475054],
[49.93742361679489, 20.16524819088677],
[49.93755543592966, 20.165452748537067],
[49.938504611028314, 20.16397981345654],
[49.938504611028314, 20.163811875057462],
[49.93927561914192, 20.16278743743897],
[49.941126142253154, 20.165448188781742],
[49.93988364085967, 20.16696147663808],
[49.93981537664073, 20.166855543851856],
[49.93958938256963, 20.167206242680553],
[49.93958938256963, 20.167319864563666],
[49.934096538617936, 20.174009799957275],
[49.93317115095019, 20.170898437500004],
],
]);

t.doesNotThrow(
() => difference(featureCollection([poly1, poly2])),
"[poly1, poly2] should not throw"
);

t.end();
});

test("difference - unable to find segment - issue 2306", (t) => {
// Test example copied from https://github.com/Turfjs/turf/issues/2306
const poly1 = polygon([
[
[10.299138347373786, 48.460352133145804],
[10.299142854373908, 48.46034913552448],
[10.299142854373908, 48.460339214732976],
[10.299225267807545, 48.46033958029495],
[10.29927355737747, 48.46034017606536],
[10.29927355737747, 48.46034613790316],
[10.299266796877285, 48.46035363195647],
[10.299257782877039, 48.46035363195647],
[10.299251022376856, 48.46035812838846],
[10.299251022376856, 48.46036507625937],
[10.299195961284822, 48.4603647734974],
[10.299201445375504, 48.46036112600978],
[10.29919468487532, 48.460356629577795],
[10.299187924375136, 48.46035812838846],
[10.299187924375136, 48.46036112600978],
[10.299193387184792, 48.460364759343314],
[10.299138347373786, 48.460364456698365],
[10.299138347373786, 48.460352133145804],
],
[
[10.299142854373908, 48.46035812838846],
[10.299145107873969, 48.46035962719912],
[10.2991608823744, 48.46035812838846],
[10.299158628874338, 48.46035363195647],
[10.299149614874093, 48.46035363195647],
[10.299142854373908, 48.46035812838846],
],
]);

const poly2 = polygon([
[
[10.299138347373786, 48.46036049139952],
[10.299187924375136, 48.46036028454162],
[10.299187924375136, 48.46036112600978],
[10.29919468487532, 48.46036562244176],
[10.299201445375504, 48.46036112600978],
[10.299200103800315, 48.46036023372349],
[10.299208777179729, 48.46036019753419],
[10.299209806197382, 48.46031596598246],
[10.299250947482829, 48.460316111877646],
[10.299251022376856, 48.460316161689924],
[10.299251022376856, 48.46031915931125],
[10.299257677970186, 48.46032358596932],
[10.299256707063519, 48.46035434748375],
[10.299251022376856, 48.46035812838846],
[10.299251022376856, 48.4603761141164],
[10.299246515376732, 48.46037911173772],
[10.299246515376732, 48.4603872476338],
[10.29919425093356, 48.460374326689575],
[10.299190177875197, 48.460371617684416],
[10.299188312254989, 48.46037285851485],
[10.299138347373786, 48.460360506074],
[10.299138347373786, 48.46036049139952],
],
]);

t.doesNotThrow(
() => difference(featureCollection([poly1, poly2])),
"[poly1, poly2] should not throw"
);

t.end();
});
36 changes: 36 additions & 0 deletions packages/turf-difference/test/out/issue-#721-inverse.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@
]
]
}
},
{
"type": "Feature",
"properties": {
"fill-opacity": 1,
"fill": "#0F0"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-0.6462588068806041, 44.80608667910215],
[-0.6462588068796641, 44.80608667910137],
[-0.6461987, 44.8060367],
[-0.6462588068806041, 44.80608667910215]
]
],
[
[
[-0.6461042, 44.8059451],
[-0.6461041344154723, 44.805944903377586],
[-0.6461041344154722, 44.805944903377586],
[-0.6461042, 44.8059451]
]
],
[
[
[-0.6460471709105139, 44.80564743856641],
[-0.6460421, 44.8056729],
[-0.6460471709104708, 44.80564743856663],
[-0.6460471709105139, 44.80564743856641]
]
]
]
}
}
]
}
18 changes: 12 additions & 6 deletions packages/turf-difference/test/out/issue-#721.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,16 @@
[-0.6460168, 44.8047948],
[-0.6460827, 44.8050017],
[-0.6461218, 44.8051741],
[-0.6461158838109077, 44.80524109574131],
[-0.646107941865921, 44.80533857879061],
[-0.646093673696575, 44.80541109474097],
[-0.6461073, 44.8053383],
[-0.6460848, 44.8054585],
[-0.6460471709105139, 44.80564743856641],
[-0.6460471709104708, 44.80564743856663],
[-0.6460422277450562, 44.805672561504906],
[-0.6460542976856232, 44.80579530680288],
[-0.646069093721612, 44.80583985137739],
[-0.6461041344154722, 44.805944903377586],
[-0.6460542, 44.8057952],
[-0.6461041344154723, 44.805944903377586],
[-0.6461991369724274, 44.80603699057963],
[-0.6462588068806041, 44.80608667910215],
[-0.6462588068796641, 44.80608667910137],
[-0.6463809, 44.8061882],
[-0.646437, 44.806159],
[-0.64649, 44.80611],
Expand All @@ -138,6 +137,13 @@
[-0.6465049, 44.8055554],
[-0.6471907, 44.8053946],
[-0.6474134, 44.805326]
],
[
[-0.6461073, 44.8053383],
[-0.6461158838109077, 44.80524109574131],
[-0.646107941865921, 44.80533857879061],
[-0.646093673696575, 44.80541109474097],
[-0.6461073, 44.8053383]
]
]
}
Expand Down
Loading

0 comments on commit 3bbd759

Please sign in to comment.