@turf/polygonize 6.4.0 → 7.0.0-alpha.0

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.
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("@turf/helpers");
4
- var util_1 = require("./util");
3
+ const helpers_1 = require("@turf/helpers");
4
+ const util_1 = require("./util");
5
5
  /**
6
6
  * This class is inspired by GEOS's geos::operation::polygonize::PolygonizeDirectedEdge
7
7
  */
8
- var Edge = /** @class */ (function () {
8
+ class Edge {
9
9
  /**
10
10
  * @param {Node} from - start node of the Edge
11
11
  * @param {Node} to - end node of the edge
12
12
  */
13
- function Edge(from, to) {
13
+ constructor(from, to) {
14
14
  this.from = from; //< start
15
15
  this.to = to; //< End
16
16
  this.next = undefined; //< The edge to be computed after
@@ -25,20 +25,20 @@ var Edge = /** @class */ (function () {
25
25
  *
26
26
  * @returns {Edge} - Symetric Edge.
27
27
  */
28
- Edge.prototype.getSymetric = function () {
28
+ getSymetric() {
29
29
  if (!this.symetric) {
30
30
  this.symetric = new Edge(this.to, this.from);
31
31
  this.symetric.symetric = this;
32
32
  }
33
33
  return this.symetric;
34
- };
34
+ }
35
35
  /**
36
36
  * Removes edge from from and to nodes.
37
37
  */
38
- Edge.prototype.deleteEdge = function () {
38
+ deleteEdge() {
39
39
  this.from.removeOuterEdge(this);
40
40
  this.to.removeInnerEdge(this);
41
- };
41
+ }
42
42
  /**
43
43
  * Compares Edge equallity.
44
44
  *
@@ -47,20 +47,20 @@ var Edge = /** @class */ (function () {
47
47
  * @param {Edge} edge - Another Edge
48
48
  * @returns {boolean} - True if Edges are equal, False otherwise
49
49
  */
50
- Edge.prototype.isEqual = function (edge) {
50
+ isEqual(edge) {
51
51
  return this.from.id === edge.from.id && this.to.id === edge.to.id;
52
- };
53
- Edge.prototype.toString = function () {
54
- return "Edge { " + this.from.id + " -> " + this.to.id + " }";
55
- };
52
+ }
53
+ toString() {
54
+ return `Edge { ${this.from.id} -> ${this.to.id} }`;
55
+ }
56
56
  /**
57
57
  * Returns a LineString representation of the Edge
58
58
  *
59
59
  * @returns {Feature<LineString>} - LineString representation of the Edge
60
60
  */
61
- Edge.prototype.toLineString = function () {
61
+ toLineString() {
62
62
  return helpers_1.lineString([this.from.coordinates, this.to.coordinates]);
63
- };
63
+ }
64
64
  /**
65
65
  * Comparator of two edges.
66
66
  *
@@ -71,9 +71,8 @@ var Edge = /** @class */ (function () {
71
71
  * 0 if the Edges are colinear,
72
72
  * 1 otherwise
73
73
  */
74
- Edge.prototype.compareTo = function (edge) {
74
+ compareTo(edge) {
75
75
  return util_1.orientationIndex(edge.from.coordinates, edge.to.coordinates, this.to.coordinates);
76
- };
77
- return Edge;
78
- }());
76
+ }
77
+ }
79
78
  exports.default = Edge;
@@ -1,4 +1,4 @@
1
- import { Polygon, Feature } from "@turf/helpers";
1
+ import { Polygon, Feature, Point } from "geojson";
2
2
  import Edge from "./Edge";
3
3
  /**
4
4
  * Ring of edges which form a polygon.
@@ -9,8 +9,8 @@ import Edge from "./Edge";
9
9
  */
10
10
  export default class EdgeRing {
11
11
  private edges;
12
- private polygon;
13
- private envelope;
12
+ private polygon?;
13
+ private envelope?;
14
14
  constructor();
15
15
  /**
16
16
  * Add an edge to the ring, inserting it in the last position.
@@ -18,7 +18,7 @@ export default class EdgeRing {
18
18
  * @memberof EdgeRing
19
19
  * @param {Edge} edge - Edge to be inserted
20
20
  */
21
- push(edge: any): void;
21
+ push(edge: Edge): void;
22
22
  /**
23
23
  * Get Edge.
24
24
  *
@@ -26,7 +26,7 @@ export default class EdgeRing {
26
26
  * @param {number} i - Index
27
27
  * @returns {Edge} - Edge in the i position
28
28
  */
29
- get(i: any): Edge;
29
+ get(i: number): Edge;
30
30
  /**
31
31
  * Getter of length property.
32
32
  *
@@ -40,7 +40,7 @@ export default class EdgeRing {
40
40
  * @memberof EdgeRing
41
41
  * @param {Function} f - The same function to be passed to Array.prototype.forEach
42
42
  */
43
- forEach(f: any): void;
43
+ forEach(f: (edge: Edge, index: number, array: Edge[]) => void): void;
44
44
  /**
45
45
  * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
46
46
  *
@@ -48,7 +48,7 @@ export default class EdgeRing {
48
48
  * @param {Function} f - The same function to be passed to Array.prototype.map
49
49
  * @returns {Array} - The mapped values in the function
50
50
  */
51
- map(f: any): unknown[];
51
+ map<T>(f: (edge: Edge, index: number, array: Edge[]) => T): T[];
52
52
  /**
53
53
  * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
54
54
  *
@@ -56,7 +56,7 @@ export default class EdgeRing {
56
56
  * @param {Function} f - The same function to be passed to Array.prototype.some
57
57
  * @returns {boolean} - True if an Edge check the condition
58
58
  */
59
- some(f: any): boolean;
59
+ some(f: (edge: Edge, index: number, array: Edge[]) => boolean): boolean;
60
60
  /**
61
61
  * Check if the ring is valid in geomtry terms.
62
62
  *
@@ -84,9 +84,7 @@ export default class EdgeRing {
84
84
  * @memberof EdgeRing
85
85
  * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
86
86
  */
87
- toMultiPoint(): Feature<import("@turf/helpers").MultiPoint, {
88
- [name: string]: any;
89
- }>;
87
+ toMultiPoint(): Feature<import("geojson").MultiPoint, import("geojson").GeoJsonProperties>;
90
88
  /**
91
89
  * Creates a Polygon representing the EdgeRing.
92
90
  *
@@ -113,12 +111,12 @@ export default class EdgeRing {
113
111
  *
114
112
  * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
115
113
  */
116
- static findEdgeRingContaining(testEdgeRing: any, shellList: any): any;
114
+ static findEdgeRingContaining(testEdgeRing: EdgeRing, shellList: EdgeRing[]): EdgeRing | undefined;
117
115
  /**
118
116
  * Checks if the point is inside the edgeRing
119
117
  *
120
118
  * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
121
119
  * @returns {boolean} - True if it is inside, False otherwise
122
120
  */
123
- inside(pt: any): boolean;
121
+ inside(pt: Feature<Point>): boolean;
124
122
  }
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var util_1 = require("./util");
7
- var helpers_1 = require("@turf/helpers");
8
- var envelope_1 = __importDefault(require("@turf/envelope"));
9
- var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
3
+ const tslib_1 = require("tslib");
4
+ const util_1 = require("./util");
5
+ const helpers_1 = require("@turf/helpers");
6
+ const envelope_1 = tslib_1.__importDefault(require("@turf/envelope"));
7
+ const boolean_point_in_polygon_1 = tslib_1.__importDefault(require("@turf/boolean-point-in-polygon"));
10
8
  /**
11
9
  * Ring of edges which form a polygon.
12
10
  *
@@ -14,8 +12,8 @@ var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in
14
12
  *
15
13
  * This class is inspired in GEOS's geos::operation::polygonize::EdgeRing
16
14
  */
17
- var EdgeRing = /** @class */ (function () {
18
- function EdgeRing() {
15
+ class EdgeRing {
16
+ constructor() {
19
17
  this.edges = [];
20
18
  this.polygon = undefined; //< Caches Polygon representation
21
19
  this.envelope = undefined; //< Caches Envelope representation
@@ -26,12 +24,10 @@ var EdgeRing = /** @class */ (function () {
26
24
  * @memberof EdgeRing
27
25
  * @param {Edge} edge - Edge to be inserted
28
26
  */
29
- EdgeRing.prototype.push = function (edge) {
30
- // Emulate Array getter ([]) behaviour
31
- this[this.edges.length] = edge;
27
+ push(edge) {
32
28
  this.edges.push(edge);
33
29
  this.polygon = this.envelope = undefined;
34
- };
30
+ }
35
31
  /**
36
32
  * Get Edge.
37
33
  *
@@ -39,31 +35,27 @@ var EdgeRing = /** @class */ (function () {
39
35
  * @param {number} i - Index
40
36
  * @returns {Edge} - Edge in the i position
41
37
  */
42
- EdgeRing.prototype.get = function (i) {
38
+ get(i) {
43
39
  return this.edges[i];
44
- };
45
- Object.defineProperty(EdgeRing.prototype, "length", {
46
- /**
47
- * Getter of length property.
48
- *
49
- * @memberof EdgeRing
50
- * @returns {number} - Length of the edge ring.
51
- */
52
- get: function () {
53
- return this.edges.length;
54
- },
55
- enumerable: true,
56
- configurable: true
57
- });
40
+ }
41
+ /**
42
+ * Getter of length property.
43
+ *
44
+ * @memberof EdgeRing
45
+ * @returns {number} - Length of the edge ring.
46
+ */
47
+ get length() {
48
+ return this.edges.length;
49
+ }
58
50
  /**
59
51
  * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.
60
52
  *
61
53
  * @memberof EdgeRing
62
54
  * @param {Function} f - The same function to be passed to Array.prototype.forEach
63
55
  */
64
- EdgeRing.prototype.forEach = function (f) {
56
+ forEach(f) {
65
57
  this.edges.forEach(f);
66
- };
58
+ }
67
59
  /**
68
60
  * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
69
61
  *
@@ -71,9 +63,9 @@ var EdgeRing = /** @class */ (function () {
71
63
  * @param {Function} f - The same function to be passed to Array.prototype.map
72
64
  * @returns {Array} - The mapped values in the function
73
65
  */
74
- EdgeRing.prototype.map = function (f) {
66
+ map(f) {
75
67
  return this.edges.map(f);
76
- };
68
+ }
77
69
  /**
78
70
  * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
79
71
  *
@@ -81,9 +73,9 @@ var EdgeRing = /** @class */ (function () {
81
73
  * @param {Function} f - The same function to be passed to Array.prototype.some
82
74
  * @returns {boolean} - True if an Edge check the condition
83
75
  */
84
- EdgeRing.prototype.some = function (f) {
76
+ some(f) {
85
77
  return this.edges.some(f);
86
- };
78
+ }
87
79
  /**
88
80
  * Check if the ring is valid in geomtry terms.
89
81
  *
@@ -94,10 +86,10 @@ var EdgeRing = /** @class */ (function () {
94
86
  * @memberof EdgeRing
95
87
  * @returns {boolean} - Validity of the EdgeRing
96
88
  */
97
- EdgeRing.prototype.isValid = function () {
89
+ isValid() {
98
90
  // TODO: stub
99
91
  return true;
100
- };
92
+ }
101
93
  /**
102
94
  * Tests whether this ring is a hole.
103
95
  *
@@ -107,12 +99,11 @@ var EdgeRing = /** @class */ (function () {
107
99
  * @memberof EdgeRing
108
100
  * @returns {boolean} - true: if it is a hole
109
101
  */
110
- EdgeRing.prototype.isHole = function () {
111
- var _this = this;
102
+ isHole() {
112
103
  // XXX: Assuming Ring is valid
113
104
  // Find highest point
114
- var hiIndex = this.edges.reduce(function (high, edge, i) {
115
- if (edge.from.coordinates[1] > _this.edges[high].from.coordinates[1])
105
+ const hiIndex = this.edges.reduce((high, edge, i) => {
106
+ if (edge.from.coordinates[1] > this.edges[high].from.coordinates[1])
116
107
  high = i;
117
108
  return high;
118
109
  }, 0), iPrev = (hiIndex === 0 ? this.length : hiIndex) - 1, iNext = (hiIndex + 1) % this.length, disc = util_1.orientationIndex(this.edges[iPrev].from.coordinates, this.edges[hiIndex].from.coordinates, this.edges[iNext].from.coordinates);
@@ -120,40 +111,40 @@ var EdgeRing = /** @class */ (function () {
120
111
  return (this.edges[iPrev].from.coordinates[0] >
121
112
  this.edges[iNext].from.coordinates[0]);
122
113
  return disc > 0;
123
- };
114
+ }
124
115
  /**
125
116
  * Creates a MultiPoint representing the EdgeRing (discarts edges directions).
126
117
  *
127
118
  * @memberof EdgeRing
128
119
  * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
129
120
  */
130
- EdgeRing.prototype.toMultiPoint = function () {
131
- return helpers_1.multiPoint(this.edges.map(function (edge) { return edge.from.coordinates; }));
132
- };
121
+ toMultiPoint() {
122
+ return helpers_1.multiPoint(this.edges.map((edge) => edge.from.coordinates));
123
+ }
133
124
  /**
134
125
  * Creates a Polygon representing the EdgeRing.
135
126
  *
136
127
  * @memberof EdgeRing
137
128
  * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring
138
129
  */
139
- EdgeRing.prototype.toPolygon = function () {
130
+ toPolygon() {
140
131
  if (this.polygon)
141
132
  return this.polygon;
142
- var coordinates = this.edges.map(function (edge) { return edge.from.coordinates; });
133
+ const coordinates = this.edges.map((edge) => edge.from.coordinates);
143
134
  coordinates.push(this.edges[0].from.coordinates);
144
135
  return (this.polygon = helpers_1.polygon([coordinates]));
145
- };
136
+ }
146
137
  /**
147
138
  * Calculates the envelope of the EdgeRing.
148
139
  *
149
140
  * @memberof EdgeRing
150
141
  * @returns {Feature<Polygon>} - envelope
151
142
  */
152
- EdgeRing.prototype.getEnvelope = function () {
143
+ getEnvelope() {
153
144
  if (this.envelope)
154
145
  return this.envelope;
155
146
  return (this.envelope = envelope_1.default(this.toPolygon()));
156
- };
147
+ }
157
148
  /**
158
149
  * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`
159
150
  *
@@ -162,22 +153,24 @@ var EdgeRing = /** @class */ (function () {
162
153
  *
163
154
  * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
164
155
  */
165
- EdgeRing.findEdgeRingContaining = function (testEdgeRing, shellList) {
166
- var testEnvelope = testEdgeRing.getEnvelope();
167
- var minEnvelope, minShell;
168
- shellList.forEach(function (shell) {
169
- var tryEnvelope = shell.getEnvelope();
156
+ static findEdgeRingContaining(testEdgeRing, shellList) {
157
+ const testEnvelope = testEdgeRing.getEnvelope();
158
+ let minEnvelope, minShell;
159
+ shellList.forEach((shell) => {
160
+ const tryEnvelope = shell.getEnvelope();
170
161
  if (minShell)
171
162
  minEnvelope = minShell.getEnvelope();
172
163
  // the hole envelope cannot equal the shell envelope
173
164
  if (util_1.envelopeIsEqual(tryEnvelope, testEnvelope))
174
165
  return;
175
166
  if (util_1.envelopeContains(tryEnvelope, testEnvelope)) {
176
- var testPoint = testEdgeRing
177
- .map(function (edge) { return edge.from.coordinates; })
178
- .find(function (pt) {
179
- return !shell.some(function (edge) { return util_1.coordinatesEqual(pt, edge.from.coordinates); });
180
- });
167
+ const testEdgeRingCoordinates = testEdgeRing.map((edge) => edge.from.coordinates);
168
+ let testPoint;
169
+ for (const pt of testEdgeRingCoordinates) {
170
+ if (!shell.some((edge) => util_1.coordinatesEqual(pt, edge.from.coordinates))) {
171
+ testPoint = pt;
172
+ }
173
+ }
181
174
  if (testPoint && shell.inside(helpers_1.point(testPoint))) {
182
175
  if (!minShell || util_1.envelopeContains(minEnvelope, tryEnvelope))
183
176
  minShell = shell;
@@ -185,16 +178,15 @@ var EdgeRing = /** @class */ (function () {
185
178
  }
186
179
  });
187
180
  return minShell;
188
- };
181
+ }
189
182
  /**
190
183
  * Checks if the point is inside the edgeRing
191
184
  *
192
185
  * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
193
186
  * @returns {boolean} - True if it is inside, False otherwise
194
187
  */
195
- EdgeRing.prototype.inside = function (pt) {
188
+ inside(pt) {
196
189
  return boolean_point_in_polygon_1.default(pt, this.toPolygon());
197
- };
198
- return EdgeRing;
199
- }());
190
+ }
191
+ }
200
192
  exports.default = EdgeRing;
@@ -1,7 +1,7 @@
1
1
  import Node from "./Node";
2
2
  import Edge from "./Edge";
3
3
  import EdgeRing from "./EdgeRing";
4
- import { FeatureCollection, LineString, MultiLineString, Feature } from "@turf/helpers";
4
+ import { FeatureCollection, LineString, MultiLineString, Feature } from "geojson";
5
5
  /**
6
6
  * Represents a planar graph of edges and nodes that can be used to compute a polygonization.
7
7
  *
@@ -87,20 +87,20 @@ export default class Graph {
87
87
  *
88
88
  * @returns {Edge[]} edges that start rings
89
89
  */
90
- _findLabeledEdgeRings(): any[];
90
+ _findLabeledEdgeRings(): Edge[];
91
91
  /**
92
92
  * Computes the EdgeRings formed by the edges in this graph.
93
93
  *
94
94
  * @returns {EdgeRing[]} - A list of all the EdgeRings in the graph.
95
95
  */
96
- getEdgeRings(): any[];
96
+ getEdgeRings(): EdgeRing[];
97
97
  /**
98
98
  * Find all nodes in a Maxima EdgeRing which are self-intersection nodes.
99
99
  *
100
100
  * @param {Node} startEdge - Start Edge of the Ring
101
101
  * @returns {Node[]} - intersection nodes
102
102
  */
103
- _findIntersectionNodes(startEdge: Edge): any[];
103
+ _findIntersectionNodes(startEdge: Edge): Node[];
104
104
  /**
105
105
  * Get the edge-ring which starts from the provided Edge.
106
106
  *