检查 Polygon 是否包含 Shapely 中的 MultiPoint returns 意外结果

Checking if a Polygon contains MultiPoint in Shapely returns unexpected results

您能解释一下 Shapely contains 方法的行为吗?为什么第一个结果低于False,第二个结果低于True

from shapely.geometry import Polygon, Point, MultiPoint

poly = Polygon([[0,0], [2, 0], [2, 2], [0, 2]])

poly.contains(MultiPoint([Point(2,2)]))
Out[3]: False

poly.contains(MultiPoint([Point(2,2), Point(1,1)]))
Out[4]: True

poly.contains(MultiPoint([Point(2,2), Point(1,1), Point(3,3)]))
Out[5]: False

引用 docs on object.contains(other):

Returns True if no points of other lie in the exterior of the object and at least one point of the interior of other lies in the interior of object.

所以,事实上,一切都按预期进行。

1) 当你检查poly.contains(MultiPoint([Point(2,2)]))时,点不在poly的内部,而是在它的边界上。因此它returns False

2) 当检查poly.contains(MultiPoint([Point(2,2), Point(1,1)]))时,MultiPoint对象没有一点位于poly的外部,只有一个点位于其内部。这满足给定的条件。

3) 对于 poly.contains(MultiPoint([Point(2,2), Point(1,1), Point(3,3)])) 的情况,有一个点位于 poly 的外部,因此 False 作为结果。


P.S.: 你在评论中写道:

Regarding 2) the point Point(2,2) lies in the exterior of poly, i.e. poly.exterior.contains(Point(2,2)) returns True.

多边形的这个exterior属性实际上是指多边形的外部LinearRing。并且不等于外点集。 Docs on Polygon 说:

Component rings are accessed via exterior and interiors properties.

内部、边界和外部点集定义here如下:

A Surface has an interior set consisting of the infinitely many points within (imagine a Curve dragged in space to cover an area), a boundary set consisting of one or more Curves, and an exterior set of all other points including those within holes that might exist in the surface.