Forgive me if this is a bit extremely academic in nature, but I was wondering how Salesforce goes about determining the distance between two pairs of latitude and longitude?
When I've looked at this in the past it turned out not to be as straight forward as you might think. See Geographical distance and the second (inverse) geodetic problem. The earth is generally an ellipsoidal shape and there are discontinuities in the coordinate system. It's probably safe to assume changes in elevation aren't being taken into account in distance calculations.
Are they using a great-circle distance formula?
Why might this be important
How far is it from Wellington, New Zealand (41°19'S 174°49'E) to Salamanca, Spain(40°58'N 5°30'W)?
Depending on the model you use could be the difference between 19951 km,
and 19960 km.
Is that 0.04% difference important or even meaningful? Probably not for the majority of applications. Hence the statement at the start of the question that this is purely out of academic interest.
Using a Contact with a Location field set at -41.31666667 174.81666667 (Wellington) and the following SOQL query returned the Contact ID:
select Id from Contact WHERE DISTANCE(Location__c, GEOLOCATION(40.96666667, -5.50000000), 'km') > 19968.0
Increasing the distance to 19968.1 km did not.
Again, an 8 km difference wouldn't usually be considered significant on a ~20,000 km length where elevations are being ignored anyway.
Best Answer
The documentation states:
In reality Salesforce uses the haversine formula to calculate the great-circle distance between two points.
You can confirm this by creating a custom object with two geolocation fields and a formula field which uses the
DISTANCE
formula to calculate the distance between the two locations, and then comparing the results to this haversine calculator.Here are some examples:
Wellington (-41.31666667, 174.81666667) to Salamanca (40.96666667, -5.5)
London (51.507222, -0.1275) to Tokyo (35.689506, 139.6917)
Moscow (55.75, 37.616667) to Nice (43.7034, 7.2663)
Madrid (40.383333, -3.716667) to Sydney (-33.859972, 151.211111)
Pensacola (30.433333, -87.2) to Asmara (15.333333, 38.933333)
A couple of related posts have since appeared in the Salesforce documentation.
How SOQL Calculates and Compares Distances
Blog Post: Geolocation Field Types in Salesforce1 Platform