过滤关系

Filtering on relation

我正在尝试检索挪威主要道路的网络。但是,“int_ref”和“ref”标签不一致,导致道路出现缝隙。在 OpenStreetMap 中查看道路时,我可以看到 'Part of' 下的 'relation' 标签正是我所需要的。有什么方法可以使用 OSMnx 检索它吗?还有其他方法可以检索完整的道路吗?在根据 int_ref:

过滤一条特定道路时,我使用了以下代码行
G1 = ox.graph_from_place(query = "Norway", retain_all = True, custom_filter = '["int_ref"~"E 39"]')

不,OSMnx 过滤 way 标签,而不是关系。如果只想获取一个国家/地区的主要道路,请参阅此答案:

类似这样的东西可能会满足您的需求:

import osmnx as ox
ox.config(use_cache=True, log_console=True)

# get the geometry of the norwegian mainland
gdf = ox.geocode_to_gdf('Norway')
geom = max(gdf['geometry'].iloc[0], key=lambda x: x.area)

# get all motorway/trunk roads
cf = '["highway"~"motorway|motorway_link|trunk|trunk_link"]'
G = ox.graph_from_polygon(geom, network_type='drive', custom_filter=cf)

# plot it
fig, ax = ox.plot_graph(G)

需要约 370 个 Overpass API 请求才能下载挪威大陆的所有区域,因此发出所有这些请求需要一些时间。您可以在控制台的日志中查看其进度。