本文介绍了如何在Python中使用OSMnx填充水体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在项目中使用OSMnx绘制区域内的道路网络.

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.

到目前为止,我已经能够使用OSMnx的图形函数的custom_filter参数来识别水体.然后,我可以使用plot_graph函数概述水体.

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.

理想情况下,我想在其中填充水体(而不是仅概述它们).我觉得这应该可行,因为在OpenStreetMap中填充了水体,但是我不知道如何使用OSMnx做到这一点.有人有什么主意吗?

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?

这是我目前拥有的:

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')

哪个生成此图像:

我是否需要通过PlotShape使用地理数据框?还是我需要plot_footprints?我还没有找到绘制水体图的例子.似乎GDF通常用于绘制位置地图,而脚印用于建筑物.尽管这些都是面向多边形的图,但我认为这可能是正确的方法.

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.

推荐答案

这不是十全十美,但可以带您到那里:

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)

关键问题是我目前尚不清楚是否可以始终如一地查询OSM是否包含简单的水陆多边形(我在研究中通常不使用水陆边界). places可能是政治边界,可能与现实生活中的水域重叠.您可能想在此处尝试以土地还是水来查询/绘图.

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.

这篇关于如何在Python中使用OSMnx填充水体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 00:02