【问题标题】:Osmnx cannot find path between nodes in composed graph?Osmnx 在组合图中找不到节点之间的路径?
【发布时间】:2019-11-03 01:31:49
【问题描述】:

我正在尝试使用 osmnx 查找起点(纬度/经度)与最近的基础设施(如铁路、水或公园)之间的距离。

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)

【问题讨论】:

    标签: geolocation networkx distance geo osmnx


    【解决方案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]
    

    【讨论】:

    • 请注意,infrastructure 参数在最近的版本中已被弃用并被 custom_filter 取代。
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2020-09-04
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多