Performance 用Cartopy加速高分辨率海岸线绘制
我想加速执行此代码:Performance 用Cartopy加速高分辨率海岸线绘制,performance,clipping,cartopy,Performance,Clipping,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, p
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毫秒)。事实上,我在Python 3.6上使用的原始脚本的性能与您相同,但在Python 3.8上使用的是~3秒。Cartopy在两个环境(0.17.0)上的版本相同,它是从conda forge安装的。这有点令人困惑!有什么想法吗?为了试着调试这个,我会比较3.6和3.8环境中更多软件包的版本,并确保它们都有相同的安装内容。我还尝试缩小范围,看看是否有特定的线路导致性能下降。将“50米”改为“110米”是否会加快速度?