@turf/polygonize 7.0.0-alpha.1 → 7.0.0-alpha.110

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,122 +0,0 @@
1
- import { Polygon, Feature, Point } from "geojson";
2
- import Edge from "./Edge";
3
- /**
4
- * Ring of edges which form a polygon.
5
- *
6
- * The ring may be either an outer shell or a hole.
7
- *
8
- * This class is inspired in GEOS's geos::operation::polygonize::EdgeRing
9
- */
10
- export default class EdgeRing {
11
- private edges;
12
- private polygon?;
13
- private envelope?;
14
- constructor();
15
- /**
16
- * Add an edge to the ring, inserting it in the last position.
17
- *
18
- * @memberof EdgeRing
19
- * @param {Edge} edge - Edge to be inserted
20
- */
21
- push(edge: Edge): void;
22
- /**
23
- * Get Edge.
24
- *
25
- * @memberof EdgeRing
26
- * @param {number} i - Index
27
- * @returns {Edge} - Edge in the i position
28
- */
29
- get(i: number): Edge;
30
- /**
31
- * Getter of length property.
32
- *
33
- * @memberof EdgeRing
34
- * @returns {number} - Length of the edge ring.
35
- */
36
- get length(): number;
37
- /**
38
- * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.
39
- *
40
- * @memberof EdgeRing
41
- * @param {Function} f - The same function to be passed to Array.prototype.forEach
42
- */
43
- forEach(f: (edge: Edge, index: number, array: Edge[]) => void): void;
44
- /**
45
- * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
46
- *
47
- * @memberof EdgeRing
48
- * @param {Function} f - The same function to be passed to Array.prototype.map
49
- * @returns {Array} - The mapped values in the function
50
- */
51
- map<T>(f: (edge: Edge, index: number, array: Edge[]) => T): T[];
52
- /**
53
- * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
54
- *
55
- * @memberof EdgeRing
56
- * @param {Function} f - The same function to be passed to Array.prototype.some
57
- * @returns {boolean} - True if an Edge check the condition
58
- */
59
- some(f: (edge: Edge, index: number, array: Edge[]) => boolean): boolean;
60
- /**
61
- * Check if the ring is valid in geomtry terms.
62
- *
63
- * A ring must have either 0 or 4 or more points. The first and the last must be
64
- * equal (in 2D)
65
- * geos::geom::LinearRing::validateConstruction
66
- *
67
- * @memberof EdgeRing
68
- * @returns {boolean} - Validity of the EdgeRing
69
- */
70
- isValid(): boolean;
71
- /**
72
- * Tests whether this ring is a hole.
73
- *
74
- * A ring is a hole if it is oriented counter-clockwise.
75
- * Similar implementation of geos::algorithm::CGAlgorithms::isCCW
76
- *
77
- * @memberof EdgeRing
78
- * @returns {boolean} - true: if it is a hole
79
- */
80
- isHole(): boolean;
81
- /**
82
- * Creates a MultiPoint representing the EdgeRing (discarts edges directions).
83
- *
84
- * @memberof EdgeRing
85
- * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
86
- */
87
- toMultiPoint(): Feature<import("geojson").MultiPoint, import("geojson").GeoJsonProperties>;
88
- /**
89
- * Creates a Polygon representing the EdgeRing.
90
- *
91
- * @memberof EdgeRing
92
- * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring
93
- */
94
- toPolygon(): Feature<Polygon, {
95
- [name: string]: any;
96
- }>;
97
- /**
98
- * Calculates the envelope of the EdgeRing.
99
- *
100
- * @memberof EdgeRing
101
- * @returns {Feature<Polygon>} - envelope
102
- */
103
- getEnvelope(): Feature<Polygon, {
104
- [name: string]: any;
105
- }>;
106
- /**
107
- * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`
108
- *
109
- * @param {EdgeRing} testEdgeRing - EdgeRing to look in the list
110
- * @param {EdgeRing[]} shellList - List of EdgeRing in which to search
111
- *
112
- * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
113
- */
114
- static findEdgeRingContaining(testEdgeRing: EdgeRing, shellList: EdgeRing[]): EdgeRing | undefined;
115
- /**
116
- * Checks if the point is inside the edgeRing
117
- *
118
- * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
119
- * @returns {boolean} - True if it is inside, False otherwise
120
- */
121
- inside(pt: Feature<Point>): boolean;
122
- }
@@ -1,192 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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"));
8
- /**
9
- * Ring of edges which form a polygon.
10
- *
11
- * The ring may be either an outer shell or a hole.
12
- *
13
- * This class is inspired in GEOS's geos::operation::polygonize::EdgeRing
14
- */
15
- class EdgeRing {
16
- constructor() {
17
- this.edges = [];
18
- this.polygon = undefined; //< Caches Polygon representation
19
- this.envelope = undefined; //< Caches Envelope representation
20
- }
21
- /**
22
- * Add an edge to the ring, inserting it in the last position.
23
- *
24
- * @memberof EdgeRing
25
- * @param {Edge} edge - Edge to be inserted
26
- */
27
- push(edge) {
28
- this.edges.push(edge);
29
- this.polygon = this.envelope = undefined;
30
- }
31
- /**
32
- * Get Edge.
33
- *
34
- * @memberof EdgeRing
35
- * @param {number} i - Index
36
- * @returns {Edge} - Edge in the i position
37
- */
38
- get(i) {
39
- return this.edges[i];
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
- }
50
- /**
51
- * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.
52
- *
53
- * @memberof EdgeRing
54
- * @param {Function} f - The same function to be passed to Array.prototype.forEach
55
- */
56
- forEach(f) {
57
- this.edges.forEach(f);
58
- }
59
- /**
60
- * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
61
- *
62
- * @memberof EdgeRing
63
- * @param {Function} f - The same function to be passed to Array.prototype.map
64
- * @returns {Array} - The mapped values in the function
65
- */
66
- map(f) {
67
- return this.edges.map(f);
68
- }
69
- /**
70
- * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
71
- *
72
- * @memberof EdgeRing
73
- * @param {Function} f - The same function to be passed to Array.prototype.some
74
- * @returns {boolean} - True if an Edge check the condition
75
- */
76
- some(f) {
77
- return this.edges.some(f);
78
- }
79
- /**
80
- * Check if the ring is valid in geomtry terms.
81
- *
82
- * A ring must have either 0 or 4 or more points. The first and the last must be
83
- * equal (in 2D)
84
- * geos::geom::LinearRing::validateConstruction
85
- *
86
- * @memberof EdgeRing
87
- * @returns {boolean} - Validity of the EdgeRing
88
- */
89
- isValid() {
90
- // TODO: stub
91
- return true;
92
- }
93
- /**
94
- * Tests whether this ring is a hole.
95
- *
96
- * A ring is a hole if it is oriented counter-clockwise.
97
- * Similar implementation of geos::algorithm::CGAlgorithms::isCCW
98
- *
99
- * @memberof EdgeRing
100
- * @returns {boolean} - true: if it is a hole
101
- */
102
- isHole() {
103
- // XXX: Assuming Ring is valid
104
- // Find highest point
105
- const hiIndex = this.edges.reduce((high, edge, i) => {
106
- if (edge.from.coordinates[1] > this.edges[high].from.coordinates[1])
107
- high = i;
108
- return high;
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);
110
- if (disc === 0)
111
- return (this.edges[iPrev].from.coordinates[0] >
112
- this.edges[iNext].from.coordinates[0]);
113
- return disc > 0;
114
- }
115
- /**
116
- * Creates a MultiPoint representing the EdgeRing (discarts edges directions).
117
- *
118
- * @memberof EdgeRing
119
- * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
120
- */
121
- toMultiPoint() {
122
- return helpers_1.multiPoint(this.edges.map((edge) => edge.from.coordinates));
123
- }
124
- /**
125
- * Creates a Polygon representing the EdgeRing.
126
- *
127
- * @memberof EdgeRing
128
- * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring
129
- */
130
- toPolygon() {
131
- if (this.polygon)
132
- return this.polygon;
133
- const coordinates = this.edges.map((edge) => edge.from.coordinates);
134
- coordinates.push(this.edges[0].from.coordinates);
135
- return (this.polygon = helpers_1.polygon([coordinates]));
136
- }
137
- /**
138
- * Calculates the envelope of the EdgeRing.
139
- *
140
- * @memberof EdgeRing
141
- * @returns {Feature<Polygon>} - envelope
142
- */
143
- getEnvelope() {
144
- if (this.envelope)
145
- return this.envelope;
146
- return (this.envelope = envelope_1.default(this.toPolygon()));
147
- }
148
- /**
149
- * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`
150
- *
151
- * @param {EdgeRing} testEdgeRing - EdgeRing to look in the list
152
- * @param {EdgeRing[]} shellList - List of EdgeRing in which to search
153
- *
154
- * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
155
- */
156
- static findEdgeRingContaining(testEdgeRing, shellList) {
157
- const testEnvelope = testEdgeRing.getEnvelope();
158
- let minEnvelope, minShell;
159
- shellList.forEach((shell) => {
160
- const tryEnvelope = shell.getEnvelope();
161
- if (minShell)
162
- minEnvelope = minShell.getEnvelope();
163
- // the hole envelope cannot equal the shell envelope
164
- if (util_1.envelopeIsEqual(tryEnvelope, testEnvelope))
165
- return;
166
- if (util_1.envelopeContains(tryEnvelope, testEnvelope)) {
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
- }
174
- if (testPoint && shell.inside(helpers_1.point(testPoint))) {
175
- if (!minShell || util_1.envelopeContains(minEnvelope, tryEnvelope))
176
- minShell = shell;
177
- }
178
- }
179
- });
180
- return minShell;
181
- }
182
- /**
183
- * Checks if the point is inside the edgeRing
184
- *
185
- * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
186
- * @returns {boolean} - True if it is inside, False otherwise
187
- */
188
- inside(pt) {
189
- return boolean_point_in_polygon_1.default(pt, this.toPolygon());
190
- }
191
- }
192
- exports.default = EdgeRing;
@@ -1,124 +0,0 @@
1
- import Node from "./Node";
2
- import Edge from "./Edge";
3
- import EdgeRing from "./EdgeRing";
4
- import { FeatureCollection, LineString, MultiLineString, Feature } from "geojson";
5
- /**
6
- * Represents a planar graph of edges and nodes that can be used to compute a polygonization.
7
- *
8
- * Although, this class is inspired by GEOS's `geos::operation::polygonize::PolygonizeGraph`,
9
- * it isn't a rewrite. As regards algorithm, this class implements the same logic, but it
10
- * isn't a javascript transcription of the C++ source.
11
- *
12
- * This graph is directed (both directions are created)
13
- */
14
- export default class Graph {
15
- private nodes;
16
- private edges;
17
- /**
18
- * Creates a graph from a GeoJSON.
19
- *
20
- * @param {FeatureCollection<LineString>} geoJson - it must comply with the restrictions detailed in the index
21
- * @returns {Graph} - The newly created graph
22
- * @throws {Error} if geoJson is invalid.
23
- */
24
- static fromGeoJson(geoJson: FeatureCollection<LineString | MultiLineString> | LineString | MultiLineString | Feature<LineString | MultiLineString>): Graph;
25
- /**
26
- * Creates or get a Node.
27
- *
28
- * @param {number[]} coordinates - Coordinates of the node
29
- * @returns {Node} - The created or stored node
30
- */
31
- getNode(coordinates: number[]): Node;
32
- /**
33
- * Adds an Edge and its symetricall.
34
- *
35
- * Edges are added symetrically, i.e.: we also add its symetric
36
- *
37
- * @param {Node} from - Node which starts the Edge
38
- * @param {Node} to - Node which ends the Edge
39
- */
40
- addEdge(from: Node, to: Node): void;
41
- constructor();
42
- /**
43
- * Removes Dangle Nodes (nodes with grade 1).
44
- */
45
- deleteDangles(): void;
46
- /**
47
- * Check if node is dangle, if so, remove it.
48
- *
49
- * It calls itself recursively, removing a dangling node might cause another dangling node
50
- *
51
- * @param {Node} node - Node to check if it's a dangle
52
- */
53
- _removeIfDangle(node: Node): void;
54
- /**
55
- * Delete cut-edges (bridge edges).
56
- *
57
- * The graph will be traversed, all the edges will be labeled according the ring
58
- * in which they are. (The label is a number incremented by 1). Edges with the same
59
- * label are cut-edges.
60
- */
61
- deleteCutEdges(): void;
62
- /**
63
- * Set the `next` property of each Edge.
64
- *
65
- * The graph will be transversed in a CW form, so, we set the next of the symetrical edge as the previous one.
66
- * OuterEdges are sorted CCW.
67
- *
68
- * @param {Node} [node] - If no node is passed, the function calls itself for every node in the Graph
69
- */
70
- _computeNextCWEdges(node?: Node): void;
71
- /**
72
- * Computes the next edge pointers going CCW around the given node, for the given edgering label.
73
- *
74
- * This algorithm has the effect of converting maximal edgerings into minimal edgerings
75
- *
76
- * XXX: method literally transcribed from `geos::operation::polygonize::PolygonizeGraph::computeNextCCWEdges`,
77
- * could be written in a more javascript way.
78
- *
79
- * @param {Node} node - Node
80
- * @param {number} label - Ring's label
81
- */
82
- _computeNextCCWEdges(node: Node, label: number): void;
83
- /**
84
- * Finds rings and labels edges according to which rings are.
85
- *
86
- * The label is a number which is increased for each ring.
87
- *
88
- * @returns {Edge[]} edges that start rings
89
- */
90
- _findLabeledEdgeRings(): Edge[];
91
- /**
92
- * Computes the EdgeRings formed by the edges in this graph.
93
- *
94
- * @returns {EdgeRing[]} - A list of all the EdgeRings in the graph.
95
- */
96
- getEdgeRings(): EdgeRing[];
97
- /**
98
- * Find all nodes in a Maxima EdgeRing which are self-intersection nodes.
99
- *
100
- * @param {Node} startEdge - Start Edge of the Ring
101
- * @returns {Node[]} - intersection nodes
102
- */
103
- _findIntersectionNodes(startEdge: Edge): Node[];
104
- /**
105
- * Get the edge-ring which starts from the provided Edge.
106
- *
107
- * @param {Edge} startEdge - starting edge of the edge ring
108
- * @returns {EdgeRing} - EdgeRing which start Edge is the provided one.
109
- */
110
- _findEdgeRing(startEdge: Edge): EdgeRing;
111
- /**
112
- * Removes a node from the Graph.
113
- *
114
- * It also removes edges asociated to that node
115
- * @param {Node} node - Node to be removed
116
- */
117
- removeNode(node: Node): void;
118
- /**
119
- * Remove edge from the graph and deletes the edge.
120
- *
121
- * @param {Edge} edge - Edge to be removed
122
- */
123
- removeEdge(edge: Edge): void;
124
- }