making a networkx network from geodataframe of a line- how2 make the network edges' weights correspond to the length of the line (in m) between nodes

40 views Asked by At

I'm a noob when it comes to networkx module. I have a line feature as a geodataframe that looks like this: https://i.stack.imgur.com/V8Fqt.png

Using momepy, I can convert this geodataframe to a network on network x:

G = momepy.gdf_to_nx(my_line, approach="primal",length='mm_len')

This converts the geodataframe to a network that looks like this, with the blue points as the nodes created by momepy: https://i.stack.imgur.com/7Smb9.png

My aim is to find the pair of nodes that are furthest away from one another.

I can do this by this code:

    apl = dict(nx.all_pairs_dijkstra_path(G))
    bpl = [tuple((v, s, k)) for s, d in apl.items() for k, v in d.items() if v == max(d.values())]
    bpl.sort(key=lambda tup: tup[0], reverse=False) 
    length, start, fin = bpl[0]

however this works out the distances based on how many nodes the path goes through, not the actual length of the path. so returning 'length' just gets a list of coordinates. However I have no idea how to store the length of the path in the network. I want the data of the graph to be something like this: https://i.stack.imgur.com/wsBY4.png

so that when I run the dijkstra it will return the top left node and the bottom node and say the distance between them is 21m, this is the longest. Any ideas?

0

There are 0 answers