[GIS] Postgis st_intersects fails on an obtained point on the line with the line itself

postgispostgresql

I am trying to split a line from its intersection with another line. I know I can use st_split directly here but I wish to split a line from several positions where it intersects other lines. What I discover is the following:

I know that lines 1 and 31 intersect

select st_intersects(a.geom,b.geom) from dumped_roads as a,
       dumped_roads as b where a.gid=1 and b.gid=31;

Output:

st_intersects 
---------------
t
(1 row)

Also, st_intersection(a.geom,b.geom) returns an intersection point.

But when I do,

select st_intersects(a.geom, st_intersection(a.geom,b.geom))
from
  dumped_roads as a,dumped_roads as b where a.gid=1 and b.gid=31;

st_intersects 
---------------
f
(1 row)

I can't understand what the problem is. Although a.geom && st_intersection(a.geom,b.geom) returns true but that wouldn't help me in splitting the line with the point obtained from intersection.

If it is indeed how it is, is there any way out on breaking a line from an obtained point which physically lies on the line, but is not in the linestrings point set?

Best Answer

This is a robustness issue. ST_Intersection creates a new point that is probably not a vertex from either of the original LineStrings, but it probably is close to the line. A more robust test of intersection is to check if the distance is zero.

SELECT ST_AsText(ST_Intersection(a.geom, b.geom)),
       ST_Intersects(a.geom, ST_Intersection(a.geom, b.geom)),
       ST_Distance(a.geom, ST_Intersection(a.geom, b.geom))
FROM (select 'LINESTRING (156 258, 607 488)'::geometry as geom) as a,
     (select 'LINESTRING (105 418, 737 218)'::geometry as geom) as b;

-[ RECORD 1 ]-+-----------------------------------------
st_astext     | POINT(330.074206147054 346.773985396502)
st_intersects | f
st_distance   | 0

cross

Related Question