【问题标题】:How to add custom shapefile to map using cartopy如何使用 cartopy 将自定义 shapefile 添加到地图
【发布时间】:2014-01-26 05:49:11
【问题描述】:

使用底图,我曾经像这样添加我的自定义边界 shapefile:

map = Basemap(..)
map.readshapefile(file.shp, 'attribute', drawbounds=True)

如何使用 cartopy 做同样的事情?

我试过这个:

ax.add_feature(cfeature.shapereader.Polygon('file.shp'))

但这不起作用..

【问题讨论】:

    标签: cartopy


    【解决方案1】:

    目前没有 ShapefileFeature 类(虽然这很容易创建,并且可能很有意义)所以如果你真的想使用功能界面,那么有一个可以跳过的箍:

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    from cartopy.io.shapereader import Reader
    from cartopy.feature import ShapelyFeature
    
    fname = '50m_glaciated_areas.shp'
    
    ax = plt.axes(projection=ccrs.Robinson())
    shape_feature = ShapelyFeature(Reader(fname).geometries(),
                                    ccrs.PlateCarree(), facecolor='none')
    ax.add_feature(shape_feature)
    plt.show()
    

    或者,您可以只使用 add_geometries 方法,该方法不使用功能接口(因此,将来不会优化为仅从磁盘读取实际正在绘制的几何图形ShapefileFeature 类的案例):

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    from cartopy.io.shapereader import Reader
    
    fname = '50m_glaciated_areas.shp'
    
    ax = plt.axes(projection=ccrs.Robinson())
    ax.add_geometries(Reader(fname).geometries(),
                      ccrs.PlateCarree(),
                      facecolor='white', hatch='xxxx')
    plt.show()
    

    HTH

    【讨论】:

    • 谢谢,这感觉就像做对了,而不是用我的替换文件夹中的 shapefile:C:\Users\lenovo\.local\share\cartopy\shapefiles\natural_earth\physical;)
    • 确实如此!很高兴你有比这更好的解决方案:)
    猜你喜欢
    • 2014-10-10
    • 2015-05-27
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多