The ST_DistanceToPoint() function
The ST_DistanceToPoint() function returns the distance from the start of the line to the specified point. Z coordinates and measures are ignored.
Syntax
ST_DistanceToPoint (ST_LineString, ST_Point)
ST_DistanceToPoint (ST_MultiLineString, ST_Point)
Return type
DOUBLE
Example
The following SQL statement creates the sample_geometries table with two columns. The ID column uniquely identifies each row. The geometry ST_LineString column stores sample geometries.
CREATE TABLE sample_geometries(id INTEGER, geometry ST_LINESTRING);
The following SQL statement inserts two rows into the sample_geometries table:
INSERT INTO sample_geometries(id, geometry)
VALUES
(1,ST_LineString('LINESTRING ZM(0 0 0 0, 10 100 1000 10000)',1)),
(2,ST_LineString('LINESTRING ZM(10 100 1000 10000, 0 0 0 0)',1));
The following SELECT statement and the corresponding result set show how to use the ST_DistanceToPoint() function to find the distance to the point at the location (1.5, 15.0):
SELECT ID, DECIMAL(ST_DistanceToPoint(geometry,ST_Point(1.5,15.0,1)),10,5)
AS DISTANCE FROM sample_geometries;
ID DISTANCE
----------- ------------
1 15.07481
2 85.42394
2 record(s) selected.