turf.shortestPath
What is turf.shortestPath?
turf.shortestPath computes the shortest LineString path between two Points that avoids a user-supplied set of Polygon obstacles. Internally it builds a grid over the region and runs an A* search.
turf.shortestPath(start, end, options?) → Feature<LineString>Options include:
obstacles—FeatureCollection<Polygon>of impassable regionsminDistance— minimum step size in units (default0.1)units—'kilometers'(default),'meters','miles','nauticalmiles','radians','degrees'resolution— grid resolution in units
When would you use turf.shortestPath?
Use turf.shortestPath when you need a simple "walk around obstacles" router without standing up a full graph database — for example, routing a drone flight path around no-fly zones, planning a waterway route around islands, or navigating a figure around restricted areas in a small game or simulation.
It is not a replacement for road-network routing (use OSRM, Valhalla, or Mapbox Directions for that). turf.shortestPath is a geometric routing primitive best suited to open-space navigation.
undefinedFAQs
How does performance scale?
turf.shortestPath is O(grid-cells) for the A* search — scales with the area of the bounding region divided by resolution². For large regions, use a coarser resolution or a dedicated routing engine.
How do I install just shortestPath?
npm install @turf/shortest-path. It depends on @turf/helpers, @turf/invariant, @turf/bbox, @turf/distance, @turf/point-to-line-distance, @turf/transform-scale, and others.
Does it handle roads?
No — it is a geometric free-space router. For real road networks use OSRM, Valhalla, GraphHopper, or Mapbox Directions.
What if there is no valid path?
The result can be the straight line or an incomplete path depending on the obstacle configuration. Validate the output by checking that it does not intersect the obstacles (e.g. with turf.booleanIntersects).