index.cjs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
  2. var _bearing = require('@turf/bearing');
  3. var _distance = require('@turf/distance');
  4. var _destination = require('@turf/destination');
  5. var _helpers = require('@turf/helpers');
  6. function lineSliceAlong(line, startDist, stopDist, options) {
  7. options = options || {};
  8. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  9. var coords;
  10. var slice = [];
  11. if (line.type === "Feature") coords = line.geometry.coordinates;
  12. else if (line.type === "LineString") coords = line.coordinates;
  13. else throw new Error("input must be a LineString Feature or Geometry");
  14. var origCoordsLength = coords.length;
  15. var travelled = 0;
  16. var overshot, direction, interpolated;
  17. for (var i = 0; i < coords.length; i++) {
  18. if (startDist >= travelled && i === coords.length - 1) break;
  19. else if (travelled > startDist && slice.length === 0) {
  20. overshot = startDist - travelled;
  21. if (!overshot) {
  22. slice.push(coords[i]);
  23. return _helpers.lineString.call(void 0, slice);
  24. }
  25. direction = _bearing.bearing.call(void 0, coords[i], coords[i - 1]) - 180;
  26. interpolated = _destination.destination.call(void 0, coords[i], overshot, direction, options);
  27. slice.push(interpolated.geometry.coordinates);
  28. }
  29. if (travelled >= stopDist) {
  30. overshot = stopDist - travelled;
  31. if (!overshot) {
  32. slice.push(coords[i]);
  33. return _helpers.lineString.call(void 0, slice);
  34. }
  35. direction = _bearing.bearing.call(void 0, coords[i], coords[i - 1]) - 180;
  36. interpolated = _destination.destination.call(void 0, coords[i], overshot, direction, options);
  37. slice.push(interpolated.geometry.coordinates);
  38. return _helpers.lineString.call(void 0, slice);
  39. }
  40. if (travelled >= startDist) {
  41. slice.push(coords[i]);
  42. }
  43. if (i === coords.length - 1) {
  44. return _helpers.lineString.call(void 0, slice);
  45. }
  46. travelled += _distance.distance.call(void 0, coords[i], coords[i + 1], options);
  47. }
  48. if (travelled < startDist && coords.length === origCoordsLength)
  49. throw new Error("Start position is beyond line");
  50. var last = coords[coords.length - 1];
  51. return _helpers.lineString.call(void 0, [last, last]);
  52. }
  53. var turf_line_slice_along_default = lineSliceAlong;
  54. exports.default = turf_line_slice_along_default; exports.lineSliceAlong = lineSliceAlong;
  55. //# sourceMappingURL=index.cjs.map