Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

st_intersects Vs st_overlaps

What is the difference between these two queries :

select a.gid, sum(length(b.the_geom)) 
  from polygons as a 
     , roads as b 
 where st_intersects(a.the_geom,b.the_geom) 
 group by a.gid ;

select a.gid, sum(length(b.the_geom)) 
  from polygons as a 
     , roads as b 
 where st_overlaps(a.the_geom,b.the_geom) 
 group by a.gid ;

Where the first query is giving the correct output whereas the second query retrieves no rows at all. The road that intersects the polygons also overlaps it, right?

like image 530
Abhishek Sagar Avatar asked May 07 '12 10:05

Abhishek Sagar


1 Answers

From the documentation of PostGIS

http://postgis.net/docs/ST_Intersects.html

If a geometry or geography shares any portion of space then they intersect. Overlaps, Touches, Within all imply spatial intersection. If any of the aforementioned returns true, then the geometries also spatially intersect.

http://postgis.net/docs/ST_Overlaps.html

Returns TRUE if the Geometries "spatially overlap". By that we mean they intersect, but one does not completely contain another.

The difference is: If two geometries overlap 100%, they do not overlap any more.

Here is a POSTGIS example:

SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Intersects(a, b) As a_intersects_b, ST_Contains(b, a) As b_contains_a
FROM (SELECT 
    ST_Polygon(ST_GeomFromText('LINESTRING(1 1,3 1,3 3,1 1)'), 4326)  As a,
    ST_Polygon(ST_GeomFromText('LINESTRING(1 1,3 1,3 3,1 1)'), 4326)  As b)
    As foo;
    -- INTERSECT is TRUE, OVERLAP is FALSE because B equals A

    SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Intersects(a, b) As a_intersects_b, ST_Contains(b, a) As b_contains_a
FROM (SELECT 
    ST_Polygon(ST_GeomFromText('LINESTRING(1 1,3 1,3 3,1 1)'), 4326)  As a,
    ST_Polygon(ST_GeomFromText('LINESTRING(1 1,4 1,4 4,1 1)'), 4326)  As b)
    As foo;
    -- INTERSECT is TRUE, OVERLAP is FALSE because B contains A

    SELECT ST_Overlaps(a,b) As a_overlap_b, ST_Intersects(a, b) As a_intersects_b, ST_Contains(b, a) As b_contains_a
FROM (SELECT 
    ST_Polygon(ST_GeomFromText('LINESTRING(0 0,2 0,2 2,0 0)'), 4326)  As a,
    ST_Polygon(ST_GeomFromText('LINESTRING(1 1,3 1,3 3,1 1)'), 4326)  As b)
    As foo;
    -- INTERSECT is TRUE, OVERLAP is TRUE because not all of A intersects B and not all of B intersects A
like image 76
alfonx Avatar answered Sep 23 '22 11:09

alfonx