【发布时间】:2015-02-17 08:17:39
【问题描述】:
根据一组点,我使用scipy 构建了 Voronoi 镶嵌:
from scipy.spatial import Voronoi
vor = Voronoi(points)
现在我想从 Voronoi 算法创建的区域构建一个Polygon in Shapely。问题是 Polygon 类需要一个逆时针顶点列表。虽然我知道如何order these vertices,但我无法解决问题,因为通常这是我的结果:
(重叠多边形)。这是代码(一个随机示例):
def order_vertices(l):
mlat = sum(x[0] for x in l) / len(l)
mlng = sum(x[1] for x in l) / len(l)
# https://stackoverflow.com/questions/1709283/how-can-i-sort-a-coordinate-list-for-a-rectangle-counterclockwise
def algo(x):
return (math.atan2(x[0] - mlat, x[1] - mlng) + 2 * math.pi) % 2*math.pi
l.sort(key=algo)
return l
a = np.asarray(order_vertices([(9.258054711746084, 45.486245994138976),
(9.239284166975443, 45.46805963143515),
(9.271640747003861, 45.48987234571072),
(9.25828782103321, 45.44377372506324),
(9.253993275176263, 45.44484395950612),
(9.250114174032936, 45.48417979682819)]))
plt.plot(a[:,0], a[:,1])
我该如何解决这个问题?
【问题讨论】:
-
这看起来不像顶点的逆时针顺序。你确定你正确地实现了排序算法吗?
-
@Kevin 我认为我正确地实现了它......我用一个例子更新了这个问题
-
是生成图表的代码吗?或者它是一个单独的例子?
-
@Kevin 该代码生成了图表 :)
标签: python gis voronoi shapely