【发布时间】:2023-04-11 02:28:01
【问题描述】:
我的目标是创造:
这是北美的 Voronoi 图。问题是当我运行我的代码时,错误通知我IndexError: tuple index out of range。我不知道为什么会出现这个错误,我不确定如何解决它。
这是我的代码:
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']
#cities = cities.geometry.to_crs(epsg=3857)
#na = na.to_crs(epsg=3857)
cities.crs = "EPSG:3857"
na.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()
我的代码也可以在这里找到(Google Colab Notebook):https://colab.research.google.com/drive/1oDhWsbnrwLAKXpi-f8fhJlsdxhuQrxzw
我对 geopandas 比较陌生,因此非常感谢任何帮助!
【问题讨论】:
-
@MisterMiyagi 谢谢,我相应地调整了我的代码,根据文档通过
cities.crs = "EPSG:3857"设置城市的 crs。虽然 已经 解决了 crs 错误,但我现在有一个IndexError: tuple index out of range错误。不知道如何进行; -
请为您的错误提供完整的追溯。