turf-dissolve.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 turf_line_dissolve_1 = __importDefault(require("./turf-line-dissolve"));
  11. var turf_polygon_dissolve_1 = __importDefault(require("./turf-polygon-dissolve"));
  12. /**
  13. * Transform function: attempts to dissolve geojson objects where possible
  14. * [GeoJSON] -> GeoJSON geometry
  15. *
  16. * @private
  17. * @param {FeatureCollection<LineString|MultiLineString|Polygon|MultiPolygon>} geojson Features to dissolved
  18. * @param {Object} [options={}] Optional parameters
  19. * @param {boolean} [options.mutate=false] Prevent input mutation
  20. * @returns {Feature<MultiLineString|MultiPolygon>} Dissolved Features
  21. */
  22. function dissolve(geojson, options) {
  23. if (options === void 0) { options = {}; }
  24. // Optional parameters
  25. options = options || {};
  26. if (!helpers_1.isObject(options)) {
  27. throw new Error("options is invalid");
  28. }
  29. var mutate = options.mutate;
  30. // Validation
  31. if (invariant_1.getType(geojson) !== "FeatureCollection") {
  32. throw new Error("geojson must be a FeatureCollection");
  33. }
  34. if (!geojson.features.length) {
  35. throw new Error("geojson is empty");
  36. }
  37. // Clone geojson to avoid side effects
  38. // Topojson modifies in place, so we need to deep clone first
  39. if (mutate === false || mutate === undefined) {
  40. geojson = clone_1.default(geojson);
  41. }
  42. // Assert homogenity
  43. var type = getHomogenousType(geojson);
  44. if (!type) {
  45. throw new Error("geojson must be homogenous");
  46. }
  47. // Data => Typescript hack
  48. var data = geojson;
  49. switch (type) {
  50. case "LineString":
  51. return turf_line_dissolve_1.default(data, options);
  52. case "Polygon":
  53. return turf_polygon_dissolve_1.default(data, options);
  54. default:
  55. throw new Error(type + " is not supported");
  56. }
  57. }
  58. /**
  59. * Checks if GeoJSON is Homogenous
  60. *
  61. * @private
  62. * @param {GeoJSON} geojson GeoJSON
  63. * @returns {string|null} Homogenous type or null if multiple types
  64. */
  65. function getHomogenousType(geojson) {
  66. var types = {};
  67. meta_1.flattenEach(geojson, function (feature) {
  68. types[feature.geometry.type] = true;
  69. });
  70. var keys = Object.keys(types);
  71. if (keys.length === 1) {
  72. return keys[0];
  73. }
  74. return null;
  75. }
  76. exports.default = dissolve;