The SE_PerpendicularPoint() function
The SE_PerpendicularPoint() function finds the perpendicular projection of a point on to the nearest segment of a linestring or multilinestring. If two or more such perpendicular projected points are equidistant from the input point, they are all returned. If no perpendicular point can be constructed, an empty point is returned.
If the input linestring has Z values or measures, the Z value or measure of the perpendicular point are computed by linear interpolation between the adjacent vertices.
Syntax
SE_PerpendicularPoint(ST_LineString, ST_Point)
SE_PerpendicularPoint(ST_MultiLineString, ST_Point)
Return type
ST_MultiPoint
Example
CREATE TABLE linestring_test (line ST_LineString);
-- Create a U-shaped linestring:
INSERT INTO linestring_test VALUES (
ST_LineFromText('linestring z (0 10 1, 0 0 3, 10 0 5, 10 10 7)', 0)
);
-- Perpendicular point is coincident with the input point,
-- on the base of the U:
SELECT SE_PerpendicularPoint(line, ST_PointFromText('point(5 0)', 0))
FROM linestring_test;
(expression) 0 MULTIPOINT Z (5 0 4)
-- Perpendicular points are located on all three segments of the U:
SELECT SE_PerpendicularPoint(line, ST_PointFromText('point(5 5)', 0))
FROM linestring_test;
(expression) 0 MULTIPOINT Z (0 5 2, 5 0 4, 10 5 6)
-- Perpendicular points are located at the endpoints of the U:
SELECT SE_PerpendicularPoint(line, ST_PointFromText('point(5 10)', 0))
FROM linestring_test;
(expression) 0 MULTIPOINT Z (0 10 1, 10 10 7)
-- Perpendicular point is on the base of the U:
SELECT SE_PerpendicularPoint(line, ST_PointFromText('point(5 15)', 0))
FROM linestring_test;
(expression) 0 MULTIPOINT Z (5 0 4)
-- No perpendicular point can be constructed:
SELECT SE_PerpendicularPoint(line, ST_PointFromText('point(15 15)', 0))
FROM linestring_test;
(expression) 0 POINT EMPTY