'From OSM data to one way street reconfiguration - Diet the road preserving graph strong connectivity

Dears,

I am using OSM street data downloaded and manipulated with osmnx and networkx for my project. I have been able thanks to osmnx (get graph from place, simplify graph, consolidate intersections) and networkx (remove dead ends, remove self loops, take the biggest largest connected component) methods to obtain a strongly connected drive-road network.

I have now to diet the obtained network (make two way streets one way), preserving graph strong connectivity.

Osmnx saves one way streets as [a,b,k] and two way streets as [c,d,k] and [d,c,k]. I want for the whole graph to eliminate one of the two way street vectors, with the condition to still have a strongly connected graph.

Fact is that I am still new in python, and I am not sure about how to iterate what said above in code.

I have tried this, but still it does not work.

strongly_connected_graph = ox.load_graphml("./data/copygraph.graphml")
cleargraph = nx.MultiDiGraph(strongly connected graph) #a copy of the graph

for u, v, key, data in copygraph.edges(data=True, keys=True):
    for u1, v1, key1, data1 in copygraph.edges(data=True, keys=True):
        if (u,v) == (v1,u1):
            try:
               if (u,v) is not nx.bridges(cleargraph): cleargraph.remove_edge(u,v,key=key)
            except:
                print('oopsie daisz')'

Unfortunately, I am not sure this is the right way to proceed.

If anyone would have suggestions/recommendations, I would be really thankful.

Thank you in advance to this wonderful community, Lucione



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source