OSMNX最短路径节点-获取节点旅行时间

时间:2020-06-02 16:41:17

标签: python networkx osmnx

我想使用Osmnx获取最短路径路由中的节点之间的旅行时间。有没有一种方法可以获取节点之间的旅行时间。

import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)
import pandas as pd

pla__version__Piedmont, CA, USA
G = ox.graph_from_place(place, network_type='drive')

orig = list(G)[0]
dest = list(G)[-1]
route = nx.shortest_path(G, orig, dest)
#fig, ax = ox.plot_graph_route(G, route, route_linewidth=6, node_size=0, bgcolor='k')


for i, val in enumerate(route):
    print(i, val, G.nodes[val]['x'], G.nodes[val]['y'])

我想存储节点,即上述循环中实现的纬度和经度,但是有一种方法也可以存储两个节点之间的旅行时间和/或两个节点之间的距离。

1 个答案:

答案 0 :(得分:2)

OSM的速度和行驶时间数据往往参差不齐。使用OSMnx的speed module来估算缺失的边沿速度并计算自由流动的时间。

import networkx as nx
import osmnx as ox
import pandas as pd
ox.config(use_cache=True, log_console=True)

place = 'Piedmont, CA, USA'
G = ox.graph_from_place(place, network_type='drive')

# impute missing edge speeds and add travel times
G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

# calculate route minimizing some weight
orig, dest = list(G)[0], list(G)[-1]
route = nx.shortest_path(G, orig, dest, weight='travel_time')

# OPTION 1: see the travel time for the whole route
travel_time = nx.shortest_path_length(G, orig, dest, weight='travel_time')
print(round(travel_time))

# OPTION 2: loop through the edges in your route
# and print the length and travel time of each edge
for u, v in zip(route[:-1], route[1:]):
    length = round(G.edges[(u, v, 0)]['length'])
    travel_time = round(G.edges[(u, v, 0)]['travel_time'])
    print(u, v, length, travel_time, sep='\t')

# OPTION 3: use get_route_edge_attributes
cols = ['osmid', 'length', 'travel_time']
attrs = ox.utils_graph.get_route_edge_attributes(G, route)
print(pd.DataFrame(attrs)[cols])