ST_LineLocatePoint
What is ST_LineLocatePoint?
ST_LineLocatePoint is a PostGIS function that returns a fractional value between 0 and 1 representing the position along a linestring of the closest point to a given input point.
1ST_LineLocatePoint(geometry line, geometry point) → float
2ST_LineLocatePoint(geography gline, geography gpoint, boolean use_spheroid = true) → floatWhen would you use ST_LineLocatePoint?
Use ST_LineLocatePoint to linearly reference an arbitrary point against a route — matching GPS traces to roads, computing how far along a river a tributary joins, locating a landmark along a boundary, or implementing "you are here" positioning.
1SELECT ST_LineLocatePoint(road.geom, ev.geom) AS position_fraction,
2 ST_Length(road.geom) * ST_LineLocatePoint(road.geom, ev.geom) AS distance_m
3FROM roads road, events ev
4WHERE road.id = 7;FAQs
Does the input point have to lie on the line?
No. ST_LineLocatePoint uses the closest point on the line to the input point — the input is effectively projected onto the line first. This is the typical use case for matching off-line observations to a route.
How do I convert the fraction to a metric distance?
Multiply by ST_Length(line) for 2D length, or by ST_3DLength(line) if you are working in 3D. For geography, ST_Length(line::geography) returns metres directly.
Does it work on MultiLineStrings?
No — the input must be a single LINESTRING. For multi-part routes, merge with ST_LineMerge first if contiguous.
How is it the inverse of ST_LineInterpolatePoint?
ST_LineInterpolatePoint(line, fraction) takes a fraction and returns a point; ST_LineLocatePoint(line, point) takes a point and returns a fraction. Composing them is a common pattern: locate a user click on the line, then interpolate back to the exact nearest-point geometry.