Hope you can help me with the following, proably very simple but I just can't seem to get it.
I want to set nodesnames to my nodes in my graph, so I can use them later for dijkstra's algorithm from the networkx package. The shapefile has nodes based on long lat, as shown below:
crossings = list(G.nodes(data = True))
print(crossings)
Gives:
[((4.8703865, 52.364651), {}), ((4.8719547, 52.3651758), {}), ((4.9264105, 52.3695602), {}), ((4.9289823, 52.3711744), {}), ((4.9259642, 52.3692824), {}), ((4.877608, 52.3752153), {}), ((4.8847629, 52.3765112), {}), ((4.8701251, 52.3757447), {}), ((4.8738594, 52.3804434), {}), ((4.8866343, 52.3574955), {}), ((4.8873865, 52.3602753), {}), ((4.8792688, 52.3649914), {}), ((4.8775365, 52.366768), {}), ((4.879805, 52.3667268), {}), ((4.8824711, 52.3674209), {}), ((4.8790738, 52.3677393), {}), ((4.8771909, 52.3704447), {}) .....
Now I want to set nodenames, as input for the following code:
print(nx.dijkstra_path(weighted_G, "1" , "20", weight='cost'))
But I can't seem to figure out how to set a name to each node as a number, so node 1 gets name "1", node "2" gets name "2", till node n gets "n".
G=nx.read_shp('shapefile.shp', simplify=True) # shapefile from OSM
crossings = list(G.nodes(data = True))
weighted_G = nx.Graph()
j = 0
for i in crossings:
nx.set_node_attributes(weighted_G, values = "none" ,name = j)
j = j + 1
print(crossings)
But this output gives:
[((4.8703865, 52.364651), {}), ((4.8719547, 52.3651758), {}), ((4.9264105, 52.3695602), {}), ((4.9289823, 52.3711744), {}), ((4.9259642, 52.3692824), {}), ((4.877608, 52.3752153),.......
As you can see, no number has been added as nodename, does anybody know how to fix this?
Thanks in advance
EDIT and update--------------------------------------------------------------
So I changed my code last night into:
weighted_G=nx.read_shp('shapefile.shp', simplify=True) # shapefile from OSM
c = list(weighted_G.nodes(data=True))
j = 0
for i in weighted_G.nodes:
weighted_G.node[i]['name']= j
j = j + 1
print (c[0])
Which got the same output as the code @zohar.kom provided. Each node now contains the following:
((4.8703865, 52.364651), {'name': 0})
But Dijkstra can't read this input if I say:
print(nx.dijkstra_path(weighted_G, {'name': 1} , {'name': 20}, weight='cost'))
But as @zohar.kom said, which gave me more insight, it changes the attribute, and not the name of the node itself; which is needed for the dijkstra algorithm in networkx.
Any thoughts on how I can change/add a name or a label to each node? So that my input for Dijkstra is
print(nx.dijkstra_path(weighted_G, 'node 1' , 'node 20', weight='cost'))
and each node will look something like this:
(node 1, (4.8719547, 52.3651758))
instead of:
((4.8703865, 52.364651), {'name': 0})
Hope you can help me with this!
UPDATE 2------------------------------------------------------------------
With the help of @zohar.kom I managed to finally solve it. When I directly used the code from @zohar.kom, I still got an error in
for edge in G.edges():
w_G.add_edge(lat_lon_to_index[edge[0]], lat_lon_to_index[edge[1]], cost=edge[2]['cost_property_name'])
saying:
IndexError: tuple index out of range
But I solved that last part with the following by adding data = True:
for edge in G.edges(data=True):
w_G.add_edge(lat_lon_to_index[edge[0]], lat_lon_to_index[edge[1]], cost=edge[2]['cost_property_name'])
Which solved it completely!! So here is the final code for anyone!
G=nx.read_shp('path/shapefile.shp', simplify=False) # use simplify is false otherwise chart get shifted
w_G = nx.DiGraph()
lat_lon_to_index = {}
for i, node in enumerate(G.nodes()):
w_G.add_node(i, lat_lon= node)
lat_lon_to_index[node] = i
for edge in G.edges(data=True):
w_G.add_edge(lat_lon_to_index[edge[0]], lat_lon_to_index[edge[1]], weight=edge[2]['cost'])
c =list(w_G.nodes(data = True))
j = list(w_G.edges(data = True))
print(c[1])
print (j[2])
gives:
(1, {'lat_lon': (4.8716933, 52.3650215)})
(1, 2, {'weight': 122.826431079961})
Because, like @zohar.kom said, I already have attributes for each edge in the shapefile. So all I had to do was add them. Thanks a lot!!