如何将相邻多边形合并为1个多边形并保留最小/最大数据?

时间:2014-11-22 18:42:20

标签: postgis esri qgis polygons

我在PostGIS中有以下多边形

enter image description here

每个多边形都有“Data”值的字段。 我想自动合并相互接触的多边形: 1-2和3-4-5-6-7

另外,如果可能的话,我希望将每个多边形的列中的最小值/最大值保持为新的多边形

Id  Data    Geom
1   8.45098 MULTIPOLYGON(((178253.411393551 665205.232423685,178248.411393552 665205.232423685,178248.411393552 665210.232423684,178253.411393551 665210.232423684,178253.411393551 665205.232423685)))
2   10.7918 MULTIPOLYGON(((178258.411393551 665205.232423685,178253.411393551 665205.232423685,178253.411393551 665210.232423684,178258.411393551 665210.232423684,178258.411393551 665205.232423685)))
3   10.7918 MULTIPOLYGON(((178263.411393552 665185.232423682,178258.411393551 665185.232423682,178258.411393551 665190.232423685,178263.411393552 665190.232423685,178263.411393552 665185.232423682)))
4   10.4139 MULTIPOLYGON(((178268.411393553 665185.232423682,178263.411393552 665185.232423682,178263.411393552 665190.232423685,178268.411393553 665190.232423685,178268.411393553 665185.232423682)))
5   7.448   MULTIPOLYGON(((178263.411393552 665180.232423684,178258.411393551 665180.232423684,178258.411393551 665185.232423682,178263.411393552 665185.232423682,178263.411393552 665180.232423684)))
6   10.2318 MULTIPOLYGON(((178268.411393553 665180.232423684,178263.411393552 665180.232423684,178263.411393552 665185.232423682,178268.411393553 665185.232423682,178268.411393553 665180.232423684)))
7   10.998  MULTIPOLYGON(((178263.411393552 665175.232423685,178253.411393551 665175.232423685,178253.411393551 665180.232423684,178258.411393551 665180.232423684,178263.411393552 665180.232423684,178263.411393552 665175.232423685)))
8   10.7548 MULTIPOLYGON(((178263.411393552 665175.232423685,178253.411393551 665175.232423685,178253.411393551 665180.232423684,178258.411393551 665180.232423684,178263.411393552 665180.232423684,178263.411393552 665175.232423685)))

最简单的方法是什么(我对QGIS / ArcMap知之甚少,并且对PostGIS有更好的了解)?

2 个答案:

答案 0 :(得分:6)

我能弄清楚如何做到这一点的唯一方法是在CTE中创建一个联合几何图表,使用ST_Dump生成单个多边形(即1-2和3-4-5-你的问题中的6)然后从原始表(我称之为多边形,因为你没有指定名称)中选择数据属性的最大值和最小值,它们与新的联合几何相交,并按相同的新联合几何。

WITH geoms (geom) as 
   (SELECT (ST_Dump(ST_Union(geom))).geom from polygons) 
SELECT max(data), min(data), g.geom
   FROM polygons p, geoms g 
   WHERE St_Intersects(s.geom, g.geom)
   GROUP BY g.geom;

如果要将其保存到新表格,请在CREATE TABLE new_table AS前添加WITH。可能有一种更有效的方法,但这有效。在您的问题中,输入多边形是MutliPolygons,因此如果您还想在输出中使用此多边形,请在新的联合几何体前面添加ST_Multi。把它们放在一起,就会得到类似的东西:

CREATE TABLE Unioned_geometries AS
  WITH geoms (geom) as 
    (SELECT (ST_Dump(ST_Union(geom))).geom from polygons) 
  SELECT max(data), min(data), ST_Multi(g.geom)
    FROM polygons p, geoms g 
    WHERE St_Intersects(s.geom, g.geom)
    GROUP BY g.geom;

答案 1 :(得分:0)

你可以使用ST_Dump和ST_Union,但是你会遇到更大数据的问题,如果你想要UNION数百万的多边形,你的几何体将非常复杂,并且PostGIS不能用于处理大而复杂的几何形状。您可以使用拓扑,或者像这样的

CREATE TABLE block_buildings AS                                                 
SELECT                                                                          
block_id                                                                        
, ST_MemUnion(geometry)                                                         

FROM houses building                                                            
, LATERAL (                                                                     
   with recursive building_block AS (                                           
      SELECT building.id                                                        
      UNION                                                                     
      SELECT building2.id FROM building_block                                   
      JOIN houses build_geom USING(id)                                          
      JOIN houses building2                                                     
      ON st_dwithin(build_geom.geometry, building2.geometry, 0.5)               
   )                                                                            
   SELECT md5(string_agg(id::text, ',' order by id)) block_id FROM building_block JOIN houses USING(id)
) block                                                                         
GROUP BY block_id                                                               
; 

LATERAL的工作方式类似于for循环,每个行都会计算子查询。 WITH recursive是常见的表表达式,它像滚雪球一样递归。由于优化,使用ST_DWithin,如果要仅合并具有共享边界的多边形或重叠,则可以对传出几何使用转储。它很慢,但没有那么多内存消耗(因为横向),它可以被优化(例如使用plpgsql),因为每个组都是针对其所有多边形计算的。但是您可以在聚合查询中使用atrs的一些聚合。如果你只创建几何体,你可以将attrs聚集到使用ST_With和ST_PointOnSurface,如果被很好地索引,它会非常快。