Skip to content

Commit 7df1a57

Browse files
Traksewtmfedderly
andauthored
fix(polygonize): deduplicate segments in planar graph (#3045)
* fix(polygonize): ignore duplicate segments in planar graph * chore: add contributor to package.json --------- Co-authored-by: mfedderly <24275386+mfedderly@users.noreply.github.com>
1 parent 9622dbd commit 7df1a57

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

packages/turf-polygonize/lib/Graph.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ class Graph {
112112
* @param {Node} to - Node which ends the Edge
113113
*/
114114
addEdge(from: Node, to: Node) {
115+
// Note: Avoid creating duplicate Edges (#3045). This is done before the Edge constructor call, which modifies the Nodes that are passed in.
116+
if (this.edges.get(from)?.has(to)) {
117+
return;
118+
}
119+
115120
const edge = new Edge(from, to),
116121
symetricEdge = edge.getSymetric();
117122

packages/turf-polygonize/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"author": "Turf Authors",
66
"contributors": [
77
"Nicolas Cisco <@nickcis>",
8-
"Denis Carriere <@DenisCarriere>"
8+
"Denis Carriere <@DenisCarriere>",
9+
"Kenny Sabir <@Traksewt>"
910
],
1011
"license": "MIT",
1112
"bugs": {

packages/turf-polygonize/test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ test("turf-polygonize -- input mutation", (t) => {
8181
t.end();
8282
});
8383

84+
test("turf-polygonize -- duplicate segment", (t) => {
85+
const lines = featureCollection([
86+
lineString([
87+
[0, 0],
88+
[1, 0],
89+
]),
90+
lineString([
91+
[1, 0],
92+
[1, 1],
93+
]),
94+
lineString([
95+
[1, 1],
96+
[0, 1],
97+
]),
98+
lineString([
99+
[0, 1],
100+
[0, 0],
101+
]),
102+
// Duplicate boundary segment should not prevent polygonization.
103+
lineString([
104+
[0, 0],
105+
[1, 0],
106+
]),
107+
]);
108+
109+
const polygonized = polygonize(lines);
110+
t.equal(
111+
polygonized.features.length,
112+
1,
113+
"returns one polygon for closed ring with duplicate edge"
114+
);
115+
t.end();
116+
});
117+
84118
function colorize(feature, color = "#F00", width = 6) {
85119
feature.properties["fill"] = color;
86120
feature.properties["fill-opacity"] = 0.3;

0 commit comments

Comments
 (0)