2014-11-22 75 views
3

我有以下的多邊形在PostGIS的如何將adjactent多邊形合併到1個多邊形並保留最小/最大數據?

enter image description here

每個多邊形有場「數據」值。 我想自動合併相互接觸的多邊形: 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)?

+0

我已經更新了我原來的錯誤答案。我張貼沒有檢查,因爲不在電腦前,對此感到遺憾。 – 2014-11-25 15:19:51

+0

謝謝您的時間來回答這個問題,我對postgres很陌生,語法很奇怪(來自mssql),稍後我會對其進行回顧。 Thx !!!! – Alophind 2014-11-25 22:58:00

+0

:-)我也來自MySQL,所以我明白你來自哪裏的語法是很奇怪的評論。對於空間(以及其他一些事情),Postgres/Postgis一旦適應了MySQL,就會將MySQL拋出水面。對於它的價值,像Oracle和SQL Server也支持像CTE(WITH queries)這樣的東西,所以它真的是奇怪的MySQL。 – 2014-11-25 23:02:15

回答

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; 

如果要將此保存到一個新的表,然後在WITH前加CREATE TABLE new_table AS。可能會有更有效的方法,但這是有效的。在你的問題中,你的輸入多邊形是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;