目前,我在一个项目中使用OSMnx绘制一个区域内的道路网络。
我现在想增加水体,这样我们就可以清楚地看到一个区域的哪一部分是水和土地。
到目前为止,我已经能够使用OSMnx的图形函数的custom_filter参数来识别水体。然后,我可以使用plot_graph函数勾勒出这些水体。
理想情况下,我会想要填充水体(而不是仅仅概述它们)。我觉得这应该是可能的,因为在OpenStreetMap中,水体是充满的,但我不知道如何用OSMnx来做到这一点。有人有什么想法吗?
以下是我目前的情况:
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中使用geodataframe?还是plot_footprints是我所需要的?我还没能找到有人策划水体的例子。似乎GDF通常用于绘制一个地方的地图,而脚印则用于建筑物。虽然这些都是以多边形为导向的情节,但我觉得这可能是正确的方法。
发布于 2020-06-09 16:49:48
这并不完美,但它让你几乎达到了目的:
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)

关键的问题是,我目前还不清楚OSM是否可以被持续地询问直接的陆地和水多边形(我通常在我的研究中不使用陆地/水边界)。places可能是政治边界,这可能与现实生活中的水域重叠。你可能想尝试一下你在这里的土地和水之间的关系。
发布于 2020-11-08 19:16:33
我已经和这个斗争了一段时间了,我有一个小项目。可接受的解决方案所做的是在water_color背景上绘制获取的土地,并添加用标记{'natural':'water'}获取的封闭多边形。只有当land覆盖整个视场时,这才能很好地工作。如果您只保留了列表中的一个位置(比如'Manhattan, NY, USA'),那么您就只能在蓝色的海洋中选择这片土地。
好吧,如果你想要的话。
我想要做的,以及我怀疑OP也想要的(因为他们从一个边界框中获取信息),就是在边界框中拥有所有的水陆接口。如果需要的是等高线,也包括海岸线,但是海岸线不是封闭的多边形(它们是分开的),所以没有简单的方法可以做到这一点。
我开始研究使用osmnx.geometries.linemerge和osmnx.geo_utils.split来打破海岸线上的多边形,但我终于发现有人已经完成了所有的工作:
https://osmdata.openstreetmap.de/contact.html
这个存储库将所有海岸线连接成多边形(无论是在水边还是陆地边)。github回购是https://github.com/fossgis。
所以我要说的是,按照OP的要求,最干净的方法就是下载并使用这些shapefiles。假设水多边形文件已在工作目录中解压缩,这是一个工作示例:
import osmnx as ox
import geopandas as gpd
# Bounding box
bN, bS, bE, bW = 40.9666, 40.4362, -73.6084, -74.3254
# Fetch water
G = ox.geometries.geometries_from_bbox(bN, bS, bE, bW, tags={"natural": "water"})
# Load coastline polygons
water = gpd.read_file('water-polygons-split-4326/water_polygons.shp', bbox=(bW, bN, bE, bS))
# Plot
fig, ax = ox.plot_footprints(water, bbox=(bN, bS, bE, bW),
color=water_color, bgcolor=land_color,
show=False, close=False)
ax = G.plot(ax=ax, fc=water_color, markersize=0)

https://stackoverflow.com/questions/62285134
复制相似问题