Ошибка, которую вы получаете, связана с тем, что информация о городе, которую вы запрашиваете, содержит несколько городов Северной Америки или что они не распознаются должным образом в пределах границ Северной Америки. Ваш вопрос касается создания диаграммы Вороного на основе столиц, поэтому я включил ссылку на набор данных для столиц США , чтобы вы могли протестировать пример с достоверным количеством городов:
import matplotlib.pyplot as plt
import numpy as np
import geopandas as gpd
from geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords
cities = gpd.read_file('us-state-capitals.csv')
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
usa = world[world.name == 'United States of America']
usa = usa.to_crs(epsg=3857)
usa_shape = usa.iloc[0].geometry
coords = np.array(list(zip(cities.Shape_X,cities.Shape_Y)), dtype='float')
poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, usa_shape)
fig, ax = subplot_for_map()
plot_voronoi_polys_with_points_in_area(ax, usa_shape, poly_shapes, coords)
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()
Производство:
Для Северной Америки вы можете скачать города CSV и использовать следующий код:
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
cities = gpd.read_file('world_populated_cities.csv')
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
na = world[world.continent == 'North America']
na = na.to_crs(epsg=3857)
cities.geometry.to_crs(epsg=3857)
na_shape = cascaded_union(na.geometry)
cities = cities.to_crs(na.crs) # convert city coordinates to same CRS!
cities = cities[cities.geometry.within(na_shape)]
coords = points_to_coords(cities.geometry)
poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, na_shape)
fig, ax = subplot_for_map()
plot_voronoi_polys_with_points_in_area(ax, na_shape, poly_shapes, coords)
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()
Производство: