turf-polygon-dissolve.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 clone_1 = __importDefault(require("@turf/clone"));
  7. var helpers_1 = require("@turf/helpers");
  8. var invariant_1 = require("@turf/invariant");
  9. var meta_1 = require("@turf/meta");
  10. var topojson_client_1 = require("topojson-client");
  11. var topojson_server_1 = require("topojson-server");
  12. /**
  13. * Dissolves all overlapping (Multi)Polygon
  14. *
  15. * @param {FeatureCollection<Polygon|MultiPolygon>} geojson Polygons to dissolve
  16. * @param {Object} [options={}] Optional parameters
  17. * @param {boolean} [options.mutate=false] Prevent input mutation
  18. * @returns {Feature<Polygon|MultiPolygon>} Dissolved Polygons
  19. */
  20. function polygonDissolve(geojson, options) {
  21. if (options === void 0) { options = {}; }
  22. // Validation
  23. if (invariant_1.getType(geojson) !== "FeatureCollection") {
  24. throw new Error("geojson must be a FeatureCollection");
  25. }
  26. if (!geojson.features.length) {
  27. throw new Error("geojson is empty");
  28. }
  29. // Clone geojson to avoid side effects
  30. // Topojson modifies in place, so we need to deep clone first
  31. if (options.mutate === false || options.mutate === undefined) {
  32. geojson = clone_1.default(geojson);
  33. }
  34. var geoms = [];
  35. meta_1.flattenEach(geojson, function (feature) {
  36. geoms.push(feature.geometry);
  37. });
  38. var topo = topojson_server_1.topology({ geoms: helpers_1.geometryCollection(geoms).geometry });
  39. var merged = topojson_client_1.merge(topo, topo.objects.geoms.geometries);
  40. return merged;
  41. }
  42. exports.default = polygonDissolve;