我有一个这样的代码:
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()
country = list(countries)[25]
central_lon, central_lat = 20, 0
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1,projection=ccrs.PlateCarree())
ax2 = fig.add_subplot(1,2,2, projection=ccrs.Orthographic(central_lon, central_lat))
fig.subplots_adjust(bottom=0.05, top=0.95,
left=0.04, right=0.95, wspace=0.02)
s = cfeature.AdaptiveScaler('coarse',(('intermediate', 30), ('fine', 10)))
ax1.set_extent((2, 52, -40, 10))
ax1.coastlines(resolution='50m')
ax1.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
ax1.add_feature(cartopy.feature.LAND,facecolor=np.array((240, 240, 220)) / 256.)
ax1.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 0, 1))
ax2.gridlines()
ax2.add_feature(cartopy.feature.LAND,facecolor=np.array((240, 240, 220)) / 256.)
ax2.add_feature(cartopy.feature.OCEAN) ax2.add_geometries(country.geometry,ccrs.PlateCarree(),facecolor=(0, 0, 1))
ax2.coastlines(resolution='50m')
plt.savefig("cartopy/{}.png".format("img1"))
plt.show()这将生成以下图像:

。
我有问题的线路是ax1.set_extent((2, 52, -40, 10))。有没有办法,我可以得到一个国家的范围,而不是手动输入它?在所有的文档中,我总是看到人们硬编码元组,但当循环遍历整个国家数据集时,这是不可行的。
发布于 2020-06-29 01:20:28
从一个国家的几何形状,你可以得到它的边界。从界限中,您可以像这样获取限制:
lon_min, lat_min, lon_max, lat_max = acountry.geometry.bounds可以使用获得的值来设置范围:
ax1.set_extent((lon_min, lon_max, lat_min, lat_max))https://stackoverflow.com/questions/62623797
复制相似问题