Python icon indicating copy to clipboard operation
Python copied to clipboard

Haversine distance may be using the wrong phi1 and phi2 angle

Open lucasalavapena opened this issue 2 years ago • 3 comments

Repository commit

2e405f397bbcefccc470f215c7ff024875ef16c5

Python version (python --version)

NA

Dependencies version (pip freeze)

NA

Expected behavior

First of thanks for all the algorithms and the work!

The current (and initial) implementation of the haversine distance is using reduced latitudes (as one does for Lambert's formula see https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines), however, from my understanding the haversine distance should be using the raw latitudes (in radians like the longitudes are currently being handled). At least that is how it is written in https://en.wikipedia.org/wiki/Haversine_formula#Formulation .

The haversine distance from my understanding is supposed to work on a sphere, however, the notion on reduced latitude and flattening factors exist to deal with the non-spherical nature of the Earth (see https://en.wikipedia.org/wiki/Flattening). I am by no means an expert, I could very well be wrong.

Moreover, reading the wikipage and other implementations the transformation to a reduced latitude is not needed for the haversine distance, the latitude is used directly. Here are some references:

  • https://en.wikipedia.org/wiki/Haversine_formula#Formulation (also linked in the implementations comments)
  • https://github.com/mapado/haversine/blob/main/haversine/haversine.py#L183 from https://pypi.org/project/haversine/

additional implementations:

  • https://gist.github.com/vananth22/888ed9a22105670e7a4092bdcf0d72e4
  • https://stackoverflow.com/a/4913653

I think most likely there was some confusion with Lambert's formula.

I am happy to make a small PR, if there is an agreement with my observation.

Actual behavior

Use the right angles and the results should slightly be different. I did some test, it is not that significant.

lucasalavapena avatar Feb 26 '24 21:02 lucasalavapena

The issue is resolved by me, please review my PR :)

Maanit491 avatar Oct 01 '24 21:10 Maanit491

Kindly review my PR @lucasalavapena

Maanit491 avatar Oct 09 '24 13:10 Maanit491

Haversine Distance Calculation Issue ​The second image describes an issue where the implementation of the Haversine distance seems to be using "reduced latitudes" or may be confusing the Haversine formula with the Lambert formula. ​The user's main concern is: Haversine distance may be using the wrong phi1 and phi2 angle. ​Understanding the Formulas ​The Haversine formula is used to calculate the great-circle distance between two points on a sphere (like the Earth) given their longitudes and latitudes. ​The standard Haversine formula for distance d between two points (\phi_1, \lambda_1) and (\phi_2, \lambda_2) on a sphere of radius R is: a = \sin^2\left(\frac{\phi_2 - \phi_1}{2}\right) + \cos(\phi_1) \cos(\phi_2) \sin^2\left(\frac{\lambda_2 - \lambda_1}{2}\right) c = 2 \arctan2(\sqrt{a}, \sqrt{1-a}) d = R \cdot c Conclusion and Next Steps ​If the code is implementing the Haversine distance based on the assumption of a perfect sphere (which is what the Haversine formula is for), reduced latitudes are not needed. The use of reduced latitudes would only be necessary if the calculation were accounting for the Earth's flattening (an ellipsoidal shape). ​If you are the developer or are responding to the user, here is the suggested action: ​Verify the Formula: Check the source code to see if it is using \phi (geodetic latitude) or \beta (reduced latitude) in the trigonometric calculations. ​Confirm the Model: If the formula is intended for an ellipsoid (flattened Earth), then using a method like Vincenty's or calculating the reduced latitude might be appropriate, but that would mean the formula is not a pure Haversine implementation. ​Correct the Latitudes: If the goal is a standard Haversine calculation on a sphere, ensure the input latitudes (\phi_1 and \phi_2) are used directly.

Asukasuk avatar Dec 14 '25 11:12 Asukasuk