I have been reading the documentation on OSMnx and I am trying to use the command osmnx.shortest_path(G, orig, dest, weight = "length") on a graph of a city. I know which nodes I want to define as "orig" and "dest", but I can't figure out how to get their IDs.

I saw on the example given on the example repo of osmnx that list(g)[index] is used but I do not understand how to get the index I want for a given node.


If you know the locations you want to route from and to, you can use the built-in method nearest_nodes:

import osmnx as ox

place = "Piedmont, California, USA"
G = ox.graph_from_place(place, network_type="drive")
Gp = ox.project_graph(G) # convert to UTM

orig_point = (567866.7056592009, 4186342.41614376) # UTM coordinates
dest_point = (566824.8126361968, 4186063.440007686)

orig_node, dest_node = ox.nearest_nodes(
                            X=[orig_point[0], dest_point[0]],
                            Y=[orig_point[1], dest_point[1]]

The method returns the osmids of the nodes closest to the input points. These now serve as input for the routing:

route = ox.shortest_path(Gp, orig_node, dest_node, weight="length")
  • I do not understand how to obtain the UTM coordinates, I only have the coordinates in Open street map, which don't give the desired result when inputting them on the orig_point and dest_point. How do i transform those to UTM? Many thanks Jul 11 '21 at 15:05
  • Try Pyproj
    – Christian
    Jul 12 '21 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.