Python: 如何将Voronoi 单元扩展到几何的边界?

Python: how to extend Voronoi cells to the boundary of the geometry?

我有两个 geopandas 数据框 dfMdcentersdfMd 由多边形构成,而 centers 由点构成。 Here link 到文件。

f,ax=plt.subplots()
dfMd.plot(ax=ax, column='volume')
centers.plot(ax=ax, color='red')

我想生成扩展到整个几何体的点的 Voronoi 流苏。这就是我正在做的事情:

from shapely.geometry import mapping
g = [i for i in centers.geometry]
coords = []
for i in range(0, len(g)):
    coords.append(mapping(g[i])["coordinates"]) # for first feature/row

from libpysal.cg.voronoi  import voronoi, voronoi_frames

regions, vertices = voronoi(coords)
region_df, point_df = voronoi_frames(coords)


fig, ax = plt.subplots()
region_df.plot(ax=ax, color='white',edgecolor='black', lw=3)
dfMd.plot(ax=ax, column='volume',alpha=0.1)
point_df.plot(ax=ax, color='red')

fig, ax = plt.subplots()
region_df.plot(ax=ax, color='white',edgecolor='black', lw=3)
dfMd.plot(ax=ax, column='volume',alpha=0.1)
point_df.plot(ax=ax, color='red')

如何将 Voronoi 区域扩展到 dfMd 的外部边界?

您需要 voronoi_frames() 中的选项 clip=box()。相关代码如下

from shapely.geometry import box

region_df, point_df = voronoi_frames(coords, clip=box(-4.2, 40.15, -3.0, 40.85))