index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var helpers_1 = require("@turf/helpers");
  7. var Graph_1 = __importDefault(require("./lib/Graph"));
  8. var EdgeRing_1 = __importDefault(require("./lib/EdgeRing"));
  9. /**
  10. * Polygonizes {@link LineString|(Multi)LineString(s)} into {@link Polygons}.
  11. *
  12. * Implementation of GEOSPolygonize function (`geos::operation::polygonize::Polygonizer`).
  13. *
  14. * Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly
  15. * noded, i.e., they must only meet at their endpoints.
  16. *
  17. * The implementation correctly handles:
  18. *
  19. * - Dangles: edges which have one or both ends which are not incident on another edge endpoint.
  20. * - Cut Edges (bridges): edges that are connected at both ends but which do not form part of a polygon.
  21. *
  22. * @name polygonize
  23. * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geoJson Lines in order to polygonize
  24. * @returns {FeatureCollection<Polygon>} Polygons created
  25. * @throws {Error} if geoJson is invalid.
  26. */
  27. function polygonize(geoJson) {
  28. var graph = Graph_1.default.fromGeoJson(geoJson);
  29. // 1. Remove dangle node
  30. graph.deleteDangles();
  31. // 2. Remove cut-edges (bridge edges)
  32. graph.deleteCutEdges();
  33. // 3. Get all holes and shells
  34. var holes = [], shells = [];
  35. graph
  36. .getEdgeRings()
  37. .filter(function (edgeRing) { return edgeRing.isValid(); })
  38. .forEach(function (edgeRing) {
  39. if (edgeRing.isHole())
  40. holes.push(edgeRing);
  41. else
  42. shells.push(edgeRing);
  43. });
  44. // 4. Assign Holes to Shells
  45. holes.forEach(function (hole) {
  46. if (EdgeRing_1.default.findEdgeRingContaining(hole, shells))
  47. shells.push(hole);
  48. });
  49. // 5. EdgeRings to Polygons
  50. return helpers_1.featureCollection(shells.map(function (shell) { return shell.toPolygon(); }));
  51. }
  52. exports.default = polygonize;