Returns a float between 0 and 1 representing the location of the closest point on a LineString to the given point, expressed as a fraction of the total line length.
Use the returned value as input to ST_LineInterpolatePoint to get the actual coordinates of that closest point, or to ST_LineSubstring to extract a segment of the line. This is useful for approximating address numbers along a street.
Syntax
float8 ST_LineLocatePoint(geometry aLinestring, geometry aPoint);
float8 ST_LineLocatePoint(geography aLinestring, geography aPoint);Parameters
| Parameter | Description |
|---|---|
aLinestring | The LineString object. |
aPoint | The point object. |
Examples
Find the closest location on a line to a point
Both examples use the same input geometry: a vertical line from (0,0) to (0,2) and a point at (1,1). The geometry overload returns exactly 0.5 because the point lies at the midpoint of the line. The geography overload returns a value slightly different from 0.5 due to spheroid correction.
-- geometry: exact midpoint
SELECT ST_LineLocatePoint('LINESTRING(0 0,0 2)'::geometry, 'POINT(1 1)'::geometry); st_linelocatepoint
--------------------
0.5
(1 row)-- geography: spheroid-corrected result
SELECT ST_LineLocatePoint('LINESTRING(0 0,0 2)'::geography, 'POINT(1 1)'::geography); st_linelocatepoint
--------------------
0.5000746195163556
(1 row)What's next
ST_LineInterpolatePoint — get the point at a given fraction along a line
ST_LineSubstring — extract a line segment between two fractions