index.js 1.3 KB

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