使用 Cartopy 加速高分辨率海岸线绘图

Speeding up high-res coastline plotting with Cartopy

我想加快这段代码的执行速度:

import cartopy.crs as ccrs
from cartopy.feature import NaturalEarthFeature
import matplotlib.pyplot as plt

# the extent in the original code is calculated on the fly
extent = [0, 50, 20, 60]

plt.figure("Test Map")
ax = plt.subplot(111, projection=ccrs.PlateCarree())
ax.set_extent(extent, crs=ccrs.PlateCarree())

ax.add_feature(NaturalEarthFeature('physical', 'ocean', '50m'))

plt.show()

代码目前需要几秒钟时间来可视化结果图。 当我在结果图中平移时,我可以看到 cartopy 实际上已经绘制了所有多边形! cartopy 有剪辑功能吗?

简短的回答是不,CartoPy 没有任何内置的裁剪操作。但是,CartoPy 使用的 Shapely 可以。

我猜你看到的性能问题是在你自己的情节中的某个地方。您发布的代码在我的系统上运行时间约为 45 毫秒。下面是一个示例,您可以在其中使用 Shapely 计算地图要素与范围框的交集。

import cartopy.crs as ccrs
from cartopy.feature import NaturalEarthFeature
import matplotlib.pyplot as plt
import shapely.geometry as sgeom

# the extent in the original code is calculated on the fly
extent = [0, 50, 20, 60]

feat = NaturalEarthFeature('physical', 'ocean', '50m')
box = sgeom.box(extent[0], extent[2], extent[1], extent[3])
geoms = [geom.intersection(box) for geom in feat.geometries()]

plt.figure("Test Map")
ax = plt.subplot(111, projection=ccrs.PlateCarree())
ax.set_extent(extent, crs=ccrs.PlateCarree())

ax.add_geometries(geoms, crs=ccrs.PlateCarree())

plt.show()

虽然这个例子对我来说实际上运行得更慢(~97 毫秒)。