【问题标题】:Get the neighbours of a point in polygon using Shapely使用 Shapely 获取多边形中一个点的邻居
【发布时间】:2017-12-01 02:24:30
【问题描述】:

假设我有一个包含点 a、b、c、d 的多边形

sample = Polygon(((10, 10), (10, 20), (20, 10), (20, 20)))

谁能告诉我如何只找到它的邻居,而不是对角线的点。在示例中,如果我在 (10,10),我需要得到 (10,20) 和 (20,10) 而不是 (20,20)。任何帮助将不胜感激。

【问题讨论】:

    标签: python polygon computational-geometry shapely


    【解决方案1】:

    来自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))
    

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 1970-01-01
      • 2012-11-09
      • 2013-12-26
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多