@turf/polygonize 7.0.0-alpha.2 → 7.0.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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../index.ts","../../lib/util.ts","../../lib/Node.ts","../../lib/Edge.ts","../../lib/EdgeRing.ts","../../lib/Graph.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n LineString,\n MultiLineString,\n Polygon,\n} from \"geojson\";\nimport { featureCollection } from \"@turf/helpers\";\nimport { Graph } from \"./lib/Graph.js\";\nimport { EdgeRing } from \"./lib/EdgeRing.js\";\n\n/**\n * Polygonizes {@link LineString|(Multi)LineString(s)} into {@link Polygons}.\n *\n * Implementation of GEOSPolygonize function (`geos::operation::polygonize::Polygonizer`).\n *\n * Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly\n * noded, i.e., they must only meet at their endpoints.\n *\n * The implementation correctly handles:\n *\n * - Dangles: edges which have one or both ends which are not incident on another edge endpoint.\n * - Cut Edges (bridges): edges that are connected at both ends but which do not form part of a polygon.\n *\n * @name polygonize\n * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geoJson Lines in order to polygonize\n * @returns {FeatureCollection<Polygon>} Polygons created\n * @throws {Error} if geoJson is invalid.\n */\nfunction polygonize<T extends LineString | MultiLineString>(\n geoJson: Feature<T> | FeatureCollection<T> | T\n): FeatureCollection<Polygon> {\n const graph = Graph.fromGeoJson(geoJson);\n\n // 1. Remove dangle node\n graph.deleteDangles();\n\n // 2. Remove cut-edges (bridge edges)\n graph.deleteCutEdges();\n\n // 3. Get all holes and shells\n const holes: EdgeRing[] = [],\n shells: EdgeRing[] = [];\n\n graph\n .getEdgeRings()\n .filter((edgeRing) => edgeRing.isValid())\n .forEach((edgeRing) => {\n if (edgeRing.isHole()) holes.push(edgeRing);\n else shells.push(edgeRing);\n });\n\n // 4. Assign Holes to Shells\n holes.forEach((hole) => {\n if (EdgeRing.findEdgeRingContaining(hole, shells)) shells.push(hole);\n });\n\n // 5. EdgeRings to Polygons\n return featureCollection(shells.map((shell) => shell.toPolygon()));\n}\n\nexport { polygonize };\nexport default polygonize;\n","import { Feature, Polygon } from \"geojson\";\nimport { booleanPointInPolygon } from \"@turf/boolean-point-in-polygon\";\nimport { point } from \"@turf/helpers\";\n\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign#Polyfill\nfunction mathSign(x: number) {\n return ((x > 0) as unknown as number) - ((x < 0) as unknown as number) || +x;\n}\n\n/**\n * Returns the direction of the point q relative to the vector p1 -> p2.\n *\n * Implementation of geos::algorithm::CGAlgorithm::orientationIndex()\n * (same as geos::algorithm::CGAlgorithm::computeOrientation())\n *\n * @param {number[]} p1 - the origin point of the vector\n * @param {number[]} p2 - the final point of the vector\n * @param {number[]} q - the point to compute the direction to\n *\n * @returns {number} - 1 if q is ccw (left) from p1->p2,\n * -1 if q is cw (right) from p1->p2,\n * 0 if q is colinear with p1->p2\n */\nexport function orientationIndex(p1: number[], p2: number[], q: number[]) {\n const dx1 = p2[0] - p1[0],\n dy1 = p2[1] - p1[1],\n dx2 = q[0] - p2[0],\n dy2 = q[1] - p2[1];\n\n return mathSign(dx1 * dy2 - dx2 * dy1);\n}\n\n/**\n * Checks if two envelopes are equal.\n *\n * The function assumes that the arguments are envelopes, i.e.: Rectangular polygon\n *\n * @param {Feature<Polygon>} env1 - Envelope\n * @param {Feature<Polygon>} env2 - Envelope\n * @returns {boolean} - True if the envelopes are equal\n */\nexport function envelopeIsEqual(\n env1: Feature<Polygon>,\n env2: Feature<Polygon>\n) {\n const envX1 = env1.geometry.coordinates[0].map((c) => c[0]),\n envY1 = env1.geometry.coordinates[0].map((c) => c[1]),\n envX2 = env2.geometry.coordinates[0].map((c) => c[0]),\n envY2 = env2.geometry.coordinates[0].map((c) => c[1]);\n\n return (\n Math.max.apply(null, envX1) === Math.max.apply(null, envX2) &&\n Math.max.apply(null, envY1) === Math.max.apply(null, envY2) &&\n Math.min.apply(null, envX1) === Math.min.apply(null, envX2) &&\n Math.min.apply(null, envY1) === Math.min.apply(null, envY2)\n );\n}\n\n/**\n * Check if a envelope is contained in other one.\n *\n * The function assumes that the arguments are envelopes, i.e.: Convex polygon\n * XXX: Envelopes are rectangular, checking if a point is inside a rectangule is something easy,\n * this could be further improved.\n *\n * @param {Feature<Polygon>} self - Envelope\n * @param {Feature<Polygon>} env - Envelope\n * @returns {boolean} - True if env is contained in self\n */\nexport function envelopeContains(\n self: Feature<Polygon>,\n env: Feature<Polygon>\n) {\n return env.geometry.coordinates[0].every((c) =>\n booleanPointInPolygon(point(c), self)\n );\n}\n\n/**\n * Checks if two coordinates are equal.\n *\n * @param {number[]} coord1 - First coordinate\n * @param {number[]} coord2 - Second coordinate\n * @returns {boolean} - True if coordinates are equal\n */\nexport function coordinatesEqual(coord1: number[], coord2: number[]) {\n return coord1[0] === coord2[0] && coord1[1] === coord2[1];\n}\n","import { orientationIndex } from \"./util.js\";\nimport { Edge } from \"./Edge.js\";\n\n/**\n * Node\n */\nclass Node {\n static buildId(coordinates: number[]) {\n return coordinates.join(\",\");\n }\n\n public id: string;\n public coordinates: number[];\n public innerEdges: Edge[];\n private outerEdges: Edge[];\n private outerEdgesSorted: boolean;\n\n constructor(coordinates: number[]) {\n this.id = Node.buildId(coordinates);\n this.coordinates = coordinates; //< {Number[]}\n this.innerEdges = []; //< {Edge[]}\n\n // We wil store to (out) edges in an CCW order as geos::planargraph::DirectedEdgeStar does\n this.outerEdges = []; //< {Edge[]}\n this.outerEdgesSorted = false; //< {Boolean} flag that stores if the outer Edges had been sorted\n }\n\n removeInnerEdge(edge: Edge) {\n this.innerEdges = this.innerEdges.filter((e) => e.from.id !== edge.from.id);\n }\n\n removeOuterEdge(edge: Edge) {\n this.outerEdges = this.outerEdges.filter((e) => e.to.id !== edge.to.id);\n }\n\n /**\n * Outer edges are stored CCW order.\n *\n * @memberof Node\n * @param {Edge} edge - Edge to add as an outerEdge.\n */\n addOuterEdge(edge: Edge) {\n this.outerEdges.push(edge);\n this.outerEdgesSorted = false;\n }\n\n /**\n * Sorts outer edges in CCW way.\n *\n * @memberof Node\n * @private\n */\n sortOuterEdges() {\n if (!this.outerEdgesSorted) {\n //this.outerEdges.sort((a, b) => a.compareTo(b));\n // Using this comparator in order to be deterministic\n this.outerEdges.sort((a, b) => {\n const aNode = a.to,\n bNode = b.to;\n\n if (\n aNode.coordinates[0] - this.coordinates[0] >= 0 &&\n bNode.coordinates[0] - this.coordinates[0] < 0\n )\n return 1;\n if (\n aNode.coordinates[0] - this.coordinates[0] < 0 &&\n bNode.coordinates[0] - this.coordinates[0] >= 0\n )\n return -1;\n\n if (\n aNode.coordinates[0] - this.coordinates[0] === 0 &&\n bNode.coordinates[0] - this.coordinates[0] === 0\n ) {\n if (\n aNode.coordinates[1] - this.coordinates[1] >= 0 ||\n bNode.coordinates[1] - this.coordinates[1] >= 0\n )\n return aNode.coordinates[1] - bNode.coordinates[1];\n return bNode.coordinates[1] - aNode.coordinates[1];\n }\n\n const det = orientationIndex(\n this.coordinates,\n aNode.coordinates,\n bNode.coordinates\n );\n if (det < 0) return 1;\n if (det > 0) return -1;\n\n const d1 =\n Math.pow(aNode.coordinates[0] - this.coordinates[0], 2) +\n Math.pow(aNode.coordinates[1] - this.coordinates[1], 2),\n d2 =\n Math.pow(bNode.coordinates[0] - this.coordinates[0], 2) +\n Math.pow(bNode.coordinates[1] - this.coordinates[1], 2);\n\n return d1 - d2;\n });\n this.outerEdgesSorted = true;\n }\n }\n\n /**\n * Retrieves outer edges.\n *\n * They are sorted if they aren't in the CCW order.\n *\n * @memberof Node\n * @returns {Edge[]} - List of outer edges sorted in a CCW order.\n */\n getOuterEdges() {\n this.sortOuterEdges();\n return this.outerEdges;\n }\n\n getOuterEdge(i: number) {\n this.sortOuterEdges();\n return this.outerEdges[i];\n }\n\n addInnerEdge(edge: Edge) {\n this.innerEdges.push(edge);\n }\n}\n\nexport { Node };\nexport default Node;\n","import { lineString } from \"@turf/helpers\";\nimport { orientationIndex } from \"./util.js\";\nimport { Node } from \"./Node.js\";\nimport { EdgeRing } from \"./EdgeRing.js\";\n\n/**\n * This class is inspired by GEOS's geos::operation::polygonize::PolygonizeDirectedEdge\n */\nclass Edge {\n public label?: number;\n public symetric?: Edge;\n public from: Node;\n public to: Node;\n public next?: Edge;\n public ring?: EdgeRing;\n\n /**\n * Creates or get the symetric Edge.\n *\n * @returns {Edge} - Symetric Edge.\n */\n getSymetric() {\n if (!this.symetric) {\n this.symetric = new Edge(this.to, this.from);\n this.symetric.symetric = this;\n }\n\n return this.symetric;\n }\n\n /**\n * @param {Node} from - start node of the Edge\n * @param {Node} to - end node of the edge\n */\n constructor(from: Node, to: Node) {\n this.from = from; //< start\n this.to = to; //< End\n\n this.next = undefined; //< The edge to be computed after\n this.label = undefined; //< Used in order to detect Cut Edges (Bridges)\n this.symetric = undefined; //< The symetric edge of this\n this.ring = undefined; //< EdgeRing in which the Edge is\n\n this.from.addOuterEdge(this);\n this.to.addInnerEdge(this);\n }\n\n /**\n * Removes edge from from and to nodes.\n */\n deleteEdge() {\n this.from.removeOuterEdge(this);\n this.to.removeInnerEdge(this);\n }\n\n /**\n * Compares Edge equallity.\n *\n * An edge is equal to another, if the from and to nodes are the same.\n *\n * @param {Edge} edge - Another Edge\n * @returns {boolean} - True if Edges are equal, False otherwise\n */\n isEqual(edge: Edge) {\n return this.from.id === edge.from.id && this.to.id === edge.to.id;\n }\n\n toString() {\n return `Edge { ${this.from.id} -> ${this.to.id} }`;\n }\n\n /**\n * Returns a LineString representation of the Edge\n *\n * @returns {Feature<LineString>} - LineString representation of the Edge\n */\n toLineString() {\n return lineString([this.from.coordinates, this.to.coordinates]);\n }\n\n /**\n * Comparator of two edges.\n *\n * Implementation of geos::planargraph::DirectedEdge::compareTo.\n *\n * @param {Edge} edge - Another edge to compare with this one\n * @returns {number} -1 if this Edge has a greater angle with the positive x-axis than b,\n * 0 if the Edges are colinear,\n * 1 otherwise\n */\n compareTo(edge: Edge) {\n return orientationIndex(\n edge.from.coordinates,\n edge.to.coordinates,\n this.to.coordinates\n );\n }\n}\n\nexport { Edge };\nexport default Edge;\n","import { Polygon, Feature, Point, Position } from \"geojson\";\nimport {\n orientationIndex,\n envelopeIsEqual,\n envelopeContains,\n coordinatesEqual,\n} from \"./util.js\";\nimport { multiPoint, polygon, point } from \"@turf/helpers\";\nimport { envelope } from \"@turf/envelope\";\nimport { booleanPointInPolygon } from \"@turf/boolean-point-in-polygon\";\nimport { Edge } from \"./Edge.js\";\n\n/**\n * Ring of edges which form a polygon.\n *\n * The ring may be either an outer shell or a hole.\n *\n * This class is inspired in GEOS's geos::operation::polygonize::EdgeRing\n */\nclass EdgeRing {\n private edges: Edge[];\n private polygon?: Feature<\n Polygon,\n {\n [name: string]: any;\n }\n >;\n private envelope?: Feature<\n Polygon,\n {\n [name: string]: any;\n }\n >;\n\n constructor() {\n this.edges = [];\n this.polygon = undefined; //< Caches Polygon representation\n this.envelope = undefined; //< Caches Envelope representation\n }\n\n /**\n * Add an edge to the ring, inserting it in the last position.\n *\n * @memberof EdgeRing\n * @param {Edge} edge - Edge to be inserted\n */\n push(edge: Edge) {\n this.edges.push(edge);\n this.polygon = this.envelope = undefined;\n }\n\n /**\n * Get Edge.\n *\n * @memberof EdgeRing\n * @param {number} i - Index\n * @returns {Edge} - Edge in the i position\n */\n get(i: number) {\n return this.edges[i];\n }\n\n /**\n * Getter of length property.\n *\n * @memberof EdgeRing\n * @returns {number} - Length of the edge ring.\n */\n get length() {\n return this.edges.length;\n }\n\n /**\n * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.\n *\n * @memberof EdgeRing\n * @param {Function} f - The same function to be passed to Array.prototype.forEach\n */\n forEach(f: (edge: Edge, index: number, array: Edge[]) => void) {\n this.edges.forEach(f);\n }\n\n /**\n * Similar to Array.prototype.map for the list of Edges in the EdgeRing.\n *\n * @memberof EdgeRing\n * @param {Function} f - The same function to be passed to Array.prototype.map\n * @returns {Array} - The mapped values in the function\n */\n map<T>(f: (edge: Edge, index: number, array: Edge[]) => T): T[] {\n return this.edges.map(f);\n }\n\n /**\n * Similar to Array.prototype.some for the list of Edges in the EdgeRing.\n *\n * @memberof EdgeRing\n * @param {Function} f - The same function to be passed to Array.prototype.some\n * @returns {boolean} - True if an Edge check the condition\n */\n some(f: (edge: Edge, index: number, array: Edge[]) => boolean) {\n return this.edges.some(f);\n }\n\n /**\n * Check if the ring is valid in geomtry terms.\n *\n * A ring must have either 0 or 4 or more points. The first and the last must be\n * equal (in 2D)\n * geos::geom::LinearRing::validateConstruction\n *\n * @memberof EdgeRing\n * @returns {boolean} - Validity of the EdgeRing\n */\n isValid() {\n // TODO: stub\n return true;\n }\n\n /**\n * Tests whether this ring is a hole.\n *\n * A ring is a hole if it is oriented counter-clockwise.\n * Similar implementation of geos::algorithm::CGAlgorithms::isCCW\n *\n * @memberof EdgeRing\n * @returns {boolean} - true: if it is a hole\n */\n isHole() {\n // XXX: Assuming Ring is valid\n // Find highest point\n const hiIndex = this.edges.reduce((high, edge, i) => {\n if (edge.from.coordinates[1] > this.edges[high].from.coordinates[1])\n high = i;\n return high;\n }, 0),\n iPrev = (hiIndex === 0 ? this.length : hiIndex) - 1,\n iNext = (hiIndex + 1) % this.length,\n disc = orientationIndex(\n this.edges[iPrev].from.coordinates,\n this.edges[hiIndex].from.coordinates,\n this.edges[iNext].from.coordinates\n );\n\n if (disc === 0)\n return (\n this.edges[iPrev].from.coordinates[0] >\n this.edges[iNext].from.coordinates[0]\n );\n return disc > 0;\n }\n\n /**\n * Creates a MultiPoint representing the EdgeRing (discarts edges directions).\n *\n * @memberof EdgeRing\n * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing\n */\n toMultiPoint() {\n return multiPoint(this.edges.map((edge) => edge.from.coordinates));\n }\n\n /**\n * Creates a Polygon representing the EdgeRing.\n *\n * @memberof EdgeRing\n * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring\n */\n toPolygon() {\n if (this.polygon) return this.polygon;\n const coordinates = this.edges.map((edge) => edge.from.coordinates);\n coordinates.push(this.edges[0].from.coordinates);\n return (this.polygon = polygon([coordinates]));\n }\n\n /**\n * Calculates the envelope of the EdgeRing.\n *\n * @memberof EdgeRing\n * @returns {Feature<Polygon>} - envelope\n */\n getEnvelope() {\n if (this.envelope) return this.envelope;\n return (this.envelope = envelope(this.toPolygon()) as Feature<\n Polygon,\n { [name: string]: any }\n >);\n }\n\n /**\n * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`\n *\n * @param {EdgeRing} testEdgeRing - EdgeRing to look in the list\n * @param {EdgeRing[]} shellList - List of EdgeRing in which to search\n *\n * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing\n */\n static findEdgeRingContaining(\n testEdgeRing: EdgeRing,\n shellList: EdgeRing[]\n ): EdgeRing | undefined {\n const testEnvelope = testEdgeRing.getEnvelope();\n\n let minEnvelope: Feature<Polygon>, minShell: EdgeRing | undefined;\n shellList.forEach((shell) => {\n const tryEnvelope = shell.getEnvelope();\n\n if (minShell) minEnvelope = minShell.getEnvelope();\n\n // the hole envelope cannot equal the shell envelope\n if (envelopeIsEqual(tryEnvelope, testEnvelope)) return;\n\n if (envelopeContains(tryEnvelope, testEnvelope)) {\n const testEdgeRingCoordinates = testEdgeRing.map(\n (edge) => edge.from.coordinates\n );\n\n let testPoint: Position | undefined;\n for (const pt of testEdgeRingCoordinates) {\n if (\n !shell.some((edge) => coordinatesEqual(pt, edge.from.coordinates))\n ) {\n testPoint = pt;\n }\n }\n\n if (testPoint && shell.inside(point(testPoint))) {\n if (!minShell || envelopeContains(minEnvelope, tryEnvelope))\n minShell = shell;\n }\n }\n });\n\n return minShell;\n }\n\n /**\n * Checks if the point is inside the edgeRing\n *\n * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing\n * @returns {boolean} - True if it is inside, False otherwise\n */\n inside(pt: Feature<Point>) {\n return booleanPointInPolygon(pt, this.toPolygon());\n }\n}\n\nexport { EdgeRing };\nexport default EdgeRing;\n","import { Node } from \"./Node.js\";\nimport { Edge } from \"./Edge.js\";\nimport { EdgeRing } from \"./EdgeRing.js\";\nimport { flattenEach, coordReduce } from \"@turf/meta\";\nimport { featureOf } from \"@turf/invariant\";\nimport {\n FeatureCollection,\n LineString,\n MultiLineString,\n Feature,\n} from \"geojson\";\nimport { AllGeoJSON } from \"@turf/helpers\";\n\n/**\n * Validates the geoJson.\n *\n * @param {GeoJSON} geoJson - input geoJson.\n * @throws {Error} if geoJson is invalid.\n */\nfunction validateGeoJson(geoJson: AllGeoJSON) {\n if (!geoJson) throw new Error(\"No geojson passed\");\n\n if (\n geoJson.type !== \"FeatureCollection\" &&\n geoJson.type !== \"GeometryCollection\" &&\n geoJson.type !== \"MultiLineString\" &&\n geoJson.type !== \"LineString\" &&\n geoJson.type !== \"Feature\"\n )\n throw new Error(\n `Invalid input type '${geoJson.type}'. Geojson must be FeatureCollection, GeometryCollection, LineString, MultiLineString or Feature`\n );\n}\n\n/**\n * Represents a planar graph of edges and nodes that can be used to compute a polygonization.\n *\n * Although, this class is inspired by GEOS's `geos::operation::polygonize::PolygonizeGraph`,\n * it isn't a rewrite. As regards algorithm, this class implements the same logic, but it\n * isn't a javascript transcription of the C++ source.\n *\n * This graph is directed (both directions are created)\n */\nclass Graph {\n private nodes: { [id: string]: Node };\n private edges: Edge[];\n\n /**\n * Creates a graph from a GeoJSON.\n *\n * @param {FeatureCollection<LineString>} geoJson - it must comply with the restrictions detailed in the index\n * @returns {Graph} - The newly created graph\n * @throws {Error} if geoJson is invalid.\n */\n static fromGeoJson(\n geoJson:\n | FeatureCollection<LineString | MultiLineString>\n | LineString\n | MultiLineString\n | Feature<LineString | MultiLineString>\n ) {\n validateGeoJson(geoJson);\n\n const graph = new Graph();\n flattenEach(geoJson, (feature) => {\n featureOf(feature, \"LineString\", \"Graph::fromGeoJson\");\n // When a LineString if formed by many segments, split them\n coordReduce<number[]>(feature, (prev, cur) => {\n if (prev) {\n const start = graph.getNode(prev),\n end = graph.getNode(cur);\n\n graph.addEdge(start, end);\n }\n return cur;\n });\n });\n\n return graph;\n }\n\n /**\n * Creates or get a Node.\n *\n * @param {number[]} coordinates - Coordinates of the node\n * @returns {Node} - The created or stored node\n */\n getNode(coordinates: number[]) {\n const id = Node.buildId(coordinates);\n let node = this.nodes[id];\n if (!node) node = this.nodes[id] = new Node(coordinates);\n\n return node;\n }\n\n /**\n * Adds an Edge and its symetricall.\n *\n * Edges are added symetrically, i.e.: we also add its symetric\n *\n * @param {Node} from - Node which starts the Edge\n * @param {Node} to - Node which ends the Edge\n */\n addEdge(from: Node, to: Node) {\n const edge = new Edge(from, to),\n symetricEdge = edge.getSymetric();\n\n this.edges.push(edge);\n this.edges.push(symetricEdge);\n }\n\n constructor() {\n this.edges = []; //< {Edge[]} dirEdges\n\n // The key is the `id` of the Node (ie: coordinates.join(','))\n this.nodes = {};\n }\n\n /**\n * Removes Dangle Nodes (nodes with grade 1).\n */\n deleteDangles() {\n Object.keys(this.nodes)\n .map((id) => this.nodes[id])\n .forEach((node) => this._removeIfDangle(node));\n }\n\n /**\n * Check if node is dangle, if so, remove it.\n *\n * It calls itself recursively, removing a dangling node might cause another dangling node\n *\n * @param {Node} node - Node to check if it's a dangle\n */\n _removeIfDangle(node: Node) {\n // As edges are directed and symetrical, we count only innerEdges\n if (node.innerEdges.length <= 1) {\n const outerNodes = node.getOuterEdges().map((e) => e.to);\n this.removeNode(node);\n outerNodes.forEach((n) => this._removeIfDangle(n));\n }\n }\n\n /**\n * Delete cut-edges (bridge edges).\n *\n * The graph will be traversed, all the edges will be labeled according the ring\n * in which they are. (The label is a number incremented by 1). Edges with the same\n * label are cut-edges.\n */\n deleteCutEdges() {\n this._computeNextCWEdges();\n this._findLabeledEdgeRings();\n\n // Cut-edges (bridges) are edges where both edges have the same label\n this.edges.forEach((edge) => {\n if (edge.label === edge.symetric!.label) {\n this.removeEdge(edge.symetric!);\n this.removeEdge(edge);\n }\n });\n }\n\n /**\n * Set the `next` property of each Edge.\n *\n * The graph will be transversed in a CW form, so, we set the next of the symetrical edge as the previous one.\n * OuterEdges are sorted CCW.\n *\n * @param {Node} [node] - If no node is passed, the function calls itself for every node in the Graph\n */\n _computeNextCWEdges(node?: Node) {\n if (typeof node === \"undefined\") {\n Object.keys(this.nodes).forEach((id) =>\n this._computeNextCWEdges(this.nodes[id])\n );\n } else {\n node.getOuterEdges().forEach((edge, i) => {\n node.getOuterEdge(\n (i === 0 ? node.getOuterEdges().length : i) - 1\n ).symetric!.next = edge;\n });\n }\n }\n\n /**\n * Computes the next edge pointers going CCW around the given node, for the given edgering label.\n *\n * This algorithm has the effect of converting maximal edgerings into minimal edgerings\n *\n * XXX: method literally transcribed from `geos::operation::polygonize::PolygonizeGraph::computeNextCCWEdges`,\n * could be written in a more javascript way.\n *\n * @param {Node} node - Node\n * @param {number} label - Ring's label\n */\n _computeNextCCWEdges(node: Node, label: number) {\n const edges = node.getOuterEdges();\n let firstOutDE, prevInDE;\n\n for (let i = edges.length - 1; i >= 0; --i) {\n let de = edges[i],\n sym = de.symetric,\n outDE,\n inDE;\n\n if (de.label === label) outDE = de;\n\n if (sym!.label === label) inDE = sym;\n\n if (!outDE || !inDE)\n // This edge is not in edgering\n continue;\n\n if (inDE) prevInDE = inDE;\n\n if (outDE) {\n if (prevInDE) {\n prevInDE.next = outDE;\n prevInDE = undefined;\n }\n\n if (!firstOutDE) firstOutDE = outDE;\n }\n }\n\n if (prevInDE) prevInDE.next = firstOutDE;\n }\n\n /**\n * Finds rings and labels edges according to which rings are.\n *\n * The label is a number which is increased for each ring.\n *\n * @returns {Edge[]} edges that start rings\n */\n _findLabeledEdgeRings() {\n const edgeRingStarts: Edge[] = [];\n let label = 0;\n this.edges.forEach((edge) => {\n if (edge.label! >= 0) return;\n\n edgeRingStarts.push(edge);\n\n let e = edge;\n do {\n e.label = label;\n e = e.next!;\n } while (!edge.isEqual(e));\n\n label++;\n });\n\n return edgeRingStarts;\n }\n\n /**\n * Computes the EdgeRings formed by the edges in this graph.\n *\n * @returns {EdgeRing[]} - A list of all the EdgeRings in the graph.\n */\n getEdgeRings() {\n this._computeNextCWEdges();\n\n // Clear labels\n this.edges.forEach((edge) => {\n edge.label = undefined;\n });\n\n this._findLabeledEdgeRings().forEach((edge) => {\n // convertMaximalToMinimalEdgeRings\n this._findIntersectionNodes(edge).forEach((node) => {\n this._computeNextCCWEdges(node, edge.label!);\n });\n });\n\n const edgeRingList: EdgeRing[] = [];\n\n // find all edgerings\n this.edges.forEach((edge) => {\n if (edge.ring) return;\n edgeRingList.push(this._findEdgeRing(edge));\n });\n\n return edgeRingList;\n }\n\n /**\n * Find all nodes in a Maxima EdgeRing which are self-intersection nodes.\n *\n * @param {Node} startEdge - Start Edge of the Ring\n * @returns {Node[]} - intersection nodes\n */\n _findIntersectionNodes(startEdge: Edge) {\n const intersectionNodes = [];\n let edge = startEdge;\n do {\n // getDegree\n let degree = 0;\n edge.from.getOuterEdges().forEach((e) => {\n if (e.label === startEdge.label) ++degree;\n });\n\n if (degree > 1) intersectionNodes.push(edge.from);\n\n edge = edge.next!;\n } while (!startEdge.isEqual(edge));\n\n return intersectionNodes;\n }\n\n /**\n * Get the edge-ring which starts from the provided Edge.\n *\n * @param {Edge} startEdge - starting edge of the edge ring\n * @returns {EdgeRing} - EdgeRing which start Edge is the provided one.\n */\n _findEdgeRing(startEdge: Edge) {\n let edge = startEdge;\n const edgeRing = new EdgeRing();\n\n do {\n edgeRing.push(edge);\n edge.ring = edgeRing;\n edge = edge.next!;\n } while (!startEdge.isEqual(edge));\n\n return edgeRing;\n }\n\n /**\n * Removes a node from the Graph.\n *\n * It also removes edges asociated to that node\n * @param {Node} node - Node to be removed\n */\n removeNode(node: Node) {\n node.getOuterEdges().forEach((edge) => this.removeEdge(edge));\n node.innerEdges.forEach((edge) => this.removeEdge(edge));\n delete this.nodes[node.id];\n }\n\n /**\n * Remove edge from the graph and deletes the edge.\n *\n * @param {Edge} edge - Edge to be removed\n */\n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,6 @@
1
1
  {
2
2
  "name": "@turf/polygonize",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.0.0",
4
4
  "description": "turf polygonize module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -26,48 +26,54 @@
26
26
  "gis",
27
27
  "polygonize"
28
28
  ],
29
- "main": "dist/js/index.js",
30
- "module": "dist/es/index.js",
29
+ "type": "module",
30
+ "main": "dist/cjs/index.cjs",
31
+ "module": "dist/esm/index.js",
32
+ "types": "dist/esm/index.d.ts",
31
33
  "exports": {
32
34
  "./package.json": "./package.json",
33
35
  ".": {
34
- "types": "./dist/js/index.d.ts",
35
- "import": "./dist/es/index.js",
36
- "require": "./dist/js/index.js"
36
+ "import": {
37
+ "types": "./dist/esm/index.d.ts",
38
+ "default": "./dist/esm/index.js"
39
+ },
40
+ "require": {
41
+ "types": "./dist/cjs/index.d.cts",
42
+ "default": "./dist/cjs/index.cjs"
43
+ }
37
44
  }
38
45
  },
39
- "types": "dist/js/index.d.ts",
40
46
  "sideEffects": false,
41
47
  "files": [
42
48
  "dist"
43
49
  ],
44
50
  "scripts": {
45
- "bench": "tsx bench.js",
46
- "build": "npm-run-all build:*",
47
- "build:es": "tsc --outDir dist/es --module esnext --declaration false && echo '{\"type\":\"module\"}' > dist/es/package.json",
48
- "build:js": "tsc",
49
- "docs": "tsx ../../scripts/generate-readmes",
50
- "test": "npm-run-all test:*",
51
- "test:tape": "tsx test.js",
52
- "test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
51
+ "bench": "tsx bench.ts",
52
+ "build": "tsup --config ../../tsup.config.ts",
53
+ "docs": "tsx ../../scripts/generate-readmes.ts",
54
+ "test": "npm-run-all --npm-path npm test:*",
55
+ "test:tape": "tsx test.ts",
56
+ "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
53
57
  },
54
58
  "devDependencies": {
55
- "benchmark": "*",
56
- "load-json-file": "*",
57
- "npm-run-all": "*",
58
- "rollup": "*",
59
- "tape": "*",
60
- "tsx": "*",
61
- "typescript": "*",
62
- "write-json-file": "*"
59
+ "@types/benchmark": "^2.1.5",
60
+ "@types/tape": "^4.2.32",
61
+ "benchmark": "^2.1.4",
62
+ "load-json-file": "^7.0.1",
63
+ "npm-run-all": "^4.1.5",
64
+ "tape": "^5.7.2",
65
+ "tsup": "^8.0.1",
66
+ "tsx": "^4.6.2",
67
+ "typescript": "^5.2.2",
68
+ "write-json-file": "^5.0.0"
63
69
  },
64
70
  "dependencies": {
65
- "@turf/boolean-point-in-polygon": "^7.0.0-alpha.2",
66
- "@turf/envelope": "^7.0.0-alpha.2",
67
- "@turf/helpers": "^7.0.0-alpha.2",
68
- "@turf/invariant": "^7.0.0-alpha.2",
69
- "@turf/meta": "^7.0.0-alpha.2",
70
- "tslib": "^2.3.0"
71
+ "@turf/boolean-point-in-polygon": "^7.0.0",
72
+ "@turf/envelope": "^7.0.0",
73
+ "@turf/helpers": "^7.0.0",
74
+ "@turf/invariant": "^7.0.0",
75
+ "@turf/meta": "^7.0.0",
76
+ "tslib": "^2.6.2"
71
77
  },
72
- "gitHead": "dd35b52725945b4fa29a98d9a550733e06cc222e"
78
+ "gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
73
79
  }
package/dist/es/index.js DELETED
@@ -1,46 +0,0 @@
1
- import { featureCollection } from "@turf/helpers";
2
- import Graph from "./lib/Graph.js";
3
- import EdgeRing from "./lib/EdgeRing.js";
4
- /**
5
- * Polygonizes {@link LineString|(Multi)LineString(s)} into {@link Polygons}.
6
- *
7
- * Implementation of GEOSPolygonize function (`geos::operation::polygonize::Polygonizer`).
8
- *
9
- * Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly
10
- * noded, i.e., they must only meet at their endpoints.
11
- *
12
- * The implementation correctly handles:
13
- *
14
- * - Dangles: edges which have one or both ends which are not incident on another edge endpoint.
15
- * - Cut Edges (bridges): edges that are connected at both ends but which do not form part of a polygon.
16
- *
17
- * @name polygonize
18
- * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geoJson Lines in order to polygonize
19
- * @returns {FeatureCollection<Polygon>} Polygons created
20
- * @throws {Error} if geoJson is invalid.
21
- */
22
- export default function polygonize(geoJson) {
23
- const graph = Graph.fromGeoJson(geoJson);
24
- // 1. Remove dangle node
25
- graph.deleteDangles();
26
- // 2. Remove cut-edges (bridge edges)
27
- graph.deleteCutEdges();
28
- // 3. Get all holes and shells
29
- const holes = [], shells = [];
30
- graph
31
- .getEdgeRings()
32
- .filter((edgeRing) => edgeRing.isValid())
33
- .forEach((edgeRing) => {
34
- if (edgeRing.isHole())
35
- holes.push(edgeRing);
36
- else
37
- shells.push(edgeRing);
38
- });
39
- // 4. Assign Holes to Shells
40
- holes.forEach((hole) => {
41
- if (EdgeRing.findEdgeRingContaining(hole, shells))
42
- shells.push(hole);
43
- });
44
- // 5. EdgeRings to Polygons
45
- return featureCollection(shells.map((shell) => shell.toPolygon()));
46
- }
@@ -1,75 +0,0 @@
1
- import { lineString } from "@turf/helpers";
2
- import { orientationIndex } from "./util.js";
3
- /**
4
- * This class is inspired by GEOS's geos::operation::polygonize::PolygonizeDirectedEdge
5
- */
6
- export default class Edge {
7
- /**
8
- * @param {Node} from - start node of the Edge
9
- * @param {Node} to - end node of the edge
10
- */
11
- constructor(from, to) {
12
- this.from = from; //< start
13
- this.to = to; //< End
14
- this.next = undefined; //< The edge to be computed after
15
- this.label = undefined; //< Used in order to detect Cut Edges (Bridges)
16
- this.symetric = undefined; //< The symetric edge of this
17
- this.ring = undefined; //< EdgeRing in which the Edge is
18
- this.from.addOuterEdge(this);
19
- this.to.addInnerEdge(this);
20
- }
21
- /**
22
- * Creates or get the symetric Edge.
23
- *
24
- * @returns {Edge} - Symetric Edge.
25
- */
26
- getSymetric() {
27
- if (!this.symetric) {
28
- this.symetric = new Edge(this.to, this.from);
29
- this.symetric.symetric = this;
30
- }
31
- return this.symetric;
32
- }
33
- /**
34
- * Removes edge from from and to nodes.
35
- */
36
- deleteEdge() {
37
- this.from.removeOuterEdge(this);
38
- this.to.removeInnerEdge(this);
39
- }
40
- /**
41
- * Compares Edge equallity.
42
- *
43
- * An edge is equal to another, if the from and to nodes are the same.
44
- *
45
- * @param {Edge} edge - Another Edge
46
- * @returns {boolean} - True if Edges are equal, False otherwise
47
- */
48
- isEqual(edge) {
49
- return this.from.id === edge.from.id && this.to.id === edge.to.id;
50
- }
51
- toString() {
52
- return `Edge { ${this.from.id} -> ${this.to.id} }`;
53
- }
54
- /**
55
- * Returns a LineString representation of the Edge
56
- *
57
- * @returns {Feature<LineString>} - LineString representation of the Edge
58
- */
59
- toLineString() {
60
- return lineString([this.from.coordinates, this.to.coordinates]);
61
- }
62
- /**
63
- * Comparator of two edges.
64
- *
65
- * Implementation of geos::planargraph::DirectedEdge::compareTo.
66
- *
67
- * @param {Edge} edge - Another edge to compare with this one
68
- * @returns {number} -1 if this Edge has a greater angle with the positive x-axis than b,
69
- * 0 if the Edges are colinear,
70
- * 1 otherwise
71
- */
72
- compareTo(edge) {
73
- return orientationIndex(edge.from.coordinates, edge.to.coordinates, this.to.coordinates);
74
- }
75
- }
@@ -1,188 +0,0 @@
1
- import { orientationIndex, envelopeIsEqual, envelopeContains, coordinatesEqual, } from "./util.js";
2
- import { multiPoint, polygon, point } from "@turf/helpers";
3
- import envelope from "@turf/envelope";
4
- import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
5
- /**
6
- * Ring of edges which form a polygon.
7
- *
8
- * The ring may be either an outer shell or a hole.
9
- *
10
- * This class is inspired in GEOS's geos::operation::polygonize::EdgeRing
11
- */
12
- export default class EdgeRing {
13
- constructor() {
14
- this.edges = [];
15
- this.polygon = undefined; //< Caches Polygon representation
16
- this.envelope = undefined; //< Caches Envelope representation
17
- }
18
- /**
19
- * Add an edge to the ring, inserting it in the last position.
20
- *
21
- * @memberof EdgeRing
22
- * @param {Edge} edge - Edge to be inserted
23
- */
24
- push(edge) {
25
- this.edges.push(edge);
26
- this.polygon = this.envelope = undefined;
27
- }
28
- /**
29
- * Get Edge.
30
- *
31
- * @memberof EdgeRing
32
- * @param {number} i - Index
33
- * @returns {Edge} - Edge in the i position
34
- */
35
- get(i) {
36
- return this.edges[i];
37
- }
38
- /**
39
- * Getter of length property.
40
- *
41
- * @memberof EdgeRing
42
- * @returns {number} - Length of the edge ring.
43
- */
44
- get length() {
45
- return this.edges.length;
46
- }
47
- /**
48
- * Similar to Array.prototype.forEach for the list of Edges in the EdgeRing.
49
- *
50
- * @memberof EdgeRing
51
- * @param {Function} f - The same function to be passed to Array.prototype.forEach
52
- */
53
- forEach(f) {
54
- this.edges.forEach(f);
55
- }
56
- /**
57
- * Similar to Array.prototype.map for the list of Edges in the EdgeRing.
58
- *
59
- * @memberof EdgeRing
60
- * @param {Function} f - The same function to be passed to Array.prototype.map
61
- * @returns {Array} - The mapped values in the function
62
- */
63
- map(f) {
64
- return this.edges.map(f);
65
- }
66
- /**
67
- * Similar to Array.prototype.some for the list of Edges in the EdgeRing.
68
- *
69
- * @memberof EdgeRing
70
- * @param {Function} f - The same function to be passed to Array.prototype.some
71
- * @returns {boolean} - True if an Edge check the condition
72
- */
73
- some(f) {
74
- return this.edges.some(f);
75
- }
76
- /**
77
- * Check if the ring is valid in geomtry terms.
78
- *
79
- * A ring must have either 0 or 4 or more points. The first and the last must be
80
- * equal (in 2D)
81
- * geos::geom::LinearRing::validateConstruction
82
- *
83
- * @memberof EdgeRing
84
- * @returns {boolean} - Validity of the EdgeRing
85
- */
86
- isValid() {
87
- // TODO: stub
88
- return true;
89
- }
90
- /**
91
- * Tests whether this ring is a hole.
92
- *
93
- * A ring is a hole if it is oriented counter-clockwise.
94
- * Similar implementation of geos::algorithm::CGAlgorithms::isCCW
95
- *
96
- * @memberof EdgeRing
97
- * @returns {boolean} - true: if it is a hole
98
- */
99
- isHole() {
100
- // XXX: Assuming Ring is valid
101
- // Find highest point
102
- const hiIndex = this.edges.reduce((high, edge, i) => {
103
- if (edge.from.coordinates[1] > this.edges[high].from.coordinates[1])
104
- high = i;
105
- return high;
106
- }, 0), iPrev = (hiIndex === 0 ? this.length : hiIndex) - 1, iNext = (hiIndex + 1) % this.length, disc = orientationIndex(this.edges[iPrev].from.coordinates, this.edges[hiIndex].from.coordinates, this.edges[iNext].from.coordinates);
107
- if (disc === 0)
108
- return (this.edges[iPrev].from.coordinates[0] >
109
- this.edges[iNext].from.coordinates[0]);
110
- return disc > 0;
111
- }
112
- /**
113
- * Creates a MultiPoint representing the EdgeRing (discarts edges directions).
114
- *
115
- * @memberof EdgeRing
116
- * @returns {Feature<MultiPoint>} - Multipoint representation of the EdgeRing
117
- */
118
- toMultiPoint() {
119
- return multiPoint(this.edges.map((edge) => edge.from.coordinates));
120
- }
121
- /**
122
- * Creates a Polygon representing the EdgeRing.
123
- *
124
- * @memberof EdgeRing
125
- * @returns {Feature<Polygon>} - Polygon representation of the Edge Ring
126
- */
127
- toPolygon() {
128
- if (this.polygon)
129
- return this.polygon;
130
- const coordinates = this.edges.map((edge) => edge.from.coordinates);
131
- coordinates.push(this.edges[0].from.coordinates);
132
- return (this.polygon = polygon([coordinates]));
133
- }
134
- /**
135
- * Calculates the envelope of the EdgeRing.
136
- *
137
- * @memberof EdgeRing
138
- * @returns {Feature<Polygon>} - envelope
139
- */
140
- getEnvelope() {
141
- if (this.envelope)
142
- return this.envelope;
143
- return (this.envelope = envelope(this.toPolygon()));
144
- }
145
- /**
146
- * `geos::operation::polygonize::EdgeRing::findEdgeRingContaining`
147
- *
148
- * @param {EdgeRing} testEdgeRing - EdgeRing to look in the list
149
- * @param {EdgeRing[]} shellList - List of EdgeRing in which to search
150
- *
151
- * @returns {EdgeRing} - EdgeRing which contains the testEdgeRing
152
- */
153
- static findEdgeRingContaining(testEdgeRing, shellList) {
154
- const testEnvelope = testEdgeRing.getEnvelope();
155
- let minEnvelope, minShell;
156
- shellList.forEach((shell) => {
157
- const tryEnvelope = shell.getEnvelope();
158
- if (minShell)
159
- minEnvelope = minShell.getEnvelope();
160
- // the hole envelope cannot equal the shell envelope
161
- if (envelopeIsEqual(tryEnvelope, testEnvelope))
162
- return;
163
- if (envelopeContains(tryEnvelope, testEnvelope)) {
164
- const testEdgeRingCoordinates = testEdgeRing.map((edge) => edge.from.coordinates);
165
- let testPoint;
166
- for (const pt of testEdgeRingCoordinates) {
167
- if (!shell.some((edge) => coordinatesEqual(pt, edge.from.coordinates))) {
168
- testPoint = pt;
169
- }
170
- }
171
- if (testPoint && shell.inside(point(testPoint))) {
172
- if (!minShell || envelopeContains(minEnvelope, tryEnvelope))
173
- minShell = shell;
174
- }
175
- }
176
- });
177
- return minShell;
178
- }
179
- /**
180
- * Checks if the point is inside the edgeRing
181
- *
182
- * @param {Feature<Point>} pt - Point to check if it is inside the edgeRing
183
- * @returns {boolean} - True if it is inside, False otherwise
184
- */
185
- inside(pt) {
186
- return booleanPointInPolygon(pt, this.toPolygon());
187
- }
188
- }