PostGIS:合并多边形并保持边界

时间:2014-06-12 12:52:47

标签: postgis spatial qgis postgresql-9.3

我们正在尝试合并存储在PostGIS 2.1数据库中的两个Multipolygons,而不会丢失每个Multipolygon中包含的边界。

我们的空间数据符合以下条件。

-- Check whether the polygons share points (boundaries?)
-- ST_Intersects:
-- Returns TRUE if the Geometries/Geography "spatially intersect in 2D" - (share any portion of space)
-- and FALSE if they don't (they are Disjoint).
ST_Intersects(higher_geom,lower_geom) = TRUE    

-- ST_Crosses:
-- Returns TRUE if the supplied geometries have some, but not all, interior points in common.
ST_Crosses(higher_geom,lower_geom) = FALSE

-- Since ST_Crosses would return FALSE if the polygons have all interior points in common
-- we have to ensure this is not the case
ST_Within(higher_geom,lower_geom) = FALSE

如果我们尝试使用以下查询聚合列lower_geom和higher_geom(两者都是MultiPolygon类型),则ST_Union的结果缺少原始多边形的边框。

SELECT
    ST_Union(lower_geom, higher_geom)
FROM
    myTable

为了更清楚,我们添加了截图。在我们期望的结果中,绿色和红色多重多边形都应该包含在一个仍然包含所有边界的新多重多边形中。

enter image description here

有没有人有想法!?

提前致谢, Cord&马丁

1 个答案:

答案 0 :(得分:2)

这适用于我一起测试的几个测试多边形。它使用ST_Dump技巧取消合并几何集合,否则由内部查询产生,别名为表c,然后使用ST_Multi(ST_Collect(geom ...)重新收集几何。内部查询组合两组的交集。几何与交叉和联合的差异。

select ST_multi(ST_Collect(d.geom)) 
  from (select (ST_Dump(c.geom)).geom 
    from (select ST_Collect(ST_Intersection(a.geom, b.geom),
            ST_SymDifference(ST_Intersection(a.geom, b.geom),
            ST_Union(a.geom, b.geom))) as geom 
        from lower_geom a, higher_geom b)
   as c)
 as d;

有一种更优雅,更有效的方式来写这个,但我想在尝试之前知道这是否适用于您的数据。