【问题标题】:Draw a map of a specific country with cartopy?用cartopy画一张特定国家的地图?
【发布时间】:2014-10-15 05:10:19
【问题描述】:

我在这里试过这个例子,效果很好。但是我如何专注于另一个国家,即只显示德国?

此示例的来源

http://scitools.org.uk/cartopy/docs/latest/examples/hurricane_katrina.html

我在 extend() 方法上尝试了一些坐标,但我没有设法让它看起来像美国地图。还是我必须修改形状文件?

【问题讨论】:

    标签: python geo cartopy


    【解决方案1】:

    使用http://www.gadm.org/country 的全球行政区数据集,只需下载德国数据集并使用 cartopy 的 shapereader(与链接示例中的操作相同)。

    一个简短的自包含示例:

    import cartopy.crs as ccrs
    import cartopy.io.shapereader as shpreader
    import matplotlib.pyplot as plt
    
    # Downloaded from http://biogeo.ucdavis.edu/data/gadm2/shp/DEU_adm.zip
    fname = '/downloads/DEU/DEU_adm1.shp'
    
    adm1_shapes = list(shpreader.Reader(fname).geometries())
    
    ax = plt.axes(projection=ccrs.PlateCarree())
    
    plt.title('Deutschland')
    ax.coastlines(resolution='10m')
    
    ax.add_geometries(adm1_shapes, ccrs.PlateCarree(),
                      edgecolor='black', facecolor='gray', alpha=0.5)
    
    ax.set_extent([4, 16, 47, 56], ccrs.PlateCarree())
    
    plt.show()
    

    HTH

    【讨论】:

    • Cartopy 的创造者本人的回答——一定会喜欢 Stack Overflow。
    • 有没有办法在cartopy中标注国名?
    • 如果我想合并德国和荷兰怎么办。我可以以某种方式合并形状文件以显示两个国家并掩盖其余国家吗?
    【解决方案2】:

    让我举个例子,使用来自naturalearthdata 的数据。因此可以将其扩展到任何国家/地区。

    from cartopy.io import shapereader
    import numpy as np
    import geopandas
    import matplotlib.pyplot as plt
    
    import cartopy.crs as ccrs
    
    # get natural earth data (http://www.naturalearthdata.com/)
    
    # get country borders
    resolution = '10m'
    category = 'cultural'
    name = 'admin_0_countries'
    
    shpfilename = shapereader.natural_earth(resolution, category, name)
    
    # read the shapefile using geopandas
    df = geopandas.read_file(shpfilename)
    
    # read the german borders
    poly = df.loc[df['ADMIN'] == 'Germany']['geometry'].values[0]
    
    ax = plt.axes(projection=ccrs.PlateCarree())
    
    ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor='none', 
                      edgecolor='0.5')
    
    ax.set_extent([5, 16, 46.5, 56], crs=ccrs.PlateCarree())
    

    这会产生下图:

    【讨论】:

    • 从 cartopy 0.18 开始,我不得不将 ax.add_geometries(poly, ...) 更改为 ax.add_geometries([poly] ...)
    猜你喜欢
    • 2020-08-11
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    相关资源
    最近更新 更多