Turf.jsMeasurement

turf.length

What is turf.length?

turf.length takes a GeoJSON LineString, MultiLineString, or a Feature/FeatureCollection containing them and returns the total length as a number. It walks every segment and sums haversine distances between consecutive vertices — the same formula turf.distance uses.

JavaScript
turf.length(geojson, options?) → number

Options include:

  • units'kilometers' (default), 'meters', 'miles', 'nauticalmiles', 'radians', 'degrees'

When would you use turf.length?

Use turf.length to display the length of a user-drawn route on a MapLibre map, compute trail mileage for a hiking app, or aggregate network length by road class in a Node.js pipeline. It pairs naturally with turf.along when you want to chunk a route, place mileposts, or animate along the line.

For polygons you want perimeter of, convert with turf.polygonToLine first and then call turf.length. For point-to-line distance, use turf.pointToLineDistance.

JavaScript
import length from '@turf/length';

FAQs

Does it include every vertex or just endpoints?

Every vertex. turf.length walks each segment of the line and sums haversine distances, so densely digitised curves produce more accurate real-world length than coarse polylines.

How do I install just length?

npm install @turf/length. It depends on @turf/distance and @turf/meta and is very small.

Is a FeatureCollection supported?

Yes. turf.length accepts any GeoJSON container and sums the lengths of all LineString and MultiLineString parts it finds. Non-line features are ignored.

Why is my measured length shorter than what Google Maps says?

Usually because the input line is less densely digitised — haversine sums straight-line chords between vertices, so a polyline with few points in a curve underestimates a smooth real-world path. Densify the line (e.g. via turf.lineChunk) if you need closer agreement.