【问题标题】:Speeding up high-res coastline plotting with Cartopy使用 Cartopy 加速高分辨率海岸线绘图
【发布时间】:2020-07-24 04:56:42
【问题描述】:

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

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 有剪裁功能吗?

【问题讨论】:

    标签: performance clipping cartopy


    【解决方案1】:

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

    【讨论】:

    • 实际上,我在 Python 3.6 上使用原始脚本获得了与您相同的性能,但在 Python 3.8 上大约 3 秒。 Cartopy 在两个环境(0.17.0)上具有相同的版本,它是从 conda-forge 安装的。这有点令人困惑!有什么想法吗?
    • 为了尝试调试它,我会比较 3.6 和 3.8 环境之间的更多包的版本,并确保它们都安装了相同的总集。我还会尝试缩小范围,看看是否存在导致性能下降的特定行。将“50m”更改为“110m”是否会使事情运行得更快?
    猜你喜欢
    • 2022-11-29
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2017-12-19
    相关资源
    最近更新 更多