index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { featureEach, coordEach } from '@turf/meta';
  2. import { point, featureCollection } from '@turf/helpers';
  3. /**
  4. * Takes a feature or set of features and returns all positions as {@link Point|points}.
  5. *
  6. * @name explode
  7. * @param {GeoJSON} geojson input features
  8. * @returns {FeatureCollection<point>} points representing the exploded input features
  9. * @throws {Error} if it encounters an unknown geometry type
  10. * @example
  11. * var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);
  12. *
  13. * var explode = turf.explode(polygon);
  14. *
  15. * //addToMap
  16. * var addToMap = [polygon, explode]
  17. */
  18. function explode(geojson) {
  19. var points = [];
  20. if (geojson.type === "FeatureCollection") {
  21. featureEach(geojson, function (feature) {
  22. coordEach(feature, function (coord) {
  23. points.push(point(coord, feature.properties));
  24. });
  25. });
  26. } else {
  27. coordEach(geojson, function (coord) {
  28. points.push(point(coord, geojson.properties));
  29. });
  30. }
  31. return featureCollection(points);
  32. }
  33. export default explode;