ST_FrechetDistance
What is ST_FrechetDistance?
ST_FrechetDistance returns the discrete Frechet distance between two geometries — a "dog-on-a-leash" similarity metric that, unlike Hausdorff distance, respects the ordering of points along each input. The result is small when two paths follow similar trajectories in the same direction, even if the Hausdorff distance would also be small.
ST_FrechetDistance(geometry g1, geometry g2, float densifyFrac = -1) → floatThe optional densifyFrac parameter (between 0 and 1) densifies each segment into sub-segments before computing, giving a closer approximation to the continuous Frechet distance at higher cost. A value of -1 (default) skips densification. Units are those of the input CRS.
When would you use ST_FrechetDistance?
Use ST_FrechetDistance to compare the similarity of two paths or curves where direction matters: comparing a recorded GPS track to a planned route, detecting whether two vessel tracks describe the same manoeuvre, or clustering trajectories in a fleet dataset. It is also used in handwriting and signature comparison, and in matching geometric shapes where orientation and traversal order carry meaning.
1-- Find GPS tracks most similar to a reference route
2SELECT track_id, ST_FrechetDistance(track.geom, ref.geom, 0.001) AS frechet_m
3FROM tracks track, reference_route ref
4ORDER BY frechet_m
5LIMIT 5;FAQs
How is Frechet distance different from Hausdorff distance?
Hausdorff ignores point ordering and returns the greatest minimum distance between the two sets. Frechet respects ordering — two lines that pass through the same points but in opposite directions have small Hausdorff distance and large Frechet distance.
What does the densifyFrac parameter do?
It controls how finely each segment is subdivided before computing the discrete Frechet distance. Smaller values (e.g. 0.01) give a better approximation of the continuous Frechet distance but cost more CPU. A value of -1 disables densification.
What units is the result in?
The units of the input CRS — degrees for EPSG:4326, metres for most projected CRSs. ST_FrechetDistance does not support geography; reproject lat/lon data before calling.
When should I use Frechet instead of simple line distance?
Use Frechet when trajectory shape and direction matter. Use plain ST_Distance for proximity, ST_HausdorffDistance for direction-agnostic shape similarity, and Frechet for ordered-path similarity like GPS track matching.