首页 > 解决方案 > plot_route_folium 无向图导致 IndexError: index 0 is out of bounds for axis 0 with size 0 when

问题描述

plot_route_folium 不适用于无向图,我该怎么办?

它以这种方式适用于 Graph

G = ox.graph_from_place(place, simplify=False, custom_filter=custom_filter_ways)
orig = 25635046.0
dest = 3603422387.0
route = nx.shortest_path(G, orig, dest, weight='length')
route_map = ox.plot_route_folium(G, route)

但如果我想做

Gu = ox.get_undirected(G)
route_undirected = nx.shortest_path(Gu, orig, dest, weight='length')
route_map_u = ox.plot_route_folium(Gu, route_undirected)

我收到以下错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-50-8b91fb9c57b4> in <module>
----> 1 route_map_u = ox.plot_route_folium(Gu, route_undirected)

C:\ProgramData\Anaconda3\envs\ox\lib\site-packages\osmnx\folium.py in plot_route_folium(G, route, route_map, popup_attribute, tiles, zoom, fit_bounds, route_color, route_width, route_opacity, **kwargs)
    202     gdf_edges = utils_graph.graph_to_gdfs(G.subgraph(route), nodes=False, fill_edge_geometry=True)
    203     route_nodes = list(zip(route[:-1], route[1:]))
--> 204     index = [
    205         gdf_edges[(gdf_edges["u"] == u) & (gdf_edges["v"] == v)].index[0] for u, v in route_nodes
    206     ]

C:\ProgramData\Anaconda3\envs\ox\lib\site-packages\osmnx\folium.py in <listcomp>(.0)
    203     route_nodes = list(zip(route[:-1], route[1:]))
    204     index = [
--> 205         gdf_edges[(gdf_edges["u"] == u) & (gdf_edges["v"] == v)].index[0] for u, v in route_nodes
    206     ]
    207     gdf_route_edges = gdf_edges.loc[index]

C:\ProgramData\Anaconda3\envs\ox\lib\site-packages\pandas\core\indexes\base.py in __getitem__(self, key)
   4099         if is_scalar(key):
   4100             key = com.cast_scalar_indexer(key, warn_float=True)
-> 4101             return getitem(key)
   4102 
   4103         if isinstance(key, slice):

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

绘图工作就像

fig, ax = ox.plot_graph_route(Gu, route_undirected)

标签: osmnx

解决方案


请参阅文档:您正在使用的函数需要一个MultiDiGraph参数,但您正在传递它 a MultiGraph。尽管该函数不强制执行严格的类型检查,但您已经向它传递了一个意外的参数类型。


推荐阅读