How to fill water bodies with OSMnx in Python

♀尐吖头ヾ 提交于 2020-06-29 04:08:11

问题


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 land.

So far, I've been able to identify waterbodies using the custom_filter argument to OSMnx's graph functions. I can then outline the waterbodies using the plot_graph function.

Ideally, I'd want to fill the waterbodies in (rather than only outline them). I feel like this should be possible, as in OpenStreetMap water bodies are filled, but I can't figure out how to do this with OSMnx. Does anybody have any ideas?

Here's what I have currently:

import osmnx as ox

# Get water bodies map of the New York City area
G = ox.graph_from_bbox(40.9666,40.4362,-73.6084,-74.3254, custom_filter='["natural"~"water|coastline"]', retain_all = True)

# Plot the graph in blue on a white background
ox.plot_graph(G, bgcolor='white', node_size=0, equal_aspect=True, edge_color='blue')

Which produces this image:

Do I need to somehow use a geodataframe with PlotShape? Or is plot_footprints what I need? I haven't been able to find examples of people plotting waterbodies. It seems the GDF is generally used for plotting the map of a place, and footprints is used for buildings. Though as those are both polygon-oriented plots, I feel that might be the right way.


回答1:


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.gdf_from_places(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.pois_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.



来源:https://stackoverflow.com/questions/62285134/how-to-fill-water-bodies-with-osmnx-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!