olsiv
olsiv

Reputation: 21

How to plot a path based on some "manually chosen" nodes on osmnx?

I want to plot a route on osmnx map. When i try to create a route manually it doesn't work.

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)

I expect a map with the colored route ,but i get this error instead.

    ---------------------------------------------------------------------------
    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

if i try to find a route by a shortest path algorithm and visualize it on the map, it works fine. But I want to plot this specific route

Upvotes: 2

Views: 2447

Answers (2)

Subhrasankha Dey
Subhrasankha Dey

Reputation: 56

I have done for a shortest path, for anypath please make sure your paths are connected in terms of osmid, That you can find by simply querying the path from nx.simple_paths(G,source,destination). If the osmid s are not connected you will get this errror. Hope this helps. :)

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)

path created between the nodes

Upvotes: 1

gboeing
gboeing

Reputation: 6412

OSMnx uses the OSMIDs of nodes. When you run G=nx.convert_node_labels_to_integers(G), you overwrite these OSMIDs. However, you did not also overwrite them in the graph edges' u and v attributes, which identify the endpoints of the edge (using OSMIDs).

Upvotes: 1

Related Questions