I need to implement a Geo proximity search in my application but I'm very confused regarding the correct formula to use. After some searches in the Web and in StackOverflow I found that the solutions are:
- Use the Haversine Formula
- Use the Great-Circle Distance Formula
- Use a Spatial Search Engine in the Database
Option #3 is really not an option for me ATM. Now I'm a little confused since I always though that the Great-Circle Distance Formula and Haversine Formula were synonymous but apparently I was wrong?
The above screen shot was taken from the awesome Geo (proximity) Search with MySQL paper, and uses the following functions:
ASIN, SQRT, POWER, SIN, PI, COS
I've also seen variations from the same formula (Spherical Law of Cosines), like this one:
(3956 * ACOS(COS(RADIANS(o_lat)) * COS(RADIANS(d_lat)) * COS(RADIANS(d_lon) - RADIANS(o_lon)) + SIN(RADIANS(o_lat)) * SIN(RADIANS(d_lat))))
That uses the following functions:
ACOS, COS, RADIANS, SIN
I am not a math expert, but are these formulas the same? I've come across some more variations, and formulas (such as the Spherical Law of Cosines and the Vincenty's formulae - which seems to be the most accurate) and that makes me even more confused...
I need to choose a good general purpose formula to implement in PHP / MySQL. Can anyone explain me the differences between the formulas I mentioned above?
- Which one is the fastest to compute?
- Which one provides the most accurate results?
- Which one is the best in terms of speed / accuracy of results?
I appreciate your insight on these questions.
Based on theonlytheory answer I tested the following Great-Circle Distance Formulas:
- Vincenty Formula
- Haversine Formula
- Spherical Law of Cosines
The Vincenty Formula is dead slow, however it's pretty accurate (down to 0.5 mm).
The Haversine Formula is way faster than the Vincenty Formula, I was able to run 1 million calculations in about 6 seconds which is pretty much acceptable for my needs.
The Spherical Law of Cosines Formula revealed to be almost twice as fast as the Haversine Formula, and the precision difference is neglectfulness for most usage cases.
Here are some test locations:
- Google HQ (
37.422045
, -122.084347
)
- San Francisco, CA (
37.77493
, -122.419416
)
- Eiffel Tower, France (
48.8582
, 2.294407
)
- Opera House, Sydney (
-33.856553
, 151.214696
)
Google HQ - San Francisco, CA:
- Vincenty Formula:
49 087.066 meters
- Haversine Formula:
49 103.006 meters
- Spherical Law of Cosines:
49 103.006 meters
Google HQ - Eiffel Tower, France:
- Vincenty Formula:
8 989 724.399 meters
- Haversine Formula:
8 967 042.917 meters
- Spherical Law of Cosines:
8 967 042.917 meters
Google HQ - Opera House, Sydney:
- Vincenty Formula:
11 939 773.640 meters
- Haversine Formula:
11 952 717.240 meters
- Spherical Law of Cosines:
11 952 717.240 meters
As you can see there is no noticeable difference between the Haversine Formula and the Spherical Law of Cosines, however both have distance offsets as high as 22 kilometers compared to the Vincenty Formula because it uses an ellipsoidal approximation of the earth instead of a spherical one.
See Question&Answers more detail:
os