亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將北美劃分為碎片

將北美劃分為碎片

千萬里不及你 2022-08-16 18:50:34
我正在嘗試創建北美地圖的voronoi圖,這意味著根據其首都的位置有效地將國家切成碎片。為此,我使用Geopandas獲取北美的地理數據,然后使用GeoVoronoi庫創建一個Voronoi圖:import matplotlib.pyplot as pltimport geopandas as gpdfrom shapely.ops import cascaded_unionfrom geovoronoi.plotting import subplot_for_map, plot_voronoi_polys_with_points_in_areafrom geovoronoi import voronoi_regions_from_coords, points_to_coordslogging.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' shapesnorth_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 coordinatescoords = points_to_coords(north_am_cities.geometry)# calculate the regionspoly_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 文檔。然而,當我運行它時,我收到以下錯誤:
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

您收到的錯誤源于以下事實:您正在提取的城市信息包含北美城市很少,或者它們未被正確識別為北美境內。您的問題是關于基于首都創建Voronoi圖,因此我包含了一個指向美國首都數據集的鏈接,以便您可以使用可靠數量的城市測試示例:


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

生產:

http://img1.sycdn.imooc.com//62fb76a30001f22205780467.jpg

對于北美,您可以下載城市 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()

生產:

http://img1.sycdn.imooc.com//62fb76b000019e2705590503.jpg

查看完整回答
反對 回復 2022-08-16
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號