Created
August 16, 2018 15:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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