Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Created June 6, 2012 17:43
Show Gist options
  • Save rochacbruno/2883505 to your computer and use it in GitHub Desktop.
Save rochacbruno/2883505 to your computer and use it in GitHub Desktop.
Calculate distance between latitude longitude pairs with Python
#!/usr/bin/env python
# Haversine formula example in Python
# Author: Wayne Dyck
import math
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
return d
@killerontherun1
Copy link

Thanks so much! Just what I needed.

@Edward-Aidi
Copy link

Great! Thanks!

@Zagroz
Copy link

Zagroz commented Mar 9, 2019

Hello, thank you very much for this masterpiece. But my concern is how to do so when you have an excel file, I have bunch of cities and finding the distance from those cities to one reference point (which is also a city). Thanks

@fudigit
Copy link

fudigit commented Jun 1, 2019

Thanks much , this helped a machine learning engineer.

@AnguKumar
Copy link

Hi
I need to find the distance between two gps trajectories, from US 101 dataset, which covers totally 2000ft distance.

"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"

2,13,437,1118846980200,16.467,35.381,6451137.641,1873344.962,14.5,4.9,2,40.00,0.00,2,0,0,0.00,0.00
2,14,437,1118846980300,16.447,39.381,6451140.329,1873342.000,14.5,4.9,2,40.00,0.00,2,0,0,0.00,0.00
2,15,437,1118846980400,16.426,43.381,6451143.018,1873339.038,14.5,4.9,2,40.00,0.00,2,0,0,0.00,0.00
2,16,437,1118846980500,16.405,47.380,6451145.706,1873336.077,14.5,4.9,2,40.00,0.00,2,0,0,0.00,0.00
2,17,437,1118846980600,16.385,51.381,6451148.395,1873333.115,14.5,4.9,2,40.00,0.00,2,0,0,0.00,0.00

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 need to segregate the dataset into subsections covering 200ft distance each..

@joaovs12
Copy link

joaovs12 commented Sep 4, 2019

Hello, I have a list with 700 cities and I want to find the city where the sum of distances will be minimun
I defined a function that will give me that sum, but what I should do to find the lat,long where the sum of distances will be minimum? tks

@zuzhaoye
Copy link

Thank you! @MalyutinS

@manikyalarao16
Copy link

Hello i have two co-ordinates values sources and destination ,my source co-ordinates values are changing when i move robot , for that i have to calculate distance for each positional values pls help me how to write python code for that

@vivek1240
Copy link

vivek1240 commented Jun 11, 2020

Hello i have two co-ordinates values sources and destination ,my source co-ordinates values are changing when i move robot , for that i have to calculate the distance for each positional values pls help me how to write python code for that

#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.

def distance(source , destination): 
    lat1, lon1 = source [0],source [1]
    lat2, lon2 = destination[0],destination[1]
    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
    d = d*1000  #Converting distance to Metre as bot will make small displacements
    return d

#CALLING THE FUNCTION

source = [lat1, lon1] #Coordinates for the initial position of the robot
distance_measured = 0 #initially total distance covered by bot is 0.
Loop:
   new_latitude = GPS.latitude #  get current latitude of bot
   new_longitude = GPS.longitude # get current longitude bot
   destination = [new_latitude , new_longitude ]
   if (distance(source, destination) != 0):
         distance_measured = distance_measured + distance(source, destination)
         last_latitude = new_latitude  
	 last_longitude = new_longitude
         source =[last_latitude,last_longitude] 

Hope this helps!!

@AsadTanvir
Copy link

AsadTanvir commented Jul 28, 2020

Hello, thank you very much for this masterpiece. But my concern is how to do so when you have an excel file, I have bunch of cities and finding the distance from those cities to one reference point (which is also a city). Thanks

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!

@vivek1240
Copy link

Hello, thank you very much for this masterpiece. But my concern is how to do so when you have an excel file, I have a bunch of cities and finding the distance from those cities to one reference point (which is also a city). Thanks

Hello @Zagroz, did you able to find the distances from an excel file using this code? I am also stuck in a 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:

https://github.com/vivek1240/k-means-clustering-via-haversine-distance-/blob/master/clustering_stores_via_haversine_distance_kmeans.ipynb

Please inform if this solves your problem.
All the Best!!

@biyak
Copy link

biyak commented Nov 11, 2020

Thanks for the eloquent and easily understandable code!

@sreedhar007
Copy link

Thanks for this, can anyone explain what are 'a' and 'c' assignments , can we have better names for these variables ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment