index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. var helpers = require('@turf/helpers');
  3. var invariant = require('@turf/invariant');
  4. var meta = require('@turf/meta');
  5. var flatten = require('@turf/flatten');
  6. var polygonClipping = require('polygon-clipping');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
  9. var polygonClipping__default = /*#__PURE__*/_interopDefaultLegacy(polygonClipping);
  10. /**
  11. * Dissolves a FeatureCollection of {@link polygon} features, filtered by an optional property name:value.
  12. * Note that {@link mulitpolygon} features within the collection are not supported
  13. *
  14. * @name dissolve
  15. * @param {FeatureCollection<Polygon>} featureCollection input feature collection to be dissolved
  16. * @param {Object} [options={}] Optional parameters
  17. * @param {string} [options.propertyName] features with the same `propertyName` value will be dissolved.
  18. * @returns {FeatureCollection<Polygon>} a FeatureCollection containing the dissolved polygons
  19. * @example
  20. * var features = turf.featureCollection([
  21. * turf.polygon([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]], {combine: 'yes'}),
  22. * turf.polygon([[[0, -1], [0, 0], [1, 0], [1, -1], [0,-1]]], {combine: 'yes'}),
  23. * turf.polygon([[[1,-1],[1, 0], [2, 0], [2, -1], [1, -1]]], {combine: 'no'}),
  24. * ]);
  25. *
  26. * var dissolved = turf.dissolve(features, {propertyName: 'combine'});
  27. *
  28. * //addToMap
  29. * var addToMap = [features, dissolved]
  30. */
  31. function dissolve(fc, options) {
  32. // Optional parameters
  33. options = options || {};
  34. if (!helpers.isObject(options)) throw new Error("options is invalid");
  35. var propertyName = options.propertyName;
  36. // Input validation
  37. invariant.collectionOf(fc, "Polygon", "dissolve");
  38. // Main
  39. var outFeatures = [];
  40. if (!options.propertyName) {
  41. return flatten__default['default'](
  42. helpers.multiPolygon(
  43. polygonClipping__default['default'].union.apply(
  44. null,
  45. fc.features.map(function (f) {
  46. return f.geometry.coordinates;
  47. })
  48. )
  49. )
  50. );
  51. } else {
  52. var uniquePropertyVals = {};
  53. meta.featureEach(fc, function (feature) {
  54. if (
  55. !Object.prototype.hasOwnProperty.call(
  56. uniquePropertyVals,
  57. feature.properties[propertyName]
  58. )
  59. ) {
  60. uniquePropertyVals[feature.properties[propertyName]] = [];
  61. }
  62. uniquePropertyVals[feature.properties[propertyName]].push(feature);
  63. });
  64. var vals = Object.keys(uniquePropertyVals);
  65. for (var i = 0; i < vals.length; i++) {
  66. var mp = helpers.multiPolygon(
  67. polygonClipping__default['default'].union.apply(
  68. null,
  69. uniquePropertyVals[vals[i]].map(function (f) {
  70. return f.geometry.coordinates;
  71. })
  72. )
  73. );
  74. mp.properties[propertyName] = vals[i];
  75. outFeatures.push(mp);
  76. }
  77. }
  78. return flatten__default['default'](helpers.featureCollection(outFeatures));
  79. }
  80. module.exports = dissolve;
  81. module.exports.default = dissolve;