首页 > 解决方案 > 避免 PostGIS 中的边缘重复

问题描述

我很想在距离小于 5m 的节点之间创建边缘。以下 PostGIS 代码有效。

create table edges as
  select a.id from_gid, 
         b.id to_gid, 
         st_shortestline(a.geom_26916, b.geom_26916), 
         st_distance(a.geom_26916, b.geom_26916) as distance
  from flowers as a,
      (select * from flowers) as b
  where st_dwithin(a.geom_26916, b.geom_26916, 5);

然而,它给了我重复的边缘。如何修改代码以获得独特的边缘?谢谢!

标签: nodesgeospatialpostgisspatialedges

解决方案


同一张表被查询两次(如ab),因此它将返回记录 1 和 2 之间的一条边,以及记录 2 和 1 之间的第二条线。它还将返回同一记录的奇怪结果(一个点)。

您需要在 id 上添加过滤子句:

...
WHERE a.id < b.id AND st_dwithin(a.geom_26916, b.geom_26916, 5);

推荐阅读