Osmnx 找不到组合图中节点之间的路径?

Osmnx cannot find path between nodes in composed graph?

我正在尝试使用 osmnx 查找原点 (lat/lon) 和最近的基础设施(例如铁路、水域或公园)之间的距离。

1) 我从 network_type='walk' 的区域得到了整个图表。

2) 获取所需的基础设施,例如同一地区的铁路。

3) 将两个图合二为一

4) 在原始图中找到离原点最近的节点。

5) 在基础设施图中找到离原点最近的节点

6)求两个节点之间的最短路径长度

如果您 运行 下面的例子,您会发现它丢失了 20% 的数据,因为它找不到节点之间的路径。对于 infrastructure='way["leisure"~"park"]'infrastructure='way["natural"~"wood"]',情况更糟,80-90% 的节点未连接。


最小可重现示例:

import osmnx as ox
import networkx as nx

bbox = [55.5267243, 55.8467243, 12.4100724, 12.7300724]

g = ox.graph_from_bbox(bbox[0], bbox[1], bbox[2], bbox[3], 
                       retain_all=True, 
                       truncate_by_edge=True, 
                       simplify=False,
                       network_type='walk')


points = [(55.6790884456018, 12.568493971506154),
 (55.6790884456018, 12.568493971506154),
 (55.6867418740291, 12.58232314016353),
 (55.6867418740291, 12.58232314016353),
 (55.6867418740291, 12.58232314016353),
 (55.67119624894504, 12.587201455313153),
 (55.677406927839506, 12.57651997656002),
 (55.6856574907879, 12.590500429002823),
 (55.6856574907879, 12.590500429002823),
 (55.68465359365924, 12.585474365063224),
 (55.68153666806675, 12.582594757267945),
 (55.67796979175, 12.583111746311117),
 (55.68767346629932, 12.610040871066179),
 (55.6830855237578, 12.575431380892427),
 (55.68746749645466, 12.589488615911913),
 (55.67514254640597, 12.574308210656602),
 (55.67812748568291, 12.568454119053886),
 (55.67812748568291, 12.568454119053886),
 (55.6701733527419, 12.58989203029166),
 (55.677700136266616, 12.582800629527789)]

railway = ox.graph_from_bbox(bbox[0], bbox[1], bbox[2], bbox[3], 
                               retain_all=True, 
                               truncate_by_edge=True, 
                               simplify=False,
                               network_type='walk', 
                               infrastructure='way["railway"]')

g_rail = nx.compose(g, railway)

l_rail = []

for point in points:

    nearest_node = ox.get_nearest_node(g, point)

    rail_nn = ox.get_nearest_node(railway, point)

    if nx.has_path(g_rail, nearest_node, rail_nn):
        l_rail.append(nx.shortest_path_length(g_rail, nearest_node, rail_nn, weight='length'))
    else:
        l_rail.append(-1)

有两件事引起了我的注意。

  1. OSMNX 文档指定 ox.graph_from_bbox 参数按北、南、东、西 (https://osmnx.readthedocs.io/en/stable/osmnx.html) 的顺序给出。我提到这一点是因为当我尝试 运行 你的代码时,我得到的是空图。

  2. 参数 'retain_all = True' 是您可能已经知道的关键。当设置为 true 时,它​​会保留图中的所有节点,即使它们没有连接到图中的任何其他节点。发生这种情况的主要原因是 OpenStreetMap 的不完整性,其中包含自愿提供的地理信息。我建议你设置 'retain_all = False' 意味着你的图表现在只包含连接的节点。这样就得到一个没有任何-1的完整列表。

希望对您有所帮助。

g = ox.graph_from_bbox(bbox[1], bbox[0], bbox[3], bbox[2], 
                       retain_all=False, 
                       truncate_by_edge=True, 
                       simplify=False,
                       network_type='walk')

railway = ox.graph_from_bbox(bbox[1], bbox[0], bbox[3], bbox[2], 
                               retain_all=False, 
                               truncate_by_edge=True, 
                               simplify=False,
                               network_type='walk', 
                               infrastructure='way["railway"]')

g_rail = nx.compose(g, railway)

l_rail = []

for point in points:

    nearest_node = ox.get_nearest_node(g, point)

    rail_nn = ox.get_nearest_node(railway, point)

    if nx.has_path(g_rail, nearest_node, rail_nn):
        l_rail.append(nx.shortest_path_length(g_rail, nearest_node, rail_nn, weight='length'))
    else:
        l_rail.append(-1)

print(l_rail)
Out[60]: 
[7182.002999999995,
 7182.002999999995,
 5060.562000000002,
 5060.562000000002,
 5060.562000000002,
 6380.099999999999,
 7127.429999999996,
 4707.014000000001,
 4707.014000000001,
 5324.400000000003,
 6153.250000000002,
 6821.213000000002,
 8336.863999999998,
 6471.305,
 4509.258000000001,
 5673.294999999996,
 6964.213999999994,
 6964.213999999994,
 6213.673,
 6860.350000000001]