|# Haversine formula example in Python|
|# Author: Wayne Dyck|
|def distance(origin, destination):|
|lat1, lon1 = origin|
|lat2, lon2 = destination|
|radius = 6371 # km|
|dlat = math.radians(lat2-lat1)|
|dlon = math.radians(lon2-lon1)|
|a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \|
|* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)|
|c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))|
|d = radius * c|
@osph, you could use the function with those values by adding code like this to the end:
lat1 = 40.5; lat2 = 42; long1 = -90; long2 = -93 print( distance((lat1, long1), (lat2, long2)) )
However it would be better to save the original script in a file named
import haversine lat1 = 40.5; lat2 = 42; long1 = -90; long2 = -93 print( haversine.distance((lat1, long1), (lat2, long2)) )
The script may have been written in IDLE, or any one of several other programming tools, including just a plain text editor -- it doesn't really matter.
@marioidival The units of the equation are determined by the units of 'radius'. Which corresponds to the spherical radius of the Earth.
3959 # radius of the great circle in miles...some algorithms use 3956
Vector implementation (you can pass 4 numpy arrays instead of scalar values):
"Vehicle ID","Frame ID","Total Frames","Global Time","Local X","Local Y","Global X","Global Y","V_Len","V_Width","V_Class","V_Vel","V_Acc","Lane_ID","Pre_Veh","Fol_Veh","Spacing","Headway"
But when I am trying to find the distance between two adjacent points of the same vehicle, Its giving
in more than 20 kms..
Can any you help me to find the distance between two adjacent trajectories
#I am adding the code to calculate the distance between two coordinates, you can call this function inside a for loop to get the distance between source and destination as soon as your bot makes a displacement.
#CALLING THE FUNCTION
Hope this helps!!
Hello @Zagroz, did you able to find the distances from an excel file using this code? I am also stuck in similar kind of situation. Thank you!
Hello, I have implemented something similar to what you are trying to do. You can visit my repo which involves what you desire. The link is here:
Please inform if this solves your problem.