【问题标题】:How to plot a path based on some "manually chosen" nodes on osmnx?如何根据osmnx上的一些“手动选择”节点绘制路径?
【发布时间】:2019-06-18 07:11:41
【问题描述】:

我想在 osmnx 地图上绘制一条路线。当我尝试手动创建路线时,它不起作用。

import networkx as nx
import osmnx as ox
import requests 
import matplotlib.cm as cm
import matplotlib.colors as colors
location_point = (50.345559621674596, 7.576892382877531)
G = ox.graph_from_point(location_point, distance=500, 
distance_type='network', network_type='walk')
origin_node = ox.get_nearest_node(G, location_point)
destination_node = list(G.nodes())[-1]
fig, ax = ox.plot_graph(G)
G=nx.convert_node_labels_to_integers(G)
route = [3 ,1 ,41 ,40 ,98 ,62 ,67 ,107 ,94 ,83 ,39 ]
ox.plot_route_folium(G,route)

我希望有一张带有彩色路线的地图,但我得到了这个错误。

    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-9-11235dc07ef4> in <module>()
    ----> 1 ox.plot_route_folium(G,route)

    ~\Anaconda3\lib\site-packages\osmnx\plot.py in plot_route_folium(G, route, route_map, popup_attribute, tiles, zoom, fit_bounds, route_color, route_width, route_opacity)
        907     gdf_edges = graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
        908     route_nodes = list(zip(route[:-1], route[1:]))
    --> 909     index = [gdf_edges[(gdf_edges['u']==u) & (gdf_edges['v']==v)].index[0] for u, v in route_nodes]
        910     gdf_route_edges = gdf_edges.loc[index]
        911 

    ~\Anaconda3\lib\site-packages\osmnx\plot.py in <listcomp>(.0)
        907     gdf_edges = graph_to_gdfs(G, nodes=False, fill_edge_geometry=True)
        908     route_nodes = list(zip(route[:-1], route[1:]))
    --> 909     index = [gdf_edges[(gdf_edges['u']==u) & (gdf_edges['v']==v)].index[0] for u, v in route_nodes]
        910     gdf_route_edges = gdf_edges.loc[index]
        911 

    ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __getitem__(self, key)
       2082 
       2083         if is_scalar(key):
    -> 2084             return getitem(key)
       2085 
       2086         if isinstance(key, slice):

    IndexError: index 0 is out of bounds for axis 0 with size 0

如果我尝试通过最短路径算法找到一条路线并将其可视化在地图上,它可以正常工作。但我想绘制这条特定的路线

【问题讨论】:

  • “它不起作用”不是很精确。您能否向我们显示错误消息或解释预期结果和您当前的结果?

标签: python routes osmnx


【解决方案1】:

OSMnx 使用节点的 OSMID。当您运行 G=nx.convert_node_labels_to_integers(G) 时,您会覆盖这些 OSMID。但是,您并没有在图形边的 uv 属性中覆盖它们,这些属性标识边的端点(使用 OSMID)。

【讨论】:

    【解决方案2】:

    我已经完成了最短路径,对于任何路径,请确保您的路径是根据 osmid 连接的,您只需从 nx.simple_paths(G,source,destination) 查询路径即可找到。如果 osmid 没有连接,你会得到这个错误。希望这可以帮助。 :)

    import networkx as nx 
    import osmnx as ox
    import requests 
    import matplotlib.cm as cm
    import matplotlib.colors as colors
    location_point = (50.345559621674596, 7.576892382877531)
    G = ox.graph_from_point(location_point, distance=500,distance_type='network', network_type='walk')
    origin_node = ox.get_nearest_node(G, location_point)
    destination_node = list(G.nodes())[-1]
    shortest_path = nx.shortest_path(G,origin_node,destination_node) 
    

    ox.plot.plot_graph_route(G,shortest_path)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多