首页 > 解决方案 > 使用 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有剪辑功能吗?

标签: performanceclippingcartopy

解决方案


简短的回答是不,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()

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


推荐阅读