@turf/polygonize 7.0.0-alpha.2 → 7.1.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,26 +44,21 @@ Returns **[FeatureCollection][3]<[Polygon][9]>** Polygons created
44
44
 
45
45
  [9]: https://tools.ietf.org/html/rfc7946#section-3.1.6
46
46
 
47
- <!-- This file is automatically generated. Please don't edit it directly:
48
- if you find an error, edit the source file (likely index.js), and re-run
49
- ./scripts/generate-readmes in the turf project. -->
47
+ <!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
50
48
 
51
49
  ---
52
50
 
53
- This module is part of the [Turfjs project](http://turfjs.org/), an open source
54
- module collection dedicated to geographic algorithms. It is maintained in the
55
- [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
56
- PRs and issues.
51
+ This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
57
52
 
58
53
  ### Installation
59
54
 
60
- Install this module individually:
55
+ Install this single module individually:
61
56
 
62
57
  ```sh
63
58
  $ npm install @turf/polygonize
64
59
  ```
65
60
 
66
- Or install the Turf module that includes it as a function:
61
+ Or install the all-encompassing @turf/turf module that includes all modules as functions:
67
62
 
68
63
  ```sh
69
64
  $ npm install @turf/turf
@@ -0,0 +1,656 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
2
+ var _helpers = require('@turf/helpers');
3
+
4
+ // lib/util.ts
5
+ var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
6
+
7
+ function mathSign(x) {
8
+ return (x > 0) - (x < 0) || +x;
9
+ }
10
+ function orientationIndex(p1, p2, q) {
11
+ const dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1], dx2 = q[0] - p2[0], dy2 = q[1] - p2[1];
12
+ return mathSign(dx1 * dy2 - dx2 * dy1);
13
+ }
14
+ function envelopeIsEqual(env1, env2) {
15
+ const envX1 = env1.geometry.coordinates[0].map((c) => c[0]), envY1 = env1.geometry.coordinates[0].map((c) => c[1]), envX2 = env2.geometry.coordinates[0].map((c) => c[0]), envY2 = env2.geometry.coordinates[0].map((c) => c[1]);
16
+ return Math.max.apply(null, envX1) === Math.max.apply(null, envX2) && Math.max.apply(null, envY1) === Math.max.apply(null, envY2) && Math.min.apply(null, envX1) === Math.min.apply(null, envX2) && Math.min.apply(null, envY1) === Math.min.apply(null, envY2);
17
+ }
18
+ function envelopeContains(self, env) {
19
+ return env.geometry.coordinates[0].every(
20
+ (c) => _booleanpointinpolygon.booleanPointInPolygon.call(void 0, _helpers.point.call(void 0, c), self)
21
+ );
22
+ }
23
+ function coordinatesEqual(coord1, coord2) {
24
+ return coord1[0] === coord2[0] && coord1[1] === coord2[1];
25
+ }
26
+
27
+ // lib/Node.ts
28
+ var Node = class _Node {
29
+ static buildId(coordinates) {
30
+ return coordinates.join(",");
31
+ }
32
+ constructor(coordinates) {
33
+ this.id = _Node.buildId(coordinates);
34
+ this.coordinates = coordinates;
35
+ this.innerEdges = [];
36
+ this.outerEdges = [];
37
+ this.outerEdgesSorted = false;
38
+ }
39
+ removeInnerEdge(edge) {
40
+ this.innerEdges = this.innerEdges.filter((e) => e.from.id !== edge.from.id);
41
+ }
42
+ removeOuterEdge(edge) {
43
+ this.outerEdges = this.outerEdges.filter((e) => e.to.id !== edge.to.id);
44
+ }
45
+ /**
46
+ * Outer edges are stored CCW order.
47
+ *
48
+ * @memberof Node
49
+ * @param {Edge} edge - Edge to add as an outerEdge.
50
+ */
51
+ addOuterEdge(edge) {
52
+ this.outerEdges.push(edge);
53
+ this.outerEdgesSorted = false;
54
+ }
55
+ /**
56
+ * Sorts outer edges in CCW way.
57
+ *
58
+ * @memberof Node
59
+ * @private
60
+ */
61
+ sortOuterEdges() {
62
+ if (!this.outerEdgesSorted) {
63
+ this.outerEdges.sort((a, b) => {
64
+ const aNode = a.to, bNode = b.to;
65
+ if (aNode.coordinates[0] - this.coordinates[0] >= 0 && bNode.coordinates[0] - this.coordinates[0] < 0)
66
+ return 1;
67
+ if (aNode.coordinates[0] - this.coordinates[0] < 0 && bNode.coordinates[0] - this.coordinates[0] >= 0)
68
+ return -1;
69
+ if (aNode.coordinates[0] - this.coordinates[0] === 0 && bNode.coordinates[0] - this.coordinates[0] === 0) {
70
+ if (aNode.coordinates[1] - this.coordinates[1] >= 0 || bNode.coordinates[1] - this.coordinates[1] >= 0)
71
+ return aNode.coordinates[1] - bNode.coordinates[1];
72
+ return bNode.coordinates[1] - aNode.coordinates[1];
73
+ }
74
+ const det = orientationIndex(
75
+ this.coordinates,
76
+ aNode.coordinates,
77
+ bNode.coordinates
78
+ );
79
+ if (det < 0)
80
+ return 1;
81
+ if (det > 0)
82
+ return -1;
83
+ const d1 = Math.pow(aNode.coordinates[0] - this.coordinates[0], 2) + Math.pow(aNode.coordinates[1] - this.coordinates[1], 2), d2 = Math.pow(bNode.coordinates[0] - this.coordinates[0], 2) + Math.pow(bNode.coordinates[1] - this.coordinates[1], 2);
84
+ return d1 - d2;
85
+ });
86
+ this.outerEdgesSorted = true;
87
+ }
88
+ }
89
+ /**
90
+ * Retrieves outer edges.
91
+ *
92
+ * They are sorted if they aren't in the CCW order.
93
+ *
94
+ * @memberof Node
95
+ * @returns {Edge[]} - List of outer edges sorted in a CCW order.
96
+ */
97
+ getOuterEdges() {
98
+ this.sortOuterEdges();
99
+ return this.outerEdges;
100
+ }
101
+ getOuterEdge(i) {
102
+ this.sortOuterEdges();
103
+ return this.outerEdges[i];
104
+ }
105
+ addInnerEdge(edge) {
106
+ this.innerEdges.push(edge);
107
+ }
108
+ };
109
+
110
+ // lib/Edge.ts
111
+
112
+ var Edge = class _Edge {
113
+ /**
114
+ * Creates or get the symetric Edge.
115
+ *
116
+ * @returns {Edge} - Symetric Edge.
117
+ */
118
+ getSymetric() {
119
+ if (!this.symetric) {
120
+ this.symetric = new _Edge(this.to, this.from);
121
+ this.symetric.symetric = this;
122
+ }
123
+ return this.symetric;
124
+ }
125
+ /**
126
+ * @param {Node} from - start node of the Edge
127
+ * @param {Node} to - end node of the edge
128
+ */
129
+ constructor(from, to) {
130
+ this.from = from;
131
+ this.to = to;
132
+ this.next = void 0;
133
+ this.label = void 0;
134
+ this.symetric = void 0;
135
+ this.ring = void 0;
136
+ this.from.addOuterEdge(this);
137
+ this.to.addInnerEdge(this);
138
+ }
139
+ /**
140
+ * Removes edge from from and to nodes.
141
+ */
142
+ deleteEdge() {
143
+ this.from.removeOuterEdge(this);
144
+ this.to.removeInnerEdge(this);
145
+ }
146
+ /**
147
+ * Compares Edge equallity.
148
+ *
149
+ * An edge is equal to another, if the from and to nodes are the same.
150
+ *
151
+ * @param {Edge} edge - Another Edge
152
+ * @returns {boolean} - True if Edges are equal, False otherwise
153
+ */
154
+ isEqual(edge) {
155
+ return this.from.id === edge.from.id && this.to.id === edge.to.id;
156
+ }
157
+ toString() {
158
+ return `Edge { ${this.from.id} -> ${this.to.id} }`;
159
+ }
160
+ /**
161
+ * Returns a LineString representation of the Edge
162
+ *
163
+ * @returns {Feature<LineString>} - LineString representation of the Edge
164
+ */
165
+ toLineString() {
166
+ return _helpers.lineString.call(void 0, [this.from.coordinates, this.to.coordinates]);
167
+ }
168
+ /**
169
+ * Comparator of two edges.
170
+ *
171
+ * Implementation of geos::planargraph::DirectedEdge::compareTo.
172
+ *
173
+ * @param {Edge} edge - Another edge to compare with this one
174
+ * @returns {number} -1 if this Edge has a greater angle with the positive x-axis than b,
175
+ * 0 if the Edges are colinear,
176
+ * 1 otherwise
177
+ */
178
+ compareTo(edge) {
179
+ return orientationIndex(
180
+ edge.from.coordinates,
181
+ edge.to.coordinates,
182
+ this.to.coordinates
183
+ );
184
+ }
185
+ };
186
+
187
+ // lib/EdgeRing.ts
188
+
189
+ var _envelope = require('@turf/envelope');
190
+
191
+ var EdgeRing = class {
192
+ constructor() {
193
+ this.edges = [];
194
+ this.polygon = void 0;
195
+ this.envelope = void 0;
196
+ }
197
+ /**
198
+ * Add an edge to the ring, inserting it in the last position.
199
+ *
200
+ * @memberof EdgeRing
201
+ * @param {Edge} edge - Edge to be inserted
202
+ */
203
+ push(edge) {
204
+ this.edges.push(edge);
205
+ this.polygon = this.envelope = void 0;
206
+ }
207
+ /**
208
+ * Get Edge.
209
+ *
210
+ * @memberof EdgeRing
211
+ * @param {number} i - Index
212
+ * @returns {Edge} - Edge in the i position
213
+ */
214
+ get(i) {
215
+ return this.edges[i];
216
+ }
217
+ /**
218
+ * Getter of length property.
219
+ *
220
+ * @memberof EdgeRing
221
+ * @returns {number} - Length of the edge ring.
222
+ */
223
+ get length() {
224
+ return this.edges.length;
225
+ }
226
+ /**
227
+ * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.
228
+ *
229
+ * @memberof EdgeRing
230
+ * @param {Function} f - The same function to be passed to Array.prototype.forEach
231
+ */
232
+ forEach(f) {
233
+ this.edges.forEach(f);
234
+ }
235
+ /**
236
+ * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
237
+ *
238
+ * @memberof EdgeRing
239
+ * @param {Function} f - The same function to be passed to Array.prototype.map
240
+ * @returns {Array} - The mapped values in the function
241
+ */
242
+ map(f) {
243
+ return this.edges.map(f);
244
+ }
245
+ /**
246
+ * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
247
+ *
248
+ * @memberof EdgeRing
249
+ * @param {Function} f - The same function to be passed to Array.prototype.some
250
+ * @returns {boolean} - True if an Edge check the condition
251
+ */
252
+ some(f) {
253
+ return this.edges.some(f);
254
+ }
255
+ /**
256
+ * Check if the ring is valid in geomtry terms.
257
+ *
258
+ * A ring must have either 0 or 4 or more points. The first and the last must be
259
+ * equal (in 2D)
260
+ * geos::geom::LinearRing::validateConstruction
261
+ *
262
+ * @memberof EdgeRing
263
+ * @returns {boolean} - Validity of the EdgeRing
264
+ */
265
+ isValid() {
266
+ return true;
267
+ }
268
+ /**
269
+ * Tests whether this ring is a hole.
270
+ *
271
+ * A ring is a hole if it is oriented counter-clockwise.
272
+ * Similar implementation of geos::algorithm::CGAlgorithms::isCCW
273
+ *
274
+ * @memberof EdgeRing
275
+ * @returns {boolean} - true: if it is a hole
276
+ */
277
+ isHole() {
278
+ const hiIndex = this.edges.reduce((high, edge, i) => {
279
+ if (edge.from.coordinates[1] > this.edges[high].from.coordinates[1])
280
+ high = i;
281
+ return high;
282
+ }, 0), iPrev = (hiIndex === 0 ? this.length : hiIndex) - 1, iNext = (hiIndex + 1) % this.length, disc = orientationIndex(
283
+ this.edges[iPrev].from.coordinates,
284
+ this.edges[hiIndex].from.coordinates,
285
+ this.edges[iNext].from.coordinates
286
+ );
287
+ if (disc === 0)
288
+ return this.edges[iPrev].from.coordinates[0] > this.edges[iNext].from.coordinates[0];
289
+ return disc > 0;
290
+ }
291
+ /**
292
+ * Creates a MultiPoint representing the EdgeRing (discarts edges directions).
293
+ *
294
+ * @memberof EdgeRing
295
+ * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
296
+ */
297
+ toMultiPoint() {
298
+ return _helpers.multiPoint.call(void 0, this.edges.map((edge) => edge.from.coordinates));
299
+ }
300
+ /**
301
+ * Creates a Polygon representing the EdgeRing.
302
+ *
303
+ * @memberof EdgeRing
304
+ * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring
305
+ */
306
+ toPolygon() {
307
+ if (this.polygon)
308
+ return this.polygon;
309
+ const coordinates = this.edges.map((edge) => edge.from.coordinates);
310
+ coordinates.push(this.edges[0].from.coordinates);
311
+ return this.polygon = _helpers.polygon.call(void 0, [coordinates]);
312
+ }
313
+ /**
314
+ * Calculates the envelope of the EdgeRing.
315
+ *
316
+ * @memberof EdgeRing
317
+ * @returns {Feature<Polygon>} - envelope
318
+ */
319
+ getEnvelope() {
320
+ if (this.envelope)
321
+ return this.envelope;
322
+ return this.envelope = _envelope.envelope.call(void 0, this.toPolygon());
323
+ }
324
+ /**
325
+ * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`
326
+ *
327
+ * @param {EdgeRing} testEdgeRing - EdgeRing to look in the list
328
+ * @param {EdgeRing[]} shellList - List of EdgeRing in which to search
329
+ *
330
+ * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
331
+ */
332
+ static findEdgeRingContaining(testEdgeRing, shellList) {
333
+ const testEnvelope = testEdgeRing.getEnvelope();
334
+ let minEnvelope, minShell;
335
+ shellList.forEach((shell) => {
336
+ const tryEnvelope = shell.getEnvelope();
337
+ if (minShell)
338
+ minEnvelope = minShell.getEnvelope();
339
+ if (envelopeIsEqual(tryEnvelope, testEnvelope))
340
+ return;
341
+ if (envelopeContains(tryEnvelope, testEnvelope)) {
342
+ const testEdgeRingCoordinates = testEdgeRing.map(
343
+ (edge) => edge.from.coordinates
344
+ );
345
+ let testPoint;
346
+ for (const pt of testEdgeRingCoordinates) {
347
+ if (!shell.some((edge) => coordinatesEqual(pt, edge.from.coordinates))) {
348
+ testPoint = pt;
349
+ }
350
+ }
351
+ if (testPoint && shell.inside(_helpers.point.call(void 0, testPoint))) {
352
+ if (!minShell || envelopeContains(minEnvelope, tryEnvelope))
353
+ minShell = shell;
354
+ }
355
+ }
356
+ });
357
+ return minShell;
358
+ }
359
+ /**
360
+ * Checks if the point is inside the edgeRing
361
+ *
362
+ * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
363
+ * @returns {boolean} - True if it is inside, False otherwise
364
+ */
365
+ inside(pt) {
366
+ return _booleanpointinpolygon.booleanPointInPolygon.call(void 0, pt, this.toPolygon());
367
+ }
368
+ };
369
+
370
+ // lib/Graph.ts
371
+ var _meta = require('@turf/meta');
372
+ var _invariant = require('@turf/invariant');
373
+ function validateGeoJson(geoJson) {
374
+ if (!geoJson)
375
+ throw new Error("No geojson passed");
376
+ if (geoJson.type !== "FeatureCollection" && geoJson.type !== "GeometryCollection" && geoJson.type !== "MultiLineString" && geoJson.type !== "LineString" && geoJson.type !== "Feature")
377
+ throw new Error(
378
+ `Invalid input type '${geoJson.type}'. Geojson must be FeatureCollection, GeometryCollection, LineString, MultiLineString or Feature`
379
+ );
380
+ }
381
+ var Graph = class _Graph {
382
+ /**
383
+ * Creates a graph from a GeoJSON.
384
+ *
385
+ * @param {FeatureCollection<LineString>} geoJson - it must comply with the restrictions detailed in the index
386
+ * @returns {Graph} - The newly created graph
387
+ * @throws {Error} if geoJson is invalid.
388
+ */
389
+ static fromGeoJson(geoJson) {
390
+ validateGeoJson(geoJson);
391
+ const graph = new _Graph();
392
+ _meta.flattenEach.call(void 0, geoJson, (feature) => {
393
+ _invariant.featureOf.call(void 0, feature, "LineString", "Graph::fromGeoJson");
394
+ _meta.coordReduce.call(void 0, feature, (prev, cur) => {
395
+ if (prev) {
396
+ const start = graph.getNode(prev), end = graph.getNode(cur);
397
+ graph.addEdge(start, end);
398
+ }
399
+ return cur;
400
+ });
401
+ });
402
+ return graph;
403
+ }
404
+ /**
405
+ * Creates or get a Node.
406
+ *
407
+ * @param {number[]} coordinates - Coordinates of the node
408
+ * @returns {Node} - The created or stored node
409
+ */
410
+ getNode(coordinates) {
411
+ const id = Node.buildId(coordinates);
412
+ let node = this.nodes[id];
413
+ if (!node)
414
+ node = this.nodes[id] = new Node(coordinates);
415
+ return node;
416
+ }
417
+ /**
418
+ * Adds an Edge and its symetricall.
419
+ *
420
+ * Edges are added symetrically, i.e.: we also add its symetric
421
+ *
422
+ * @param {Node} from - Node which starts the Edge
423
+ * @param {Node} to - Node which ends the Edge
424
+ */
425
+ addEdge(from, to) {
426
+ const edge = new Edge(from, to), symetricEdge = edge.getSymetric();
427
+ this.edges.push(edge);
428
+ this.edges.push(symetricEdge);
429
+ }
430
+ constructor() {
431
+ this.edges = [];
432
+ this.nodes = {};
433
+ }
434
+ /**
435
+ * Removes Dangle Nodes (nodes with grade 1).
436
+ */
437
+ deleteDangles() {
438
+ Object.keys(this.nodes).map((id) => this.nodes[id]).forEach((node) => this._removeIfDangle(node));
439
+ }
440
+ /**
441
+ * Check if node is dangle, if so, remove it.
442
+ *
443
+ * It calls itself recursively, removing a dangling node might cause another dangling node
444
+ *
445
+ * @param {Node} node - Node to check if it's a dangle
446
+ */
447
+ _removeIfDangle(node) {
448
+ if (node.innerEdges.length <= 1) {
449
+ const outerNodes = node.getOuterEdges().map((e) => e.to);
450
+ this.removeNode(node);
451
+ outerNodes.forEach((n) => this._removeIfDangle(n));
452
+ }
453
+ }
454
+ /**
455
+ * Delete cut-edges (bridge edges).
456
+ *
457
+ * The graph will be traversed, all the edges will be labeled according the ring
458
+ * in which they are. (The label is a number incremented by 1). Edges with the same
459
+ * label are cut-edges.
460
+ */
461
+ deleteCutEdges() {
462
+ this._computeNextCWEdges();
463
+ this._findLabeledEdgeRings();
464
+ this.edges.forEach((edge) => {
465
+ if (edge.label === edge.symetric.label) {
466
+ this.removeEdge(edge.symetric);
467
+ this.removeEdge(edge);
468
+ }
469
+ });
470
+ }
471
+ /**
472
+ * Set the `next` property of each Edge.
473
+ *
474
+ * The graph will be transversed in a CW form, so, we set the next of the symetrical edge as the previous one.
475
+ * OuterEdges are sorted CCW.
476
+ *
477
+ * @param {Node} [node] - If no node is passed, the function calls itself for every node in the Graph
478
+ */
479
+ _computeNextCWEdges(node) {
480
+ if (typeof node === "undefined") {
481
+ Object.keys(this.nodes).forEach(
482
+ (id) => this._computeNextCWEdges(this.nodes[id])
483
+ );
484
+ } else {
485
+ node.getOuterEdges().forEach((edge, i) => {
486
+ node.getOuterEdge(
487
+ (i === 0 ? node.getOuterEdges().length : i) - 1
488
+ ).symetric.next = edge;
489
+ });
490
+ }
491
+ }
492
+ /**
493
+ * Computes the next edge pointers going CCW around the given node, for the given edgering label.
494
+ *
495
+ * This algorithm has the effect of converting maximal edgerings into minimal edgerings
496
+ *
497
+ * XXX: method literally transcribed from `geos::operation::polygonize::PolygonizeGraph::computeNextCCWEdges`,
498
+ * could be written in a more javascript way.
499
+ *
500
+ * @param {Node} node - Node
501
+ * @param {number} label - Ring's label
502
+ */
503
+ _computeNextCCWEdges(node, label) {
504
+ const edges = node.getOuterEdges();
505
+ let firstOutDE, prevInDE;
506
+ for (let i = edges.length - 1; i >= 0; --i) {
507
+ let de = edges[i], sym = de.symetric, outDE, inDE;
508
+ if (de.label === label)
509
+ outDE = de;
510
+ if (sym.label === label)
511
+ inDE = sym;
512
+ if (!outDE || !inDE)
513
+ continue;
514
+ if (inDE)
515
+ prevInDE = inDE;
516
+ if (outDE) {
517
+ if (prevInDE) {
518
+ prevInDE.next = outDE;
519
+ prevInDE = void 0;
520
+ }
521
+ if (!firstOutDE)
522
+ firstOutDE = outDE;
523
+ }
524
+ }
525
+ if (prevInDE)
526
+ prevInDE.next = firstOutDE;
527
+ }
528
+ /**
529
+ * Finds rings and labels edges according to which rings are.
530
+ *
531
+ * The label is a number which is increased for each ring.
532
+ *
533
+ * @returns {Edge[]} edges that start rings
534
+ */
535
+ _findLabeledEdgeRings() {
536
+ const edgeRingStarts = [];
537
+ let label = 0;
538
+ this.edges.forEach((edge) => {
539
+ if (edge.label >= 0)
540
+ return;
541
+ edgeRingStarts.push(edge);
542
+ let e = edge;
543
+ do {
544
+ e.label = label;
545
+ e = e.next;
546
+ } while (!edge.isEqual(e));
547
+ label++;
548
+ });
549
+ return edgeRingStarts;
550
+ }
551
+ /**
552
+ * Computes the EdgeRings formed by the edges in this graph.
553
+ *
554
+ * @returns {EdgeRing[]} - A list of all the EdgeRings in the graph.
555
+ */
556
+ getEdgeRings() {
557
+ this._computeNextCWEdges();
558
+ this.edges.forEach((edge) => {
559
+ edge.label = void 0;
560
+ });
561
+ this._findLabeledEdgeRings().forEach((edge) => {
562
+ this._findIntersectionNodes(edge).forEach((node) => {
563
+ this._computeNextCCWEdges(node, edge.label);
564
+ });
565
+ });
566
+ const edgeRingList = [];
567
+ this.edges.forEach((edge) => {
568
+ if (edge.ring)
569
+ return;
570
+ edgeRingList.push(this._findEdgeRing(edge));
571
+ });
572
+ return edgeRingList;
573
+ }
574
+ /**
575
+ * Find all nodes in a Maxima EdgeRing which are self-intersection nodes.
576
+ *
577
+ * @param {Node} startEdge - Start Edge of the Ring
578
+ * @returns {Node[]} - intersection nodes
579
+ */
580
+ _findIntersectionNodes(startEdge) {
581
+ const intersectionNodes = [];
582
+ let edge = startEdge;
583
+ do {
584
+ let degree = 0;
585
+ edge.from.getOuterEdges().forEach((e) => {
586
+ if (e.label === startEdge.label)
587
+ ++degree;
588
+ });
589
+ if (degree > 1)
590
+ intersectionNodes.push(edge.from);
591
+ edge = edge.next;
592
+ } while (!startEdge.isEqual(edge));
593
+ return intersectionNodes;
594
+ }
595
+ /**
596
+ * Get the edge-ring which starts from the provided Edge.
597
+ *
598
+ * @param {Edge} startEdge - starting edge of the edge ring
599
+ * @returns {EdgeRing} - EdgeRing which start Edge is the provided one.
600
+ */
601
+ _findEdgeRing(startEdge) {
602
+ let edge = startEdge;
603
+ const edgeRing = new EdgeRing();
604
+ do {
605
+ edgeRing.push(edge);
606
+ edge.ring = edgeRing;
607
+ edge = edge.next;
608
+ } while (!startEdge.isEqual(edge));
609
+ return edgeRing;
610
+ }
611
+ /**
612
+ * Removes a node from the Graph.
613
+ *
614
+ * It also removes edges asociated to that node
615
+ * @param {Node} node - Node to be removed
616
+ */
617
+ removeNode(node) {
618
+ node.getOuterEdges().forEach((edge) => this.removeEdge(edge));
619
+ node.innerEdges.forEach((edge) => this.removeEdge(edge));
620
+ delete this.nodes[node.id];
621
+ }
622
+ /**
623
+ * Remove edge from the graph and deletes the edge.
624
+ *
625
+ * @param {Edge} edge - Edge to be removed
626
+ */
627
+ removeEdge(edge) {
628
+ this.edges = this.edges.filter((e) => !e.isEqual(edge));
629
+ edge.deleteEdge();
630
+ }
631
+ };
632
+
633
+ // index.ts
634
+ function polygonize(geoJson) {
635
+ const graph = Graph.fromGeoJson(geoJson);
636
+ graph.deleteDangles();
637
+ graph.deleteCutEdges();
638
+ const holes = [], shells = [];
639
+ graph.getEdgeRings().filter((edgeRing) => edgeRing.isValid()).forEach((edgeRing) => {
640
+ if (edgeRing.isHole())
641
+ holes.push(edgeRing);
642
+ else
643
+ shells.push(edgeRing);
644
+ });
645
+ holes.forEach((hole) => {
646
+ if (EdgeRing.findEdgeRingContaining(hole, shells))
647
+ shells.push(hole);
648
+ });
649
+ return _helpers.featureCollection.call(void 0, shells.map((shell) => shell.toPolygon()));
650
+ }
651
+ var turf_polygonize_default = polygonize;
652
+
653
+
654
+
655
+ exports.default = turf_polygonize_default; exports.polygonize = polygonize;
656
+ //# sourceMappingURL=index.cjs.map