用cartopy绘制特定国家的地图?

问题描述 投票:9回答:2

我在这里试过这个例子,效果很好。但我如何专注于另一个国家,即只展示德国?

这个例子的来源

http://scitools.org.uk/cartopy/docs/latest/examples/hurricane_katrina.html exampl of us map http://scitools.org.uk/cartopy/docs/latest/_images/hurricane_katrina_01_00.png

我在extend()方法上尝试了一些坐标,但我没有设法像美国地图一样。或者我必须修改形状文件?

python geo cartopy
2个回答
23
投票

使用http://www.gadm.org/country的全球管理区域数据集,只需下载德国数据集并使用cartopy的shapereader(与链接示例中的方式相同)。

一个简短的自包含示例:

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt

# Downloaded from http://biogeo.ucdavis.edu/data/gadm2/shp/DEU_adm.zip
fname = '/downloads/DEU/DEU_adm1.shp'

adm1_shapes = list(shpreader.Reader(fname).geometries())

ax = plt.axes(projection=ccrs.PlateCarree())

plt.title('Deutschland')
ax.coastlines(resolution='10m')

ax.add_geometries(adm1_shapes, ccrs.PlateCarree(),
                  edgecolor='black', facecolor='gray', alpha=0.5)

ax.set_extent([4, 16, 47, 56], ccrs.PlateCarree())

plt.show()

HTH


5
投票

让我举一个使用naturalearthdata数据的例子。因此可以将其扩展到任何国家。

from cartopy.io import shapereader
import numpy as np
import geopandas
import matplotlib.pyplot as plt

import cartopy.crs as ccrs

# get natural earth data (http://www.naturalearthdata.com/)

# get country borders
resolution = '10m'
category = 'cultural'
name = 'admin_0_countries'

shpfilename = shapereader.natural_earth(resolution, category, name)

# read the shapefile using geopandas
df = geopandas.read_file(shpfilename)

# read the german borders
poly = df.loc[df['ADMIN'] == 'Germany']['geometry'].values[0]

ax = plt.axes(projection=ccrs.PlateCarree())

ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor='none', 
                  edgecolor='0.5')

ax.set_extent([5, 16, 46.5, 56], crs=ccrs.PlateCarree())

这产生了下图:

Figure Germany

© www.soinside.com 2019 - 2024. All rights reserved.