使用 OSMNx 绘制多条路线
Plotting Multiple Routes with OSMNx
我正在使用 OSMNx 绘制最短路径路线,但我想不出在同一张地图上绘制多条路线的方法。
到目前为止,这是我的代码,
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_route(G, route_list, node_size=0)
我希望在同一张地图上以不同颜色为每条路线绘制 route_list 中的每条路线。由于OSMNx中没有内置函数,有没有办法做到这一点?
我找到了解决方案 "plotting the graph then adding routes manually on top with matplotlib." 但无法实施。
看看 OSMNX pull request #152。它还没有被合并到 OSMNX master 中,但我自己使用了代码并且它运行良好 - 你可以简单地将它复制到你自己的脚本(或实用程序文件)中修复任何预期的导入并非常容易地使用它.
对于多种颜色,修改代码以在数组中接受 route_color 而不是单个 route_color 字符串似乎很容易。
可以这样做,
图,斧头 = ox.plot_graph_routes(G, route_list, route_color = color_list)
但实际输出并不像希望的那样准确。为了获得完美的输出,对一个边缘使用一种颜色。但是对于您的情况,这将非常有效...
`
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_routes(G, route_list, node_size=0)
`
编辑:请注意,从 OSMnx 0.15.0 开始,您现在可以轻松地以自己的颜色轻松绘制多条路线:
routes = [route1, route2, route3]
rc = ['r', 'y', 'c']
fig, ax = ox.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)
有关详细信息,请参阅 the docs or this usage example 笔记本。
=================================
2018 年的原始答案:
值得注意的是,从 v0.8.2 开始,此功能现在存在于 OSMnx 中。示例 here 绘制多条路线,每条路线都有自己的颜色。
import networkx as nx
import osmnx as ox
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
# pick 4 random nodes as origins/destinations for the 2 routes
orig1 = list(G.nodes())[0]
dest1 = list(G.nodes())[-1]
orig2 = list(G.nodes())[50]
dest2 = list(G.nodes())[-50]
# calculate shortest paths for the 2 routes
route1 = nx.shortest_path(G, orig1, dest1, weight='length')
route2 = nx.shortest_path(G, orig2, dest2, weight='length')
# create route colors
rc1 = ['r'] * (len(route1) - 1)
rc2 = ['b'] * len(route2)
rc = rc1 + rc2
nc = ['r', 'r', 'b', 'b']
# plot the routes
fig, ax = ox.plot_graph_routes(G, [route1, route2], route_color=rc, orig_dest_node_color=nc, node_size=0)
我正在使用 OSMNx 绘制最短路径路线,但我想不出在同一张地图上绘制多条路线的方法。 到目前为止,这是我的代码,
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_route(G, route_list, node_size=0)
我希望在同一张地图上以不同颜色为每条路线绘制 route_list 中的每条路线。由于OSMNx中没有内置函数,有没有办法做到这一点?
我找到了解决方案 "plotting the graph then adding routes manually on top with matplotlib." 但无法实施。
看看 OSMNX pull request #152。它还没有被合并到 OSMNX master 中,但我自己使用了代码并且它运行良好 - 你可以简单地将它复制到你自己的脚本(或实用程序文件)中修复任何预期的导入并非常容易地使用它.
对于多种颜色,修改代码以在数组中接受 route_color 而不是单个 route_color 字符串似乎很容易。
可以这样做, 图,斧头 = ox.plot_graph_routes(G, route_list, route_color = color_list) 但实际输出并不像希望的那样准确。为了获得完美的输出,对一个边缘使用一种颜色。但是对于您的情况,这将非常有效... `
route_list = []
for i in range(len(pick_drop_outliers_ratio)):
coords = pick_drop_outliers_ratio["Pickup_Points"][i]
count = pick_drop_outliers_ratio["Count"][i]
print("i: ", i, " count: ", count)
if(count>9):
coords = literal_eval(coords)
pickup_lat = (coords[0][0])
pickup_lon = (coords[0][1])
dropoff_lat = (coords[1][0])
dropoff_lon = (coords[1][1])
orig_node = ox.get_nearest_node(G, (pickup_lat, pickup_lon))
dest_node = ox.get_nearest_node(G, (dropoff_lat, dropoff_lon))
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
route_list.append(route)
fig, ax = ox.plot_graph_routes(G, route_list, node_size=0)
`
编辑:请注意,从 OSMnx 0.15.0 开始,您现在可以轻松地以自己的颜色轻松绘制多条路线:
routes = [route1, route2, route3]
rc = ['r', 'y', 'c']
fig, ax = ox.plot_graph_routes(G, routes, route_colors=rc, route_linewidth=6, node_size=0)
有关详细信息,请参阅 the docs or this usage example 笔记本。
=================================
2018 年的原始答案:
值得注意的是,从 v0.8.2 开始,此功能现在存在于 OSMnx 中。示例 here 绘制多条路线,每条路线都有自己的颜色。
import networkx as nx
import osmnx as ox
ox.config(log_console=True, use_cache=True)
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
# pick 4 random nodes as origins/destinations for the 2 routes
orig1 = list(G.nodes())[0]
dest1 = list(G.nodes())[-1]
orig2 = list(G.nodes())[50]
dest2 = list(G.nodes())[-50]
# calculate shortest paths for the 2 routes
route1 = nx.shortest_path(G, orig1, dest1, weight='length')
route2 = nx.shortest_path(G, orig2, dest2, weight='length')
# create route colors
rc1 = ['r'] * (len(route1) - 1)
rc2 = ['b'] * len(route2)
rc = rc1 + rc2
nc = ['r', 'r', 'b', 'b']
# plot the routes
fig, ax = ox.plot_graph_routes(G, [route1, route2], route_color=rc, orig_dest_node_color=nc, node_size=0)