Created
June 16, 2021 14:43
-
-
Save bennyistanto/be50c78a8da82f66646610fffa64b3cc to your computer and use it in GitHub Desktop.
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
# Haversine formula using Py3 | |
import math | |
def haversine(lat1, lon1, lat2, lon2): | |
# distance between latitudes and longitudes | |
dLat = (lat2 - lat1) * math.pi / 180.0 | |
dLon = (lon2 - lon1) * math.pi / 180.0 | |
# convert to radians | |
lat1 = (lat1) * math.pi / 180.0 | |
lat2 = (lat2) * math.pi / 180.0 | |
# apply formulae | |
a = (pow(math.sin(dLat / 2), 2) + | |
pow(math.sin(dLon / 2), 2) * | |
math.cos(lat1) * math.cos(lat2)); | |
rad = 6371 | |
c = 2 * math.asin(math.sqrt(a)) | |
return rad * c | |
# Coordinate location | |
if __name__ == "__main__": | |
# Monumen Nasional | |
lat1 = -6.175369563583253 | |
lon1 = 106.82715520757206 | |
# Tugu Pahlawan | |
lat2 = -7.2458375806095665 | |
lon2 = 112.73781723559868 | |
print(haversine(lat1, lon1,lat2, lon2), "K.M.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment