turf.pointToLineDistance
What is turf.pointToLineDistance?
turf.pointToLineDistance returns the shortest distance from a Point to any segment of a LineString. It walks each segment, computes the perpendicular (or endpoint) distance, and returns the minimum.
turf.pointToLineDistance(point, line, options?) → numberOptions include:
units—'kilometers'(default),'meters','miles','nauticalmiles','radians','degrees'method—'geodesic'(default) or'planar'
When would you use turf.pointToLineDistance?
Use turf.pointToLineDistance for snap-to-line interactions — is this click within N metres of a road? For address-to-street matching in a Node.js pipeline, or for distance-to-coast calculations. It is also handy for filtering a layer to points close to a reference route.
For finding the nearest point on the line itself (not just the distance), pair it with turf.nearestPointOnLine.
import pointToLineDistance from '@turf/point-to-line-distance';FAQs
What is the difference between geodesic and planar?
geodesic uses spherical/haversine math and is accurate on real Earth coordinates. planar treats longitude/latitude as Cartesian, which is faster but only correct over short distances at moderate latitudes. Stick with geodesic for public-facing mapping.
How do I install just pointToLineDistance?
npm install @turf/point-to-line-distance. Its dependencies include @turf/distance, @turf/helpers, and @turf/invariant.
Does it return the nearest point as well?
No — just the distance. For the projected point on the line, use turf.nearestPointOnLine which returns both the nearest point and its distance.
Does it handle MultiLineString?
Yes — flatten with turf.flatten or iterate, calling pointToLineDistance for each part and taking the minimum. Newer versions of Turf accept LineString Features; check your package version for broader input types.