Cartopy:用于绘制特定河流的子集特征

Cartopy: subsetting features for plotting a specific river

cartopyPython3 工作,我试图从 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?,似乎没有明确的方法这样做 The cartopy Feature interface documentation

您需要使用 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\physicalm_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()