Python:找到一种计算多边形“内部质心”(X,Y)的方法

时间:2012-12-06 13:28:12

标签: python geolocation geometry shapefile

我有一个多边形(在Shapely对象中转换)。我的目标是在图例后计算“内部质心”(也称为“表面上的点”)(返回x,y值)和“质心”(返回x,y值):

enter image description here

from shapely.geometry import Polygon

ref_polygon = Polygon(points)
# get the x and y coordinate of the centroid
ref_polygon.centroid.wkt
'POINT (558768.9293489187300000 6361851.0362532493000000)'

我的问题是一些程序员已经在Python中开发了一个函数来计算内部质心或知道一些模块来做这件事。

提前致谢

使用的点(多边形的顶点)是:

points = [(560036.4495758876, 6362071.890493258),
          (560036.4495758876, 6362070.890493258),
          (560036.9495758876, 6362070.890493258),
          (560036.9495758876, 6362070.390493258),
          (560037.4495758876, 6362070.390493258),
          (560037.4495758876, 6362064.890493258),
          (560036.4495758876, 6362064.890493258),
          (560036.4495758876, 6362063.390493258),
          (560035.4495758876, 6362063.390493258),
          (560035.4495758876, 6362062.390493258),
          (560034.9495758876, 6362062.390493258),
          (560034.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.890493258),
          (560030.4495758876, 6362061.890493258),
          (560030.4495758876, 6362061.390493258),
          (560029.9495758876, 6362061.390493258),
          (560029.9495758876, 6362060.390493258),
          (560029.4495758876, 6362060.390493258),
          (560029.4495758876, 6362059.890493258),
          (560028.9495758876, 6362059.890493258),
          (560028.9495758876, 6362059.390493258),
          (560028.4495758876, 6362059.390493258),
          (560028.4495758876, 6362058.890493258),
          (560027.4495758876, 6362058.890493258),
          (560027.4495758876, 6362058.390493258),
          (560026.9495758876, 6362058.390493258),
          (560026.9495758876, 6362057.890493258),
          (560025.4495758876, 6362057.890493258),
          (560025.4495758876, 6362057.390493258),
          (560023.4495758876, 6362057.390493258),
          (560023.4495758876, 6362060.390493258),
          (560023.9495758876, 6362060.390493258),
          (560023.9495758876, 6362061.890493258),
          (560024.4495758876, 6362061.890493258),
          (560024.4495758876, 6362063.390493258),
          (560024.9495758876, 6362063.390493258),
          (560024.9495758876, 6362064.390493258),
          (560025.4495758876, 6362064.390493258),
          (560025.4495758876, 6362065.390493258),
          (560025.9495758876, 6362065.390493258),
          (560025.9495758876, 6362065.890493258),
          (560026.4495758876, 6362065.890493258),
          (560026.4495758876, 6362066.890493258),
          (560026.9495758876, 6362066.890493258),
          (560026.9495758876, 6362068.390493258),
          (560027.4495758876, 6362068.390493258),
          (560027.4495758876, 6362068.890493258),
          (560027.9495758876, 6362068.890493258),
          (560027.9495758876, 6362069.390493258),
          (560028.4495758876, 6362069.390493258),
          (560028.4495758876, 6362069.890493258),
          (560033.4495758876, 6362069.890493258),
          (560033.4495758876, 6362070.390493258),
          (560033.9495758876, 6362070.390493258),
          (560033.9495758876, 6362070.890493258),
          (560034.4495758876, 6362070.890493258),
          (560034.4495758876, 6362071.390493258),
          (560034.9495758876, 6362071.390493258),
          (560034.9495758876, 6362071.890493258),
          (560036.4495758876, 6362071.890493258)]

1 个答案:

答案 0 :(得分:7)

术语“内部质心”在计算几何中并不是一个明确定义的术语,但从你的帖子中可以清楚地看出,你想要计算一个完全在多边形内部的点(在它和它附近的边缘之间有一些边界) ),它与真正的质心相当接近。

您可以尝试以下几个想法:

算法A

  1. 生成多边形的所有内部对角线。

  2. 对于每个内部对角线,请考虑中点,并根据距离最近边缘的距离以及它与质心的接近程度给出一个分数。

  3. 选择得分最高的中点。

  4. 多边形的内部对角线是连接两个完全与多边形相对应的非相邻顶点的线。可以在O( m + n 日志中生成具有 n verticies的多边形的 m 内部对角线的集合log n )使用相当复杂的算法due to Hershberger,或使用更简单的算法在O( n 2 )中。

    算法B

    1. 对多边形进行三角测量。

    2. 对于三角剖分中的每个三角形,考虑三角形的质心(或者可能是incenter?),并根据距离最近边缘的距离以及它的接近程度给出一个分数是多边形的质心。

    3. 选择得分最高的三角形中心。

    4. 具有 n 顶点的简单多边形可以使用基于分解为单调多边形due to Chazelle或O(O)的算法在O( n )中进行三角剖分。 n 2 )使用更简单的方法,如“ear clipping”。