简体   繁体   English

查找相邻多边形 - postgis查询

[英]Finding neighbouring polygons - postgis query

The problem: I have a table full of polygons (districts of a country). 问题:我有一张满是多边形的桌子(一个国家的区域)。 Some of these polygons have a certain attribute that may or may not be checked. 这些多边形中的一些具有可能或可能不被检查的特定属性。 In this case the attribute is called "spread" and the "checked" value is 1. 在这种情况下,该属性称为“spread”,“checked”值为1。

Now I'd like to run a query that finds all the "checked" polygons. 现在我想运行一个查找所有“已检查”多边形的查询。 Something like: 就像是:

SELECT * FROM gemstat WHERE spread = 1

and then I'd like to set the "spread" attribute to "1" in every neighbouring polygon that isn't already checked. 然后我想在每个尚未检查的相邻多边形中将“spread”属性设置为“1”。 (I also want to set a second attribute, but that's just a minor addition) (我也想设置第二个属性,但这只是一个小的补充)

First, let's start with a query that selects all polygons that are neighbours of a polygon with a spread-value = 1 首先,让我们从一个查询开始,该查询选择所有多边形的多边形,其中spread-value = 1

SELECT (b."GEM_NR")
FROM gemstat_simple5 as a
JOIN gemstat_simple5 as b
ON ST_Touches((a.the_geom),b.the_geom)
where a.spread =1;

This query returns all polygons that are neighbours of a polygon with spread = 1 此查询返回所有多边形的多边形,其中spread = 1

Now I want to update the table based on the results of that subquery. 现在我想根据该子查询的结果更新表。 This is realized with this piece of code provided by John Powell aka Barca (see answer and also comments below): 这是通过John Powell aka Barca提供的这段代码实现的(请参阅下面的答案和评论):

Update gemstat_simple5 gem set spread=1, time=2
FROM (
   SELECT (b."GEM_NR")
   FROM gemstat_simple5 as a,
   gemstat_simple5 as b
   WHERE ST_Touches(a.the_geom, b.the_geom) 
   AND a."GEM_NR" != b."GEM_NR"
   AND a.spread = 1
) as subquery
WHERE gem."GEM_NR" = subquery."GEM_NR"

Run this query and it will set the attributes spread to 1 and time to 2 of the adjacent polygons while not touching the original polygons with spread = 1. Therefore it poses the perfect answer to my question. 运行此查询,它将设置扩展为1和时间为2的相邻多边形的属性,而不触及spread = 1的原始多边形。因此,它为我的问题提供了完美的答案。

If you are asking how to update a table based on a subquery finding only those polygons that have a neighbor (ie, they touch another polygon), then the following should work for you. 如果您询问如何基于子查询更新表,只查找具有邻居的多边形(即,它们触摸另一个多边形),则以下内容应该适合您。

Update gemstat_simple5 gem set spread=1, time=2
  FROM (
     SELECT (b."GEM_NR")
       FROM gemstat_simple5 as a,
            gemstat_simple5 as b
       WHERE ST_Touches(a.the_geom, b.the_geom) 
       AND a."GEM_NR" != b."GEM_NR"
       AND a.spread = 1
     ) as subquery
 WHERE gem."GEM_NR" = subquery."GEM_NR"

Note, I have put AND a."GEM_NR" < b."GEM_NR", which both avoids the case where AND a."GEM_NR" = b."GEM_NR", ie, itself, which you want to avoid, and also reduces the pair wise comparisons by half. 注意,我已经把AND a。“GEM_NR”<b。“GEM_NR”,它们都避免了AND a。“GEM_NR”= b。“GEM_NR”的情况,即你想要避免的GEM_NR,并且还减少了一半明智的比较。 I have also use the a, b Where approach rather then a join b on st_touches, which is the same, but I find more confusing with spatial joins. 我也使用a,b where方法而不是st_touches上的连接b,这是相同的,但我发现空间连接更令人困惑。 Finally, you just equate the table GEM_NR you are updating with those found in the sub query. 最后,您只需将要更新的表GEM_NR与在子查询中找到的表等同起来。

The direct answer is 直接的答案是

SELECT a.*
FROM polygon1 as a
JOIN polygon1 as b
ON st_intersects((st_buffer(a.the_geom,0.00001)),b.the_geom) 
where b.id = 561334;

Tested in my Machine and it also gives the id which you give in where class. 在我的机器中测试过,它还提供了你在哪个类中给出的id。 My polygon table's Spatial Reference - EPSG:4326... 我的多边形表格的空间参考 - EPSG:4326 ...

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM