[SalesForce] What formulae does the Geolocation Distance use

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,

enter image description here

and 19960 km.

shortest ellipsoidal path between Wellington and Salamanca

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.


The documentation on the DISTANCE function.

Best Answer

The documentation states:

Distance is calculated as a straight line—as the crow flies—regardless of geography and topography between the two points.

In reality Salesforce uses the haversine formula to calculate the great-circle distance between two points.

For any two points on a sphere, the haversine of the central angle between them is given by \operatorname{haversine}\left(\frac{d}{r}\right) = \operatorname{haversin}(\phi_2 - \phi_1) + \cos(\phi_1) \cos(\phi_2)\operatorname{haversin}(\lambda_2-\lambda_1)

where

  • haversin is the haversine function:
    \operatorname{haversin}(\theta)=\sin^2\left(\frac{\theta}{2}\right)=\frac{1-\cos(\theta)}{2}
  • d is the distance between the two points (along a great circle of the sphere; see spherical distance),
  • r is the radius of the sphere,
  • \phi1, \phi2: latitude of point 1 and latitude of point 2
  • \lambda_1, \lambda_2: longitude of point 1 and longitude of point 2

On the left side of the equals sign d/r is the central angle, assuming angles are measured in radians (note that φ and λ can be converted from degrees to radians by multiplying by π/180 as usual).

Solve for d by applying the inverse haversine (if available) or by using the arcsine (inverse sine) function:

d = r  \operatorname{haversin}^{-1}(h) = 2 r \arcsin\left(\sqrt{h}\right)

where h is haversin(d/r), or more explicitly:

d = 2 r \arcsin\left(\sqrt{\operatorname{haversin}(\phi_2 - \phi_1) + \cos(\phi_1) \cos(\phi_2)\operatorname{haversin}(\lambda_2-\lambda_1)}\right) = 2 r \arcsin\left(\sqrt{\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)}\right)

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)

  • Salesforce: 19,968.02154120 km
  • Haversine Calculator: 19970 km (to 4 SF)

London (51.507222, -0.1275) to Tokyo (35.689506, 139.6917)

  • Salesforce: 9,558.73252169 km
  • Haversine Calculator: 9559 km (to 4 SF)

Moscow (55.75, 37.616667) to Nice (43.7034, 7.2663)

  • Salesforce: 2,529.41039443 km
  • Haversine Calculator: 2529 km (to 4 SF)

Madrid (40.383333, -3.716667) to Sydney (-33.859972, 151.211111)

  • Salesforce: 17,686.13743193 km
  • Haversine Calculator: 17690 km (to 4 SF)

Pensacola (30.433333, -87.2) to Asmara (15.333333, 38.933333)

  • Salesforce: 12,329.08258719 km
  • Haversine Calculator: 12330 km (to 4 SF)

A couple of related posts have since appeared in the Salesforce documentation.

How SOQL Calculates and Compares Distances

The DISTANCE function approximates the haversine, or “great circle,” distance calculation within 0.0002%. This formula assumes that the Earth is a perfect sphere, when in fact it’s an ellipsoid: an irregular one. Errors from this assumption can be up to 0.55% crossing the equator, but are usually below 0.3%, depending on latitude and direction of travel.

The DISTANCE function is fine for calculating the 10 stores closest to a customer’s current location. But don’t fuel your plane for a flight from San Francisco to Sydney based on it.

Blog Post: Geolocation Field Types in Salesforce1 Platform

The distance calculation is based on the haversine formula, which is used in mapping and navigation applications and is illustrated below.

enter image description here