Turf.jsHelper

turf.lineString

What is turf.lineString?

turf.lineString is a constructor helper that turns an array of two or more [lng, lat] positions into a valid Feature<LineString>. It throws if fewer than two coordinates are supplied.

JavaScript
turf.lineString(coordinates, properties?, options?) → Feature<LineString>

Options include:

  • bbox — bounding box [minX, minY, maxX, maxY]
  • id — identifier for the feature

When would you use turf.lineString?

Use turf.lineString whenever you need to feed a line feature into a Turf operation or a MapLibre/Mapbox source — for example converting a GPX track, a routing engine response, or a pair of waypoints into GeoJSON before calling turf.length, turf.along, or turf.buffer.

It is also the standard way to draft test fixtures in unit tests and to normalise coordinate arrays before storing them.

JavaScript
1const route = turf.lineString(
2  [[-122.42, 37.77], [-122.40, 37.78], [-122.39, 37.79]],
3  { name: 'Market Street' }
4);

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/helpers and import with import { lineString } from '@turf/helpers', or use the umbrella @turf/turf package.

What happens if I pass fewer than two coordinates?

turf.lineString throws an error. The GeoJSON spec requires at least two positions for a valid LineString — catch or validate upstream if you accept user input.

Does turf.lineString validate coordinate order (lng, lat)?

No. It only checks the shape of each position (an array of at least two numbers). It cannot detect lat/lng swaps — a common bug when porting code from libraries that use [lat, lng]. Verify against a map view before shipping.

How do I build a MultiLineString instead?

Use turf.multiLineString(coordinates, properties?) where coordinates is an array of LineString coordinate arrays. Use it for discontinuous line features such as a highway split by a bridge gap.