osmnx无法在组合图中找到节点之间的路径?

时间:2019-06-20 08:56:01

标签: geolocation networkx distance geo osmnx

我正在尝试使用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)

1 个答案:

答案 0 :(得分:0)

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

  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]