英文:
Is there ST_Intersects alternative that allows two(or more) polygons to share sides
问题
我正在使用ST_Intersects来检查两个多边形是否相交。查询的相关部分如下:
SELECT entity_number
FROM coordinates
WHERE ST_INTERSECTS($1, location)
它可以很好地确定一个多边形是否穿越另一个的表面:
我期望当两个多边形共享边界时,ST_Intersects会返回false,但实际上不会:
我了解了其他方法,如ST_Covers
、ST_Contains
、ST_ContainsProperly
、ST_Within
和ST_DWithin
。但我不确定哪种方法适合我的需求。
是否有一种方法允许两个多边形共享边界?
英文:
I am using ST_Intersects to check if two polygons intersect. Relevant part of my query is:
SELECT entity_number
FROM coordinates
WHERE ST_INTERSECTS($1, location)
It works well to determine if one polygon crosses the other's surface:
I expected ST_Intersects to return false when two polygons share sides, but it does not:
I read about other methods like ST_Covers, ST_Contains, ST_ContainsProperly, ST_Within ,ST_DWithin
. But i am not sure which one suits my needs.
Is there any method that allows two polygons to share sides?
答案1
得分: 2
你想要 ST_Overlaps
:
> 如果几何体 A 和 B "空间重叠",则返回 TRUE。两个几何体重叠,如果它们具有相同的维度,每个都至少有一个点不与另一个共享(或等效地,它们互不包含对方),并且它们内部交集的维度相同。重叠关系是对称的。
英文:
You want ST_Overlaps
:
> Returns TRUE if geometry A and B "spatially overlap". Two geometries overlap if they have the same dimension, each has at least one point not shared by the other (or equivalently neither covers the other), and the intersection of their interiors has the same dimension. The overlaps relationship is symmetrical.
答案2
得分: 1
正如Laurenz所指出的,ST_Overlaps
是你要找的函数。然而,通常仅仅检查多边形是否重叠是不够的,还需要查看它们重叠的程度。换句话说,几何形状a与几何形状b重叠多少?ST_Intersection
返回两个几何形状的交集结果,因此你可以对生成的多边形使用 ST_Area
函数,然后将其与函数中给定的一个多边形进行比较,例如给定以下多边形...
... 这将是 ST_Intersection
的结果
以下是如何计算重叠百分比的方法:
SELECT
ST_Area(ST_Intersection(g1,g2))/ST_Area(g1)*100 AS perc_g1_overlap,
ST_Intersection(g1,g2)
FROM t;
这将返回重叠百分比以及交集的几何形状。
英文:
As pointed out by Laurenz, ST_Overlaps
is what you're looking for. However, quite often it does not suffice to simply check if polygons do overlap, but also "how much" they overlap. In other words, how much of geometry a overlaps with geometry b? ST_Intersection
returns a polygon that is the result of the intersection of two geometries, so you can ST_Area
this generated polygon and compare it with one of the polygons given to the function, e.g. given these polygons ..
... this would be the result of ST_Intersection
And this is how you could calculate the percentage of the overlap
SELECT
ST_Area(ST_Intersection(g1,g2))/ST_Area(g1)*100 AS perc_g1_overlap,
ST_Intersection(g1,g2)
FROM t;
perc_g1_overlap | st_intersection
--------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16.555309365086124 | 0103000020E6100000010000000500000054435ACF861E12C02068AC4B11214B4054435ACFD60A12C02068AC4B11214B4054435ACFD60A12C04B266CCD791F4B4054435ACF861E12C04B266CCD791F4B4054435ACF861E12C02068AC4B11214B40
(1 row)
Demo: db<>fiddle
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论