# What is the best way to get two points along the line in both directions using postgis?

I have a point on a `linestring` geometry (found by an intersection). What is the best way to get two points along the line in both directions using `postgis`?

It does not really matter if the points represent a vertex or are just a given distance or fraction away from the intersection point. I just need a short line to calculate the azimuth between those two points.

• Why don't you just use the end point vertices of that initial linestring if the only purpose is to calculate the azimuth? I mean the azimuth of that short line you are expecting and the initial linestring must be same!
– Zia
Mar 17, 2015 at 9:51
• Certainly not! If the `linestring` represents an objects with a lot of curves your endpoint (or startpoint for the same matter) can be in a totally different direction (thus different degree of azimuth!) then s short fraction of the `linestring` around the given point of interest. Mar 17, 2015 at 11:00
• Hmm... So basically you want azimuth of the tangent along that point.
– Zia
Mar 17, 2015 at 13:14

Following SQL could be used to obtain tangent at a given point on a curve (although not rigorously tested). Here `poly` is the `linestring` table name and `(30.7011637,33.1748235),4326)` is the point of intersection where we wanna calculate the tangent's azimuth. The query will give you the tangent line. Let us know if this worked fine for you.

``````with points1 as (
SELECT (ST_DumpPoints(p.geom)).geom as geompt1 from poly as p
),
points2 as (
SELECT (ST_DumpPoints(p.geom)).geom as geompt2 from poly as p
),
lines1 as (
select st_makeline(geompt1, geompt2) as geomln from points1, points2 where ST_Disjoint(geompt1, geompt2)
)
select geomln as geom from lines1 order by st_distance(geomln, ST_SetSRID(ST_MakePoint(30.7011637,33.1748235),4326)) limit 1
``````
• `points1` and `points2` are the same so only one would do. Also connecting on path would be faster than on `ST_Distjoint`. And currently it might give wrong results with complicated linestrings. Mar 23, 2015 at 21:11