I want to generate a NetworKX graph with weighted edges so the weight of each edge will be its distance * driving speed on this road(if it exists)
or if the driving speed is unknown, 100*distance
for highways and 60*distance
for city roads.
I couldn't find a post similar to my needs except this one but there has to be a way to do it automatically.
My goal is to find the path with the shortest time(with Dijkstra) of driving between point A to B and this is what I did until now:
l1 = (A_lat,A_lon)
G = ox.graph_from_point(l1,distance= 100)
l1_node_id = ox.get_nearest_node(G,l1) # Find closest node ID
l2 = (B_lat,B_lon)
G = ox.graph_from_point(l2,distance = 100)
l2_node_id = ox.get_nearest_node(G,l2) # Find closest node ID
dist = vincenty(l1, l2).meters # The distance between l1 and l2
p1 = ((l1[0] + l2[0])/2,(l1[1]+l2[1])/2) #The mid point between l1 and l2
dist = vincenty(l1, l2).meters #The distance between l1 and l2
G = ox.graph_from_point(p1,distance = dist)
path = nx.shortest_path(G, l1_node_id, l2_node_id) #Find the shortest path for cutoff
for path in nx.all_simple_paths(G, source=l1_node_id, target=l2_node_id,cutoff = len(path)):
#Here I want to checke if "path" is the shortest path but right now it is without weight
In the documentation they wrote weight should be a string but how can I do it?
TIA
weight = 'distance'
. That is you should use whatever you've named your attribute. – Joeltime of travel
as weight. In that case,distance/speed
is what you need. – swatchai