index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. var meta = require('@turf/meta');
  3. var helpers = require('@turf/helpers');
  4. /**
  5. * Flattens any {@link GeoJSON} to a {@link FeatureCollection} inspired by [geojson-flatten](https://github.com/tmcw/geojson-flatten).
  6. *
  7. * @name flatten
  8. * @param {GeoJSON} geojson any valid GeoJSON Object
  9. * @returns {FeatureCollection<any>} all Multi-Geometries are flattened into single Features
  10. * @example
  11. * var multiGeometry = turf.multiPolygon([
  12. * [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
  13. * [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
  14. * [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
  15. * ]);
  16. *
  17. * var flatten = turf.flatten(multiGeometry);
  18. *
  19. * //addToMap
  20. * var addToMap = [flatten]
  21. */
  22. function flatten(geojson) {
  23. if (!geojson) throw new Error("geojson is required");
  24. var results = [];
  25. meta.flattenEach(geojson, function (feature) {
  26. results.push(feature);
  27. });
  28. return helpers.featureCollection(results);
  29. }
  30. module.exports = flatten;
  31. module.exports.default = flatten;