index.js 1.0 KB

1234567891011121314151617181920212223242526
  1. import distance from "@turf/distance";
  2. import { segmentReduce } from "@turf/meta";
  3. /**
  4. * Takes a {@link GeoJSON} and measures its length in the specified units, {@link (Multi)Point}'s distance are ignored.
  5. *
  6. * @name length
  7. * @param {Feature<LineString|MultiLineString>} geojson GeoJSON to measure
  8. * @param {Object} [options={}] Optional parameters
  9. * @param {string} [options.units=kilometers] can be degrees, radians, miles, or kilometers
  10. * @returns {number} length of GeoJSON
  11. * @example
  12. * var line = turf.lineString([[115, -32], [131, -22], [143, -25], [150, -34]]);
  13. * var length = turf.length(line, {units: 'miles'});
  14. *
  15. * //addToMap
  16. * var addToMap = [line];
  17. * line.properties.distance = length;
  18. */
  19. export default function length(geojson, options) {
  20. if (options === void 0) { options = {}; }
  21. // Calculate distance from 2-vertex line segments
  22. return segmentReduce(geojson, function (previousValue, segment) {
  23. var coords = segment.geometry.coordinates;
  24. return previousValue + distance(coords[0], coords[1], options);
  25. }, 0);
  26. }