1

How do I get the path as a geometry from networkx?

Below is my worked example which returns the nx.shortest_path_length

import osmnx as ox
import networkx as nx
from shapely.geometry import Point

def get_network(centre_point, dist):
    G = ox.graph_from_point(centre_point, dist=dist, network_type='walk', simplify=False)
    G = ox.add_edge_speeds(G)
    G = ox.add_edge_travel_times(G)
    nodes, edges = ox.graph_to_gdfs(G, nodes=True, edges=True)
    return G, nodes, edges


point = (50.864595387190924, -2.153190840083006)
G, nodes, edges = get_network(centre_point=point, dist=1000)

a = nodes.iloc[4].name
b = nodes.iloc[20].name

nx.shortest_path_length(G, a, b, weight='length', method='dijkstra')

This gives the shortest path length as 223.964, how do I get the actual path geometry of this shortest path? The paths are in edges but how do I extract the correct ones for this path?

Spatial Digger
  • 1,883
  • 1
  • 19
  • 37

1 Answers1

2

You can use shortest_path instead of shortest_path_length. Example:

nx.shortest_path(G, a, b, weight='length', method='dijkstra')

Result:

[29954468,
 1586097212,
 1586097440,
 1586097310,
 4500037088,
 2279573449,
 2279573451,
 2279573460,
 10081635099,
 1585877072,
 294588504,
 8515680389,
 294588511,
 294588514,
 8515680388,
 294588551]
maciek97x
  • 2,251
  • 2
  • 10
  • 21