st_intersects Vs st_overlaps
Asked Answered
S

1

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?

Sinistral answered 7/5, 2012 at 10:14 Comment(4)
I suppose you are talking about PostGIS, because st_overlaps and st_intersects exists in PostGIS in exactly that writing. So I add the tag "postgis"... hope thats okMamelon
@Mamelon Considering that the accepted answer is for SQL Server, I don't think postgis is an appropriate tag.Pyrognostics
yes, this is postGIS related Query only.Sinistral
@Abhishek Sagar please reconsider marking the correct answer. I added one for PostGIS. thanksMamelon
M
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
Mamelon answered 10/6, 2015 at 21:3 Comment(3)
now, how do those differ from crosses and the example will be completeMichellmichella
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).Workday
@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 frustratingOctonary

© 2022 - 2024 — McMap. All rights reserved.