index.js 1.1 KB

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