我想加快这段代码的执行速度:
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
是否具有任何剪切功能?
答案 0 :(得分:1)
简短的答案是,不,CartoPy没有任何内置的剪切操作。但是,CartoPy使用的Shapely确实可以。
我猜想您看到的性能问题在您自己的情节中。您发布的代码在我的系统上的运行时间约为45ms。这是一个示例,您可以在其中使用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)。