我正在尝试创建北美地图的voronoi图,这意味着根据其首都的位置有效地将国家切成碎片。为此,我使用Geopandas获取北美的地理数据,然后使用GeoVoronoi库创建一个Voronoi图:
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.ops import cascaded_union
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords, points_to_coords
logging.basicConfig(level=logging.INFO)
geovoronoi_log = logging.getLogger('geovoronoi')
geovoronoi_log.setLevel(logging.INFO)
geovoronoi_log.propagate = True
#
# load geo data
#
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
# focus on South America, convert to World Mercator (unit: meters)
north_am = world[world.continent == 'North America'].to_crs(epsg=3395)
cities = cities.to_crs(north_am.crs) # convert city coordinates to same CRS!
# create the bounding shape as union of all South American countries' shapes
north_am_shape = cascaded_union(north_am.geometry)
north_am_cities = cities[cities.geometry.within(north_am_shape)] # reduce to cities in South America
#
# calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them
#
# convert the pandas Series of Point objects to NumPy array of coordinates
coords = points_to_coords(north_am_cities.geometry)
# calculate the regions
poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, north_am_shape)
#
# Plotting
#
fig, ax = subplot_for_map()
plot_voronoi_polys_with_points_in_area(ax, north_am_shape, poly_shapes, pts)
ax.set_title('Cities data for South America from GeoPandas\nand Voronoi regions around them')
plt.tight_layout()
plt.savefig('using_geopandas.png')
plt.show()
这些代码大部分直接取自 Geovoronoi 文档。然而,当我运行它时,我收到以下错误:
aluckdog
相关分类