Project-OSRM / osrm-backend

Open Source Routing Machine - C++ backend
http://map.project-osrm.org
BSD 2-Clause "Simplified" License
6.41k stars 3.4k forks source link

Haversine vs Great Circle Distance #3567

Closed daniel-j-h closed 7 years ago

daniel-j-h commented 7 years ago

In Coordinate calculation it seems like we provide

for measuring distance between two coordinates on a sphere (this is usually called great circle distance).

From what I can see the greatCircleDistance titled function really is a equirectangular approximation.

The only reason for using it I can come up with is speed. Are those calls really carefully sprinkled in when we need to care about the performance? Any insights here?

cc @TheMarex @oxidase

oxidase commented 7 years ago

greatCircleDistance is the haversine distance equation where the haversine is approximated as x^2/4 for small x and cos(phi_1)cos(phi_2) as cos^2((phi_1+phi_2)/2) for close phi_1 and phi_2.

I think also the reason to use the approximated version is only performance, but to check how it is better some profiling is needed.

TheMarex commented 7 years ago

The original cycle here was something like we started out with haversine everywhere since it is the most precise formula that is somewhat fast, then switched everything to great circle distance to get a performance speedup. However we realized that great circle distance is not precise enough for some things and switched them back.

I would not call it carefully sprinkled but at least picked with some care.

daniel-j-h commented 7 years ago

Interesting. I'm closing this as interesting but not actionable for us at the moment.

We can profile and check in the future if needs be.

kkdd commented 7 years ago

The following is a good distance calculation from "5. Starting point for Newton’s method" in "Algorithms for geodesics" by Charles F. F. Karney (2013):

$ ./geodesic_inverse_approx.py 60.0 60.1 0.1
input: lat1= 60.000000  lat2= 60.100000  lam12=     0.100000
output: az1= 26.526      az2= 26.612       s12= 12456.777
error:  az1= 1.2e-05     az2= 1.2e-05      s12=   1.3e-03

python source: https://gist.github.com/c8ad6ce4a936e456d281511e2d7a2434

felixdivo commented 6 years ago

Good read: https://stackoverflow.com/questions/38248046/is-the-haversine-formula-or-the-vincentys-formula-better-for-calculating-distan