如何从 PostGIS 表中删除几何数据

How to remove geometric data from PostGIS tables

我有 2 个 table 从 shapefile 生成。一个 table 有陆地区域,另一个有水域。如何在第一个 table.

中减去第二个 table 的水域

像这样

UPDATE table1 
   SET table1.geom AS table1.geom-table2.geom

使用QGIS GEO Processing Difference可以轻松完成。

使用函数ST_Difference()。我认为,您还需要一个列作为连接两个表的外键,例如一个 id 列:

UPDATE table1 
SET table1.geom = ST_Difference(table1.geom, table2.geom) 
FROM table2 WHERE table1.id = table2.id;

如果您可以加入表格,请使用 Tommaso Di Bucchianico 的回答。这是最有效的查询。

如果您无法连接表(这是 GIS 表的常见情况),请使用 ST_Intersects ST_Difference :

UPDATE table1
SET table1.geom = ST_Difference(table1.geom, table2.geom)
FROM table2
WHERE ST_Intersects(table1.geom, table2.geom);

如果 table1.geom 类型是多边形,使用 ST_Multi :

UPDATE table1
SET table1.geom = ST_Multi(ST_Difference(table1.geom, table2.geom))
FROM table2
WHERE ST_Intersects(table1.geom, table2.geom);