使用Python3
在cartopy
工作,我正在尝试从Natureal Earth数据库中划分并绘制特定的河流。
绘制所有河流然后在特定区域设置范围非常简单:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature
rivers = cartopy.feature.NaturalEarthFeature(
category='physical', name='rivers_lake_centerlines',
scale='10m', facecolor='none', edgecolor='blue')
fig, ax = plt.subplots(
nrows=1, ncols=1, subplot_kw={'projection': ccrs.PlateCarree()},
figsize=(10,6))
ax.add_feature(rivers, linewidth=1)
ax.set_extent([-65, -45, -40, -17.5])
plt.show()
(结果如下所示)
但是,如果我只想绘制一条特定的河流(为了说明目的,由于编码问题而在数据中命名为Paran?
的Paraná,似乎没有明确的方法可做这The cartopy Feature interface documentation
答案 0 :(得分:3)
您需要使用cartopy.io.shapereader
,以下是适用于我的计算机的代码:
from cartopy import config
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import shapereader
#config # format-dict
# assuming you have downloaded that file already using your original code
# its full path name should be (Windows)
fpath = config['data_dir'] + r'\shapefiles\natural_earth\physical\10m_rivers_lake_centerlines.shp'
as_shp = shapereader.Reader( fpath )
fig, ax = plt.subplots( nrows=1, ncols=1, \
subplot_kw={'projection': ccrs.PlateCarree()}, \
figsize=(10,6) )
# plot some geometries, based on their attribs
for rec in as_shp.records():
if rec.attributes['name'] == 'Parana?ba':
ax.add_geometries( [rec.geometry], ccrs.PlateCarree(), edgecolor='none', facecolor='blue' )
pass
ax.coastlines( resolution='110m' )
ax.set_extent([-65, -45, -40, -17.5])
plt.show()