index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 bearing_1 = __importDefault(require("@turf/bearing"));
  7. var destination_1 = __importDefault(require("@turf/destination"));
  8. var distance_1 = __importDefault(require("@turf/distance"));
  9. var helpers_1 = require("@turf/helpers");
  10. var invariant_1 = require("@turf/invariant");
  11. /**
  12. * Takes a {@link LineString} and returns a {@link Point} at a specified distance along the line.
  13. *
  14. * @name along
  15. * @param {Feature<LineString>} line input line
  16. * @param {number} distance distance along the line
  17. * @param {Object} [options] Optional parameters
  18. * @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers
  19. * @returns {Feature<Point>} Point `distance` `units` along the line
  20. * @example
  21. * var line = turf.lineString([[-83, 30], [-84, 36], [-78, 41]]);
  22. * var options = {units: 'miles'};
  23. *
  24. * var along = turf.along(line, 200, options);
  25. *
  26. * //addToMap
  27. * var addToMap = [along, line]
  28. */
  29. function along(line, distance, options) {
  30. if (options === void 0) { options = {}; }
  31. // Get Coords
  32. var geom = invariant_1.getGeom(line);
  33. var coords = geom.coordinates;
  34. var travelled = 0;
  35. for (var i = 0; i < coords.length; i++) {
  36. if (distance >= travelled && i === coords.length - 1) {
  37. break;
  38. }
  39. else if (travelled >= distance) {
  40. var overshot = distance - travelled;
  41. if (!overshot) {
  42. return helpers_1.point(coords[i]);
  43. }
  44. else {
  45. var direction = bearing_1.default(coords[i], coords[i - 1]) - 180;
  46. var interpolated = destination_1.default(coords[i], overshot, direction, options);
  47. return interpolated;
  48. }
  49. }
  50. else {
  51. travelled += distance_1.default(coords[i], coords[i + 1], options);
  52. }
  53. }
  54. return helpers_1.point(coords[coords.length - 1]);
  55. }
  56. exports.default = along;