Skip to content

Instantly share code, notes, and snippets.

@acoiman
Created July 5, 2020 16:38
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 acoiman/048648cd99f4a056203ff1beeda9bc5f to your computer and use it in GitHub Desktop.
Save acoiman/048648cd99f4a056203ff1beeda9bc5f to your computer and use it in GitHub Desktop.
Haversine distance formula
import math
x_dist = math.radians(lon2-lon)
y_dist = math.radians(lat2-lat)
y1_rad = math.radians(lat)
y2_rad = math.radians(lat2)
a = math.sin(y_dist/2)**2 + math.sin(x_dist/2)**2 \
* math.cos(y1_rad) * math.cos(y2_rad)
c = 2*math.asin(math.sqrt(a))
distance = c * 6371 # kilometers
print()
print("Distance between the strongest earthquake (ID:"+str(id) +") and weakest earthquake \
(ID:"+str(id2)+"): "+str(round(distance,2))+" km.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment