11

这两个查询有什么区别:

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 ;

第一个查询给出正确的输出,而第二个查询根本不检索任何行。与多边形相交的道路也与它重叠,对吧?

4

1 回答 1

22

来自 PostGIS 的文档

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

如果几何或地理共享空间的任何部分,则它们相交。Overlaps, Touches, Within 都暗示着空间的交叉。如果上述任何一个返回 true,则几何图形也在空间上相交。

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

如果几何“空间重叠”,则返回 TRUE。我们的意思是它们相交,但一个并不完全包含另一个。

区别在于:如果两个几何图形 100% 重叠,则它们不再重叠。

这是一个 POSTGIS 示例:

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
于 2015-06-10T21:03:52.690 回答