python - 将北美分成几 block

标签 python geopandas

我正在尝试创建北美 map 的沃罗诺伊图,这意味着根据首都的位置将国家有效地分割成碎片。为此,我使用 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 文档。然而,当我运行它时,我收到以下错误: enter image description here

最佳答案

您收到的错误是由于您正在提取的城市信息包含很少的北美城市,或者它们没有被正确识别为在北美边界内。你的问题是关于创建一个基于大写字母的 Voronoi 图,所以我已经包含了 a link to a data set for US capitals这样您就可以测试具有可靠数量的城市的示例:

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()

制作:

enter image description here

对于北美,您可以下载a cities 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()

制作:

enter image description here

关于python - 将北美分成几 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60470648/

相关文章:

python - 使用 __str__ 表示在容器中打印对象

python - 如何从 python 转换为 matlab - a*dx.min( )/abs(v) -

python - 关于 read_file() 的 Geopandas 警告

python - 如何减少 Python 绘图中的空白?

python - Python 中 NOT、AND、OR 的逻辑运算符的优先级(运算顺序)

python - 要附加到的空 numpy 数组

pandas - 如何在由分区分隔的 map 上绘制散点图?

Python Pandas "Error: Could not install packages due to an OSError: No such file or directory:"

pandas - Geopandas 绘图作为子图

python - 限制 python 3 中的无效输入