来自shapelydocumentation:
Polygon 构造函数有两个位置参数。第一个是 (x, y[, z]) 点元组的有序序列,其处理方式与 LinearRing 情况完全相同。
此外,多边形边框不能与自身相交。
因此,一个点的邻居就是多边形定义中的前后点。
通过外部和内部属性访问组件环。
list(polygon.exterior.coords)
返回:
[(10, 10), (10, 20), (20, 10), (20, 20)]
角点的邻居是它之前和之后的点;我们需要以循环方式处理这个点列表:
def get_two_neighbors(point, polygon):
"""retrieve the two neighboring points of point in the polygon
:point: a tuple representing a point of the polygon
:polygon: a shapely Polygon
return: a tuple of the two points immediately neighbors of point
"""
points = list(polygon.exterior.coords)
ndx = points.index(point)
two_neighbors = points[(ndx-1)%len(points)], points[(ndx+1)%len(points)]
return two_neighbors
在参考的例子中:
sample = Polygon(((10, 10), (10, 20), (20, 10), (20, 20)))
get_neighbors((10, 10), polygon)
返回:
((20, 20), (10, 20))