How to fill water bodies with OSMnx in Python

前端 未结 2 484
鱼传尺愫
鱼传尺愫 2021-01-25 09:43

I currently use OSMnx for a project to draw road networks in an area.

I\'d now like to add water bodies so that we can clearly see which parts of an area are water and

2条回答
  •  时光说笑
    2021-01-25 10:29

    This isn't perfect but it gets you nearly there:

    import osmnx as ox
    ox.config(log_console=True, use_cache=True)
    
    # add more items here to get all the landforms you want
    places = ['Manhattan, NY, USA', 'Brooklyn, NY, USA', 'Queens, NY, USA', 'Bronx, NY, USA']
    land = ox.geocode_to_gdf(places)
    
    # get the water bodies
    left, bottom, right, top = land.total_bounds
    bbox = top, bottom, right, left
    poly = ox.utils_geo.bbox_to_poly(*bbox)
    water = ox.geometries_from_polygon(poly, tags={'natural': 'water'})
    
    # constrain the plotting window as desired
    c = land.unary_union.centroid
    bbox = ox.utils_geo.bbox_from_point((c.y, c.x), dist=12000)
    
    water_color = 'blue'
    land_color = '#aaaaaa'
    fig, ax = ox.plot_footprints(water, bbox=bbox,
                                 color=water_color, bgcolor=water_color,
                                 show=False, close=False)
    ax = land.plot(ax=ax, zorder=0, fc=land_color)
    

    The key issue is that I'm currently unclear if OSM can be consistently queried for straightforward land vs water polygons (I don't normally work with land/water boundaries in my research). The places may be political boundaries, which could overlap with the water area in real life. You may want to experiment with what you query/plot as land vs water here.

提交回复
热议问题