PostGIS Line Intersection – How to Find Intersection Between Two Line Segments

intersectionlinepointpostgis

I am trying to find the intersection point of two lines of which I know 2 points.

For example, I have these 2 line segments:

select st_geometryfromtext('LINESTRING (4.50554487780521 51.922119943633575, 4.504656820078167 51.92231795217855)', 4326),
st_geometryfromtext('LINESTRING (4.505476754241158 51.92221504789901, 4.505379267847784 51.92221833721103)', 4326)

As it can be seen on the map,they do not have an intersection point:

enter image description here

What I am interested in finding is the intersection point of the lines which these segments belong to, as shown below:

enter image description here

Best Answer

Just extend (extrapolate) one of your lines and get the intersection point as:

select st_intersection((select st_geometryfromtext('LINESTRING (4.50554487780521 51.922119943633575, 4.504656820078167 51.92231795217855)', 4326)),
(select ST_MakeLine(ST_TRANSLATE(a, sin(az1) * len, cos(az1) * 
len),ST_TRANSLATE(b,sin(az2) * len, cos(az2) * len))
  FROM (
    SELECT a, b, ST_Azimuth(a,b) AS az1, ST_Azimuth(b, a) AS az2, ST_Distance(a,b) + 1 AS len
      FROM (
        SELECT ST_StartPoint(st_geometryfromtext('LINESTRING (4.505476754241158 51.92221504789901, 4.505379267847784 51.92221833721103)', 4326)) AS a, 
        ST_EndPoint(st_geometryfromtext('LINESTRING (4.505476754241158 51.92221504789901, 4.505379267847784 51.92221833721103)', 4326)) AS b
          FROM ST_MakeLine(ST_MakePoint(1,2), ST_MakePoint(3,4)) AS the_geom
    ) AS sub
) AS sub2));

extrapolation example from How to extend a straight line in postgis?

Related Question