Turf.jsMeasurement

turf.nearestPoint

What is turf.nearestPoint?

turf.nearestPoint takes a target Point and a FeatureCollection<Point> and returns the single point in the collection that is closest to the target. It adds distanceToPoint and featureIndex properties to the returned feature.

JavaScript
turf.nearestPoint(targetPoint, points) → Feature<Point>

When would you use turf.nearestPoint?

Use turf.nearestPoint for "find the closest venue/stop/store" problems on small to medium point datasets — a few hundred to a few thousand points. It is perfect for snapping a clicked location to the nearest POI in a MapLibre app, or for assigning each customer address to its nearest warehouse in a Node.js job.

For large datasets (tens of thousands of points) a linear scan is too slow — use a spatial index such as @turf/geojson-rbush or supercluster for the coarse lookup and call turf.distance only on candidates.

Code
undefined

FAQs

What units is distanceToPoint in?

Kilometers. If you need another unit, convert with turf.convertLength or call turf.distance again with the desired units option after finding the nearest feature.

How does performance scale?

turf.nearestPoint is O(n) — it computes distance to every point. For collections above ~10,000 points consider building an r-tree with @turf/geojson-rbush and running nearestPoint only on nearby candidates.

How do I install just nearestPoint?

npm install @turf/nearest-point. It depends on @turf/distance, @turf/meta, and @turf/invariant.

Does it mutate the original feature?

It returns a clone of the closest feature with distanceToPoint and featureIndex added to its properties. The original FeatureCollection is not modified.