Skip to content

Instantly share code, notes, and snippets.

@joaopcnogueira
Created August 16, 2018 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaopcnogueira/dea9728c13802c64ecf07ac2bdebd6e7 to your computer and use it in GitHub Desktop.
Save joaopcnogueira/dea9728c13802c64ecf07ac2bdebd6e7 to your computer and use it in GitHub Desktop.
Function to calculate the euclidian distance between to points given by their latlon coordinates
import numpy as np
def latlon2km(latlon1,latlon2):
# approximate radius of earth in km
R = 6373.0
lat1 = np.radians(latlon1[0])
lon1 = np.radians(latlon1[1])
lat2 = np.radians(latlon2[0])
lon2 = np.radians(latlon2[1])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
distance = R * c
return distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment