0

I have a dictionary of coordinates I am using for the placement of nodes. I am using the networkx module for making graphs.

For my project, i need to know the real distance in m or km of al the edges in the graph. I am now using the eucledian distance, but ofcourse that is not the real distance. I am using lon lat coordinates.

This is how, a part of, my dictionary looks like:

coord = {0:[ 4.98666883, 52.29477044],
1: [ 4.96969627 ,52.31851732],
2: [ 4.95026886 ,52.31294893],
3: [ 4.97016485 ,52.30191036],
4: [ 4.98254078 ,52.32017747],
5: [ 4.99130712 ,52.29755548],
6: [ 4.97406065 ,52.28985602]}

And this is the definition i am using for adding weight to the edges:

def Length(G,coord):
    for i,j in G.edges():
        G[i][j]['weight'] = euclidean(coord[i],coord[j])
    return G

And this is the code i am using for the rest of the graph.

G = nx.Graph()
G.add_nodes_from(coord.keys())
G = nx.complete_graph(coord.keys())
G = Length(G,coord) #Dit gaat goed nu
print(TotalLength(G))

out: 500 meters

For printing the total length i am using:

Def TotalLength(G):
    return G.size('weight')

This defition works everytime.

What can i do to add the actual distance to the edges?

Thanks in advance

  • 1
    You need to use the Haversine formula: https://en.wikipedia.org/wiki/Haversine_formula. See this question: https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points – nonDucor Dec 08 '21 at 15:58
  • I think I wasn't specific enough, but I want the total length of al the edges in this Graph. Is this formula still applicable? – user17335922 Dec 08 '21 at 16:00
  • 1
    Does this answer your question? [Haversine Formula in Python (Bearing and Distance between two GPS points)](https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points) – Code-Apprentice Dec 08 '21 at 16:04
  • 1
    @user17335922 Step back from the computer for a minute. What does "total length of all edges in the graph" represent? How would you calculate this by hand? What information do you need in order to calculate it? – Code-Apprentice Dec 08 '21 at 16:04
  • Yes I think this problem is similar to this question. But is there any way I can calculate the Length of all edges in the graph at once? I am a real rookie. – user17335922 Dec 08 '21 at 16:06
  • By hand you would calculate the length of each edge using haversine, then add them all together. And that's *exactly* how you would program the calculation. Is there any reason it shouldn't be this simple? – DisappointedByUnaccountableMod Dec 08 '21 at 16:09
  • I there any way i can use haversine using haversine(node1,node2) and that I will automatically take the lon lat coordinates and calculate it? – user17335922 Dec 08 '21 at 16:16
  • Thanks guys, you helped me out! – user17335922 Dec 08 '21 at 17:28

1 Answers1

0
from haversine import haversine

def TotalLength(G,coord):
    for i,j in G.edges():
        G[i][j]['weight'] = haversine(coord[i],coord[j])
    return G

def Length(G):
    return G.size('weight') 
print(TotalLength(G))

I also had to change my coordinates from [ 4.98666883, 52.29477044] to [52.29477044, 4.98666883].

Thanks everyone for your help.

Sincerely,

A programming dummy who somehow has to do his thesis in python without programming experience.

divibisan
  • 11,659
  • 11
  • 40
  • 58