Разделение Северной Америки - PullRequest
0 голосов
/ 21 марта 2020

Моя цель - создать:

enter image description here

Это диаграмма Вороного для Северной Америки. Проблема в том, что когда я запускаю свой код, ошибка сообщает мне 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

Я относительно новичок в гео pandas, поэтому любая помощь очень ценится!

1 Ответ

0 голосов
/ 08 мая 2020

В случае, если кто-то наткнется на это, график может быть создан путем установки правильной проекции:

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.crs = ({'init': 'epsg:3857'}) 
cities = cities.to_crs({'init': 'epsg:4326'}) 
na_shape = cascaded_union(na.geometry)
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()

Файл города здесь .

...