12

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?

alfonx
  • 6,936
  • 2
  • 49
  • 58
Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44

1 Answers1

22

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
alfonx
  • 6,936
  • 2
  • 49
  • 58
  • 1
    now, how do those differ from crosses and the example will be complete – slashdottir Oct 14 '15 at 21:45
  • 4
    I know this question is a fossil in internet epochs, but it's also worth noting that `ST_Intersects(a,b)` is T for geometries a,b that **touch** (i.e., no point interior to a is interior to b, and vice versa, but they share points on their respective boundaries). My white whale is an undocumented PostGIS function will enable me to stop having to write `where ST_Intersects(a.g1, b.g1) and not ST_Touches(a.g1, b.g1)` (I've got it set as a code snippet, so I only have to write `pg_tni[tab]`... but that's not the point). – GT. Apr 27 '17 at 01:48
  • @GT I'm struggling with this currently - adding `and not st_touches(geom, geom)` unfortunately didn't work for me. Query is still returning geoms that share a border instead of overlapping/intersecting. So frustrating – spops Apr 20 '18 at 17:29