views:

99

answers:

2

When researching on how to do the classic "get POI in range" problem I've found that the most used algorithms are Haversine and if you need real accuracy then Vincenty's formula. I went the first one because high accuracy wasn't an issue. However, it got me thinking on something that hits me as odd, why is that I found no references to caching the Cartesean coordinates on the database instead of using the haversine formula with the lat/lon?

The issue here is, of course, performance. The haversine formula requires a ton of cos/sin function calls, but wouldn't it be simpler to store the projected X, Y and Z of a lat/lon point on the database and apply the dot product directly? That would require a single arccos call unless I'm mistaken.

+3  A: 

Because any given Cartesian projection will only give the correct answer for certain points - a projection which gives the right distance between two points on one particular circle around a sphere will distort distances along another particular circle.

Formulas such as Haversine are independent of the relative locations of the various points on the sphere; they return the correct distance regardless.

Amber
Can you give any particular example of that? As far as I understand the haversine is just a quick fix for some floating point problems over the great circle distance formula. The last however, is as simple as projecting points into cartesean coordinates and computing the dot product between them. So why is that one method is "relative" and the other isn't?
Alexandre Gomes
I think you and OA mean very different things by "Cartesian projection" here.
AVB
Yeah, I think I may have misread what kind of cartesian projection you were thinking of (3d vs 2d).
Amber
A: 

No, you are not mistaken. Two things, though. First, obviously, your performance benefits will depend on the nature of your application. If you need to use the same points many times in your calculations, you will improve performance by doing what you suggest.

Second, the formula you use is not the issue here. If you precompute and store sin and cos of lat/lon and use haversine, you will get the exact same performance improvements.

AVB
Not really. Precomputing the harversines/sin/cos might seem the same here but in fact it would barely improve performance because the haversine formula requires many deltas (e.g. haversin(lat1-lat2)) and those can't be computed when you don't know the point you will be comparing with. With a cache of the projected x, y, z all you would need would be arccos(x1*x2+y1*y2+z1*z2).As for the nature of the applications I understand your point and I mean something like Google Maps or 4Square where you wish to compare points to a list of almost "static"/rarely-changing points.
Alexandre Gomes
`haversin(lat1-lat2)` is a reasonably simple algebraic function of `sin` and `cos` of `lat1` and `lat2`, which involves no further trig. computations. Just take a look at the definition.
AVB