简体   繁体   English

如何找到仅在一个点相互接触并且仅列出每对一次的所有多边形对

[英]How to find all pair of polygons which only touch each other in a point and only list each pair once

How to find all pair of polygons which only touch each other in a point and only list each pair once in PostgreSQL using PostGIS? 如何使用PostGIS在PostgreSQL中找到相互接触且仅列出每对一次的所有多边形对? like the cycle shown on the picture: 就像图片中显示的周期:

在此处输入图片说明

I have written the following query: 我写了以下查询:

with kms as (
    select
      a.county as cn1,
      b.county as cn2
    from spatial.us_counties as a, spatial.us_counties as b
    where ST_Touches(a.geom, b.geom) = 'true' and a.id != b.id and ST_GeometryType(ST_Intersection(a.geom,b.geom)) = 'ST_Point'
)
/**    below is for remove reversed pairs  **/
SELECT  t1.cn1
        ,t1.cn2
FROM kms AS t1
    LEFT OUTER JOIN kms AS t2
    ON t1.cn1 = t2.cn2
    AND t1.cn2 = t2.cn1
WHERE   t2.cn1 IS NULL
      OR t1.cn1 < t2.cn1

But this query caused serious performance issue and it returned all pairs twice (reversed pair) 但是此查询导致严重的性能问题,并且两次返回所有对(反向对)

This approach is not the solution at all. 这种方法根本不是解决方案。

So is there anyone can help me with that or give me any hints? 那么,有没有人可以帮助我或者给我任何提示?

I'm not absolutely sure so I need your feedback for this answer.. 我不太确定,因此需要您的反馈意见。

Try: 尝试:

SELECT DISTINCT A.county
  FROM spatial.us_counties AS A, spatial.us_counties AS B
    WHERE ST_Touches(A.geom, B.geom) = 'true'

According to: https://postgis.net/docs/ST_Touches.html ST_Touches should return touching polygons only and not intersecting so this should eliminate the need for the where statement that checks if it's a point intersection. 根据: https: //postgis.net/docs/ST_Touches.html ST_Touches应该仅返回接触的多边形并且不相交,因此这应该不需要检查语句是否为点相交的where语句。 Selecting DISTINCT should help with the duplicates. 选择DISTINCT应该有助于重复。

Adding an index https://postgis.net/docs/using_postgis_dbmanagement.html#idm2269 to the table will help speed up the geometry queries. 在表中添加索引https://postgis.net/docs/using_postgis_dbmanagement.html#idm2269将有助于加快几何查询的速度。 Let me know if you've already done all this, I can edit my answer. 如果您已经完成所有操作,请告诉我,我可以编辑答案。

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

相关问题 无论 SQL 和关系代数中的列顺序如何,如何只列出每对元组一次? - How to list each pair of tuple only once irrespective of column order in SQL and relational algebra? 每个ID仅返回一次匹配对和非匹配对 - Return matched-pair and the non-matched pair for each ID only once 查找在StoreID中出现次数最多的对,每个STOREID仅具有该对SQL - Find the pairs that appear the most times in StoreID and each STOREID has only this pair SQL 相互朋友列,只选择一对 - Mutual Friend column, select a pair only once 仅一次将表中的每一行与其他行进行比较 - Compare each row in a table to every other row, once and once only 我需要查询以将表中的每个项目与其他所有项目配对,但仅一次 - I need query to pair every item in a table, with every other item, but only once 查找彼此之间的距离最大的一对点 - Finding the pair of points whose distance from each other is maximal 使用 SQL 查找彼此最接近的坐标对 - Finding the pair of coordinates closest to each other using SQL 查询两个表中的电子邮件地址,每个表仅列出一次 - Query two tables for email addresses, list each only once 如何仅从列表中获取值我的列表返回键值对在休眠本机查询中 - how to get only value from list my list return key value pair in hibernate native query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM