使用 osmnx 绘制多条路线

Plot multiple routes using osmnx

我正在尝试绘制多条路线,但出现错误 - networkx.exception.NetworkXError: nbunch 序列中的节点 [20461931, 75901933] 不是有效节点。

绘制单个路线不再是问题,但一起绘制时会出现错误。


import networkx as nx
import osmnx as ox


# Create Graph 
G = ox.graph_from_place('Munich, Germany', network_type='drive')


# route1 calc
origin_node = 20461931
destination_node =   75901933
route1 = nx.shortest_path(G, origin_node, destination_node)

# route2 calc
start =   (48.1336081,  11.58172095)
end = (48.17822992, 11.53754219)
start_node = ox.get_nearest_node(G, start)
end_node = ox.get_nearest_node(G, end)
route2 = nx.shortest_path(G, start_node, end_node, weight='travel_time')

#plot the route with folium
route_list = [route1,route2]
route_map = ox.plot_route_folium(G,route_list)

# save as html file then display map as an iframe
filepath = 'route.html'
route_map.save(filepath)
 

您向其传递的是 节点列表 的列表,而不是节点列表。有关用法详细信息,请参阅 docs

import osmnx as ox
ox.config(use_cache=True, log_console=True)

G = ox.graph_from_place('Munich, Germany', network_type='drive')
route1 = ox.shortest_path(G, 20461931, 75901933, weight=None)

orig = ox.get_nearest_node(G, (48.1336081,  11.58172095))
dest = ox.get_nearest_node(G, (48.17822992, 11.53754219))
route2 = ox.shortest_path(G, orig, dest, weight='travel_time')

route_map = ox.plot_route_folium(G, route1, route_color='#ff0000', opacity=0.5)
route_map = ox.plot_route_folium(G, route2, route_map=route_map, route_color='#0000ff', opacity=0.5)
route_map.save('route.html')