[英]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.