我正在尝试绘制特定区域的河流,但我不明白为什么
osmnx
在某些地方找不到它。
我有这个代码
import osmnx as ox
Sriver = ox.graph_from_place('Providencia, Chile',
# retain_all=False,
# truncate_by_edge=False,
# simplify=True,
custom_filter='["waterway"~"river"]',
# custom_filter='way["waterway"]'
)
但是失败了:
Found no graph nodes within the requested polygon
如果我从 OSM 地图查询特征,河流显然就在那里 https://www.openstreetmap.org/query?lat=-33.41919&lon=-70.61062
我应该使用其他过滤器吗?我在文档中找不到更多信息。或者,有没有办法从一个区域中提取我可以使用的所有可用过滤器?
如 OSMnx 文档的入门指南中所述:
在幕后,OSMnx 做了几件事来生成最好的模型。它首先创建一个 500m 缓冲的图表,然后将其截断到所需的查询区域,以确保准确的每个节点街道统计数据并减弱图表周界影响。它还简化了图拓扑,如下所述。
你说:
我正在尝试绘制特定区域的河流,但我不明白为什么 osmnx 在某些地方找不到它。
...但要更仔细地查看您得到的完整错误回溯:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[2], line 3
1 import osmnx as ox
----> 3 Sriver = ox.graph_from_place('Providencia, Chile',
4 # retain_all=False,
5 # truncate_by_edge=False,
6 # simplify=True,
7 custom_filter='["waterway"~"river"]',
8 # custom_filter='way["waterway"]'
9 )
File ~/Dropbox/Documents/School/Projects/Code/osmnx-repos/osmnx/osmnx/graph.py:375, in graph_from_place(query, network_type, simplify, retain_all, truncate_by_edge, which_result, buffer_dist, clean_periphery, custom_filter)
372 utils.log("Constructed place geometry polygon(s) to query API")
374 # create graph using this polygon(s) geometry
--> 375 G = graph_from_polygon(
376 polygon,
377 network_type=network_type,
378 simplify=simplify,
379 retain_all=retain_all,
380 truncate_by_edge=truncate_by_edge,
381 clean_periphery=clean_periphery,
382 custom_filter=custom_filter,
383 )
385 utils.log(f"graph_from_place returned graph with {len(G):,} nodes and {len(G.edges):,} edges")
386 return G
File ~/Dropbox/Documents/School/Projects/Code/osmnx-repos/osmnx/osmnx/graph.py:492, in graph_from_polygon(polygon, network_type, simplify, retain_all, truncate_by_edge, clean_periphery, custom_filter)
485 G_buff = simplification.simplify_graph(G_buff)
487 # truncate graph by original polygon to return graph within polygon
488 # caller wants. don't simplify again: this allows us to retain
489 # intersections along the street that may now only connect 2 street
490 # segments in the network, but in reality also connect to an
491 # intersection just outside the polygon
--> 492 G = truncate.truncate_graph_polygon(G_buff, polygon, retain_all, truncate_by_edge)
494 # count how many physical streets in buffered graph connect to each
495 # intersection in un-buffered graph, to retain true counts for each
496 # intersection, even if some of its neighbors are outside the polygon
497 spn = stats.count_streets_per_node(G_buff, nodes=G.nodes)
File ~/Dropbox/Documents/School/Projects/Code/osmnx-repos/osmnx/osmnx/truncate.py:162, in truncate_graph_polygon(G, polygon, retain_all, truncate_by_edge, quadrat_width, min_num)
159 if not to_keep:
160 # no graph nodes within the polygon: can't create a graph from that
161 msg = "Found no graph nodes within the requested polygon"
--> 162 raise ValueError(msg)
164 # now identify all nodes whose point geometries lie outside the polygon
165 gs_nodes_outside_poly = gs_nodes[~gs_nodes.index.isin(to_keep)]
ValueError: Found no graph nodes within the requested polygon
OSMnx is 正在寻找您正在寻找的河流。它以 500m 缓冲图的形式执行此操作,然后将其截断回您所需的研究区域。但是因为您要求 OSMnx 自动简化图拓扑,并且因为您有一个非常琐碎的空间几何作为唯一结果,所以简化步骤 删除了研究区域内的所有图节点,仅留下研究之外的几个端点但在 500m 缓冲区内。因此,在截断到您的研究区域后,它发现您的研究边界内没有剩余的图形节点。
您可以通过 1)根本不简化图,或 2)在构建图后简化图来解决此问题。两个选项的简单示例,以及正在发生的情况的说明:
import osmnx as ox
# get boundary polygon and unsimplified graph
gdf = ox.geocode_to_gdf("Providencia, Chile")
geom = gdf.iloc[0]["geometry"]
G = ox.graph_from_polygon(geom, retain_all=True, truncate_by_edge=True, simplify=False, custom_filter='["waterway"~"river"]')
# plot unsimplified graph over boundary polygon
ax = gdf.plot()
fig, ax = ox.plot_graph(G, ax=ax, node_color="r", edge_color="k")
# plot simplified graph over boundary polygon
Gs = ox.simplify_graph(G)
ax = gdf.plot()
fig, ax = ox.plot_graph(Gs, ax=ax, node_color="r", edge_color="k")