【问题标题】:How to combine two spatial functions in one union query如何在一个联合查询中组合两个空间函数
【发布时间】:2015-01-10 03:03:03
【问题描述】:

我想用PostgreSQL找出同一个查询的交集和差异(其他数据库的逻辑应该是一样的)。

如何在同一个查询中结合 ST_Intersection 和 ST_Difference 并返回结果。

我试过了:

select 1 as keys,(st_intersection(tale1.the_geom,table2.the_geom)) from table1,table2 
UNION select ,st_difference(table1.the_geom,table2.the_geom) from table1,table2;

但是在select ^ ,st_difference(....

【问题讨论】:

  • 这个问题似乎已经做了一些努力,所以我努力回答它,甚至认为它似乎是当前一系列分配问题的一部分。

标签: mysql postgresql postgis psql


【解决方案1】:

您有几个问题:您在第一次选择中有 2 列,而在第 2 次选择中只有一列,而且您在 ST_Difference 的 from 中有一个逗号,大概您想在其中放置 2 或一些东西。您可以通过在 With 查询中从 table1 和 table2 中选择 geom,然后合并它们来稍微简化查询,例如,

 with geoms (geom1, geom2) as 
   (select a.the_geom, b.the_geom from table1 a, table2 b) 
 select 1, st_intersection(geom1, geom2) from geoms where st_intersects(geom1, geom2)
   union 
 select 2, st_difference(geom1, geom2) from geoms where st_intersects(geom1, geom2);

请注意在两个查询中添加了 where ST_Intersects(geom1, geom2) ——这将把交叉点和差异限制在至少有一些交叉点的​​多边形上,避免笛卡尔连接。理想情况下,您也应该在其中设置 where a.id != b.id,以避免自相交/差异,但我希望您明白这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 2015-03-18
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多