index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // index.ts
  2. import { featureCollection, isObject, multiPolygon } from "@turf/helpers";
  3. import { collectionOf } from "@turf/invariant";
  4. import { featureEach } from "@turf/meta";
  5. import { flatten } from "@turf/flatten";
  6. import * as polyclip from "polyclip-ts";
  7. function dissolve(fc, options = {}) {
  8. options = options || {};
  9. if (!isObject(options)) throw new Error("options is invalid");
  10. const { propertyName } = options;
  11. collectionOf(fc, "Polygon", "dissolve");
  12. const outFeatures = [];
  13. if (!propertyName) {
  14. return flatten(
  15. multiPolygon(
  16. polyclip.union.apply(
  17. null,
  18. // List of polygons expressed as Position[][][] a.k.a. Geom[]
  19. fc.features.map(function(f) {
  20. return f.geometry.coordinates;
  21. })
  22. )
  23. )
  24. );
  25. } else {
  26. const uniquePropertyVals = {};
  27. featureEach(fc, function(feature) {
  28. if (feature.properties) {
  29. if (!Object.prototype.hasOwnProperty.call(
  30. uniquePropertyVals,
  31. feature.properties[propertyName]
  32. )) {
  33. uniquePropertyVals[feature.properties[propertyName]] = [];
  34. }
  35. uniquePropertyVals[feature.properties[propertyName]].push(feature);
  36. }
  37. });
  38. const vals = Object.keys(uniquePropertyVals);
  39. for (let i = 0; i < vals.length; i++) {
  40. const mp = multiPolygon(
  41. polyclip.union.apply(
  42. null,
  43. // List of polygons expressed as Position[][][] a.k.a. Geom[]
  44. uniquePropertyVals[vals[i]].map(function(f) {
  45. return f.geometry.coordinates;
  46. })
  47. )
  48. );
  49. if (mp && mp.properties) {
  50. mp.properties[propertyName] = vals[i];
  51. outFeatures.push(mp);
  52. }
  53. }
  54. }
  55. return flatten(featureCollection(outFeatures));
  56. }
  57. var turf_dissolve_default = dissolve;
  58. export {
  59. turf_dissolve_default as default,
  60. dissolve
  61. };
  62. //# sourceMappingURL=index.js.map