turf.lineSlice
What is turf.lineSlice?
turf.lineSlice returns the portion of a LineString between two reference Points. Each reference point is first snapped to the nearest position on the line, then the function returns the sub-line between those snapped positions.
turf.lineSlice(startPt, stopPt, line) → Feature<LineString>When would you use turf.lineSlice?
Use turf.lineSlice for cutting a route between two user-selected points — for example, "show me the segment of this route between these two stops" or "highlight the leg of the trail between the trailhead and the summit marker." It is ideal for interactive route manipulation on MapLibre.
In Node.js, it is useful for splitting imported routes at known event points (e.g. a charging stop, a rest break) and for generating tracked legs from a full GPS log.
undefinedFAQs
Do the start and stop points need to lie exactly on the line?
No — they are snapped to the nearest point on the line before slicing. Ideal for user clicks near a route.
How do I install just lineSlice?
npm install @turf/line-slice. It depends on @turf/helpers, @turf/invariant, and @turf/nearest-point-on-line.
What if the start is after the stop?
turf.lineSlice returns the sub-line in the order determined by position along the line — effectively the range between the two, regardless of which you labelled as start. For directional slicing, ensure start is before stop by pre-computing their location via nearestPointOnLine.
Should I use lineSliceAlong instead?
Use turf.lineSliceAlong when you know the start and stop as distances along the line (e.g. mileposts). Use turf.lineSlice when you know them as geographic points.