3

Given a linestring geometry, how can I find the point and bearing which is a given distance from the end? e.g. I have a line representing a road and need to identify the point exactly 100 feet from the end, and the direction of travel at that point.

1

1 Answer 1

3

Get the last two points of your linestring. ST_Azimuth returns the north-based azimuth as the angle in radians measured clockwise from the vertical on pointA to pointB. Extrapolate the line.

Assuming your coordinates are in feet and your road geometry is in mytable.geom:

WITH s1 AS (SELECT
    ST_PointN(geom, ST_NPoints(geom) - 1) AS p1,
    ST_EndPoint(geom) AS p2
    FROM mytable),
s2 AS (SELECT
    p1, p2,
    ST_Azimuth(p1, p2) AS dir
    FROM s1)
SELECT
    degrees(dir) AS dir,
    ST_Translate(p2, 100.0 * sin(dir), 100.0 * cos(dir)) AS extra
    FROM s2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.