index.js 1.0 KB

1234567891011121314151617181920212223242526272829
  1. // index.js
  2. import { getCoords, getType } from "@turf/invariant";
  3. import { lineString as linestring } from "@turf/helpers";
  4. import { nearestPointOnLine } from "@turf/nearest-point-on-line";
  5. function lineSlice(startPt, stopPt, line) {
  6. var coords = getCoords(line);
  7. if (getType(line) !== "LineString")
  8. throw new Error("line must be a LineString");
  9. var startVertex = nearestPointOnLine(line, startPt);
  10. var stopVertex = nearestPointOnLine(line, stopPt);
  11. var ends;
  12. if (startVertex.properties.index <= stopVertex.properties.index) {
  13. ends = [startVertex, stopVertex];
  14. } else {
  15. ends = [stopVertex, startVertex];
  16. }
  17. var clipCoords = [ends[0].geometry.coordinates];
  18. for (var i = ends[0].properties.index + 1; i < ends[1].properties.index + 1; i++) {
  19. clipCoords.push(coords[i]);
  20. }
  21. clipCoords.push(ends[1].geometry.coordinates);
  22. return linestring(clipCoords, line.properties);
  23. }
  24. var turf_line_slice_default = lineSlice;
  25. export {
  26. turf_line_slice_default as default,
  27. lineSlice
  28. };
  29. //# sourceMappingURL=index.js.map