有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

huangapple go评论74阅读模式
英文:

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的替代方法,允许两个(或更多)多边形共享边界?

我期望当两个多边形共享边界时,ST_Intersects会返回false,但实际上不会:

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

我了解了其他方法,如ST_CoversST_ContainsST_ContainsProperlyST_WithinST_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:

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

I expected ST_Intersects to return false when two polygons share sides, but it does not:

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

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_Intersects的替代方法,允许两个(或更多)多边形共享边界?

... 这将是 ST_Intersection 的结果

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

以下是如何计算重叠百分比的方法:

SELECT 
  ST_Area(ST_Intersection(g1,g2))/ST_Area(g1)*100 AS perc_g1_overlap,
  ST_Intersection(g1,g2)
FROM t;

这将返回重叠百分比以及交集的几何形状。

演示:db<>fiddle

英文:

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 ..

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

... this would be the result of ST_Intersection

有没有ST_Intersects的替代方法,允许两个(或更多)多边形共享边界?

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

huangapple
  • 本文由 发表于 2023年6月1日 19:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定