首页 > 解决方案 > Python:如何将 Voronoi 单元扩展到几何边界?

问题描述

我有两个 geopandas 数据框dfMdcenters. dfMd由多边形组成,而centers由点组成。这里是文件的链接。

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

标签: pythongeopandasvoronoi

解决方案


您需要clip=box()voronoi_frames(). 相关代码如下。

from shapely.geometry import box

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

沃罗诺伊


推荐阅读