0

I have created a graph representing NYC road network. I am trying to calculate a path between two nodes using Networkx shortest_path. Running it like this works fine and I'm able to visualize the route. However, the route is not following the correct direction of traffic. I would like to somehow set shortest_path weight equal a function that returns 0 if the edge attribute says the direction of traffic is against, and return 1 if the attribute says with or two way. Like this, the algo will disregard edges that are in wrong direction of traffic.
I'm using custom data, in which there is a column called "trafdir" which stores the traffic direction value.

# geojson file of roads
st = geopandas.read_file("test1.geojson")

# create graph
G = momepy.gdf_to_nx(st, approach="primal")


def flow_of_traffic(u, v, attribute_dict):
    
    # if edge["trafdir"] == "Against" return 0
    # if edge["trafdir"] == "With" or "Two-way" return 1


destination = (-73.96715254341488, 40.75676519476177)  # 2nd and 53th
origin = (-73.96852047039987, 40.754876190454986) # 2nd and 50th 


shortest_path = nx.shortest_path(G, origin, destination, weight =flow_of_traffic)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.