Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/turf-polygonize/lib/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function validateGeoJson(geoJson: AllGeoJSON) {
class Graph {
private nodes: { [id: string]: Node };
private edges: Edge[];
private edgeIds: { [id: string]: true };

/**
* Creates a graph from a GeoJSON.
Expand Down Expand Up @@ -102,15 +103,21 @@ class Graph {
* @param {Node} to - Node which ends the Edge
*/
addEdge(from: Node, to: Node) {
const edge = new Edge(from, to),
symetricEdge = edge.getSymetric();
const edgeId = `${from.id}->${to.id}`;
if (this.edgeIds[edgeId]) return;

const edge = new Edge(from, to);
const symetricEdge = edge.getSymetric();

this.edgeIds[`${edge.from.id}->${edge.to.id}`] = true;
this.edgeIds[`${symetricEdge.from.id}->${symetricEdge.to.id}`] = true;
this.edges.push(edge);
this.edges.push(symetricEdge);
}

constructor() {
this.edges = []; //< {Edge[]} dirEdges
this.edgeIds = {};

// The key is the `id` of the Node (ie: coordinates.join(','))
this.nodes = {};
Expand Down Expand Up @@ -348,6 +355,7 @@ class Graph {
removeEdge(edge: Edge) {
this.edges = this.edges.filter((e) => !e.isEqual(edge));
edge.deleteEdge();
delete this.edgeIds[`${edge.from.id}->${edge.to.id}`];
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/turf-polygonize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"author": "Turf Authors",
"contributors": [
"Nicolas Cisco <@nickcis>",
"Denis Carriere <@DenisCarriere>"
"Denis Carriere <@DenisCarriere>",
"Kenny Sabir <@Traksewt>"
],
"license": "MIT",
"bugs": {
Expand Down
34 changes: 34 additions & 0 deletions packages/turf-polygonize/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ test("turf-polygonize -- input mutation", (t) => {
t.end();
});

test("turf-polygonize -- duplicate segment", (t) => {
const lines = featureCollection([
lineString([
[0, 0],
[1, 0],
]),
lineString([
[1, 0],
[1, 1],
]),
lineString([
[1, 1],
[0, 1],
]),
lineString([
[0, 1],
[0, 0],
]),
// Duplicate boundary segment should not prevent polygonization.
lineString([
[0, 0],
[1, 0],
]),
]);

const polygonized = polygonize(lines);
t.equal(
polygonized.features.length,
1,
"returns one polygon for closed ring with duplicate edge"
);
t.end();
});

function colorize(feature, color = "#F00", width = 6) {
feature.properties["fill"] = color;
feature.properties["fill-opacity"] = 0.3;
Expand Down