2

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

4

1 回答 1

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]
于 2019-07-02T06:45:29.107 回答