performance - 使用 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()
这个例子对我来说实际上运行得更慢(~97ms)。
推荐阅读
- php - 如何在 WordPress 中启用图片库
- javascript - 无法用 v-if 比较数字
- matlab - 如何在 Matlab 中找到 X、Y 和 Z 的 RANSAC
- android - 通知托盘中的 Android Bot 而不是启动器图标 - FCM
- javascript - Angular 7:更新的 systemjs.config.js 是什么?
- javascript - 测试:62 未捕获的 ReferenceError:未在 HTMLButtonElement.onclick 中定义(测试:62)
- azerothcore - 如何禁用实例?
- objective-c - 如何让 IOS 应用程序信任本地主机以进行开发 API
- angular-material - 加载带有多个选项的材质选择并选中某些选项?
- android - 使用 Kotlin 在 Android Studio 中返回当前日期