index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. var 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. var holes = [], shells = [];
  30. graph
  31. .getEdgeRings()
  32. .filter(function (edgeRing) { return edgeRing.isValid(); })
  33. .forEach(function (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(function (hole) {
  41. if (EdgeRing.findEdgeRingContaining(hole, shells))
  42. shells.push(hole);
  43. });
  44. // 5. EdgeRings to Polygons
  45. return featureCollection(shells.map(function (shell) { return shell.toPolygon(); }));
  46. }