-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created geodesy section with one algorithm #1757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
36c1792
395a806
e440ed2
3dfd133
e037a9f
73e8401
b5d09f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from math import asin, atan, cos, sin, sqrt, tan, pow, radians | ||
|
||
|
||
def haversine_distance(lat1, lon1, lat2, lon2): | ||
""" | ||
Calculate great circle distance between two points in a sphere, | ||
given longitudes and latitudes. | ||
(https://en.wikipedia.org/wiki/Haversine_formula) | ||
|
||
Args: | ||
lat1, lon1: latitude and longitude of coordinate 1 | ||
lat2, lon2: latitude and longitude of coordinate 2 | ||
|
||
Returns: | ||
geographical distance between two points in metres | ||
|
||
>>> int(haversine_distance(37.774856, -122.424227, 37.864742, -119.537521)) # From SF to Yosemite | ||
254352 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the unit of measure in great circle calculations? Google maps says 164 miles or 264 kilometers but that is road distance, not as-the-crow-flies distance. Helping readers connect what they are learning with what they already know will improve the value of this submission. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm planning on adding two more algorithms which improve approximation so haversine is a good starter method. Perhaps, a readme file can follow with comparisons and detailed explanations. For now: We know that the globe is "sort of" spherical, so a path between two points There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the unit of measure? Is it meters, kilometres, miles, lightyears? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 15 - metres.. Clarified that in the latest commit as well. unit of measure meant is irrelevant for explanation. |
||
|
||
""" | ||
|
||
# CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System | ||
AXIS_A = 6378137.0 | ||
AXIS_B = 6356752.314245 | ||
RADIUS = 6378137 | ||
|
||
# Equation parameters | ||
# Equation https://en.wikipedia.org/wiki/Haversine_formula#Formulation | ||
flattening = (AXIS_A - AXIS_B) / AXIS_A | ||
phi_1 = atan((1 - flattening) * tan(radians(lat1))) | ||
phi_2 = atan((1 - flattening) * tan(radians(lat2))) | ||
lambda_1 = radians(lon1) | ||
lambda_2 = radians(lon2) | ||
|
||
# Equation | ||
sin_sq_phi = pow(sin((phi_2 - phi_1) / 2), 2) | ||
sin_sq_lambda = pow(sin((lambda_2 - lambda_1) / 2), 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about sqrt vs ** .5? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to this stackoverflow thread you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And as a side note, we can simplify the above lines even more: So you can change the above lines to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did that and made it a little more readable |
||
h_value = sqrt(sin_sq_phi + (cos(phi_1) * cos(phi_2) * sin_sq_lambda)) | ||
|
||
distance = 2 * RADIUS * asin(h_value) | ||
|
||
return distance | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Uh oh!
There was an error while loading. Please reload this page.