index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // index.ts
  2. import { bearing } from "@turf/bearing";
  3. import { destination } from "@turf/destination";
  4. import { distance as measureDistance } from "@turf/distance";
  5. import { point } from "@turf/helpers";
  6. import { getGeom } from "@turf/invariant";
  7. function along(line, distance, options = {}) {
  8. const geom = getGeom(line);
  9. const coords = geom.coordinates;
  10. let travelled = 0;
  11. for (let i = 0; i < coords.length; i++) {
  12. if (distance >= travelled && i === coords.length - 1) {
  13. break;
  14. } else if (travelled >= distance) {
  15. const overshot = distance - travelled;
  16. if (!overshot) {
  17. return point(coords[i]);
  18. } else {
  19. const direction = bearing(coords[i], coords[i - 1]) - 180;
  20. const interpolated = destination(
  21. coords[i],
  22. overshot,
  23. direction,
  24. options
  25. );
  26. return interpolated;
  27. }
  28. } else {
  29. travelled += measureDistance(coords[i], coords[i + 1], options);
  30. }
  31. }
  32. return point(coords[coords.length - 1]);
  33. }
  34. var turf_along_default = along;
  35. export {
  36. along,
  37. turf_along_default as default
  38. };
  39. //# sourceMappingURL=index.js.map