Turf.jsMisc

turf.lineChunk

What is turf.lineChunk?

turf.lineChunk takes a LineString or MultiLineString and splits it into segments of approximately equal length. The result is a FeatureCollection<LineString>.

JavaScript
turf.lineChunk(line, segmentLength, options?) → FeatureCollection<LineString>

Options include:

  • units'kilometers' (default), 'meters', 'miles', 'nauticalmiles', 'radians', 'degrees'
  • reverseboolean (default false); when true, chunks are measured from the end

When would you use turf.lineChunk?

Use turf.lineChunk to break a route into equal segments for mileposts, progress tracking, or animation frames. On MapLibre, chunks are perfect for gradient-coloured lines where each segment gets its own colour based on an attribute like speed or elevation.

In Node.js, chunking is a common preparation for elevation profile queries (fetch elevation per chunk midpoint), for heartbeat/tile tracking along a route, or for tile-based linear referencing.

Code
undefined

FAQs

What if the line length is not a multiple of segmentLength?

The last chunk will be shorter than segmentLength. All earlier chunks match the requested length exactly (within floating-point precision).

How do I install just lineChunk?

npm install @turf/line-chunk. It depends on @turf/helpers, @turf/meta, @turf/length, and @turf/line-slice-along.

Does it densify the line?

Not directly — it splits at computed positions, which may fall between existing vertices. The cut points are new vertices in the output, so dense input is not required.

Can I chunk by count instead of length?

Not with lineChunk. Use turf.length to compute total length, divide by the desired count, then pass that as segmentLength — the output will have approximately that many chunks.