index.js 1.0 KB

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