How to add a point-feature shapefile to map using cartopy

后端 未结 1 414
再見小時候
再見小時候 2021-02-06 12:31

I have two shapefiles. One is a point feature shapefile, named \"point.shp\", the other is a polygon shapefile named \"polygon.shp\". Both I want to add to a map using cartopy.

相关标签:
1条回答
  • 2021-02-06 12:56

    add_geometries currently turns a geometry into a polygon and then colours it appropriately, which of course means that when you pass points the add_geometries, the polygons are not visible. Potentially cartopy could do a better job of this in the future, but in the meantime, it sounds like you just want to use something like scatter to visualize your data.

    You can achieve this by getting the x and y coordinate values out of the geometry and passing these straight on to scatter with the appropriate transform:

    import cartopy.crs as ccrs
    import cartopy.io
    import matplotlib.pyplot as plt
    
    
    fname = cartopy.io.shapereader.natural_earth(resolution='10m',
                                                   category='cultural',
                                                   name='populated_places_simple')
    
    plt.figure(figsize=(12, 6))
    ax = plt.axes(projection=ccrs.Robinson())
    
    ax.set_title('Populated places of the world.')
    ax.coastlines()
    
    points = list(cartopy.io.shapereader.Reader(fname).geometries())
    
    ax.scatter([point.x for point in points],
               [point.y for point in points],
               transform=ccrs.Geodetic())
    
    plt.show()
    

    output

    HTH

    0 讨论(0)
提交回复
热议问题