外面的距离 shapely.Polygon

Distance outside shapely.Polygon

如何确定点到多边形的距离?如果该点在多边形内,则结果应为 0。

我猜是这样的:

def distance_from(poly,point):
  if poly.contains(point): return 0 
  return poly.exterior.distance(point)

我想澄清一下:

传递给 distance_from 函数的实例由 类 shapely.geometry.polygon.Polygonshapely.geometry 模块的 shapely.geometry.point.Point 组成 Shapely Python 计算几何包。 (有关形状和几何对象的更多信息,请参阅手册:Shapely Python Package, Geometric Objects)。

几何对象的distance方法returns到另一个几何对象的最小浮动距离,如手册中here所述。多边形的外部是 shapely.geometry.polygon.LinearRing 的实例,它构成了多边形的边界(尽管它不能与多边形的 boundary 属性混淆 returns LineString).因此,对于上述情况,您不需要显式使用 poly.exterior.distance(point),因为整个多边形的最小距离与其边界的最小距离相同。它们具有完全相同的形状,只是几何类型不同。

如果您只使用 poly.distance(point) 就不会那么容易出错,因为那样的话,如果兴趣点位于多边形的 interior (more info here) 内,它就直接清楚它包含它,因此您不需要显式检查,并且 interior 内的点到其自身的距离为 0。如果您尝试获取与 [= 的距离,情况会有所不同22=](如上所述,它是 LinearRing,即闭 LineString)到位于 "inner" 部分的点。在这种情况下,您将获得 "inner" 部分中的点与周围线性环之间的最小距离。

有关二元关系的更多信息(即几何对象与另一个几何对象之间的关系,涵盖 containswithintouches 等),请参阅 this part of the manual and for those interested in the underlying DE-9IM relationships (which are a way to clearly identify the way a geometric objects relates to another) refer to this 部分.

Shapely 是 GEOS suite which itself is a C++ port of JTS 的 python 包装器。在 link 功能 sheet 下的 JTS 页面上,可以找到有关如何检查和计算空间关系的更多信息