【问题标题】:Plotting Multiple Routes with OSMNx使用 OSMNx 绘制多条路线
【发布时间】:2018-12-17 20:59:16
【问题描述】:

我正在使用 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中没有内置函数,有没有办法做到这一点?

我发现解决方案是“绘制图表,然后使用 matplotlib 在顶部手动添加路线”。但无法实现。

【问题讨论】:

    标签: python python-3.x matplotlib osrm osmnx


    【解决方案1】:

    编辑:请注意,从 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 或此 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)
    

    【讨论】:

      【解决方案2】:

      可以这样做, 无花果,ax = 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)
      

      `

      My example

      【讨论】:

        【解决方案3】:

        看看 OSMNX pull request #152。它尚未合并到 OSMNX 主控器中,但我自己使用了代码并且效果很好 - 您可以简单地将其复制到您自己的脚本(或实用程序文件)中,修复任何预期的导入并非常轻松地使用它.

        对于多种颜色,修改代码以接受 route_color 数组而不是单个 route_color 字符串似乎很容易。

        【讨论】:

          猜你喜欢
          • 2021-04-17
          • 1970-01-01
          • 1970-01-01
          • 2021-11-05
          • 1970-01-01
          • 2020-02-06
          • 1970-01-01
          • 2020-03-05
          • 2017-06-13
          相关资源
          最近更新 更多