geodist()

Calculates the distance between two geographic coordinates. Uses the Haversine formula to compute the spherical distance.

Syntax

geodist(LAT1, LON1, LAT2, LON2[, UNIT])

Parameters

LAT1
Latitude of the first point in degrees. Must be a numeric type.
LON1
Longitude of the first point in degrees. Must be a numeric type.
LAT2
Latitude of the second point in degrees. Must be a numeric type.
LON2
Longitude of the second point in degrees. Must be a numeric type.
UNIT
A string specifying the distance unit. Specify one of "km" (kilometers), "mile" or "mi" (miles), or "nmile" (nautical miles). Defaults to kilometers ("km") if omitted.

Description

The geodist() function accepts two pairs of latitude/longitude coordinates and returns the distance between the two points. The distance is calculated using the Haversine formula, assuming an Earth radius of 6,378 km. The return type is a 64-bit double.

Returns null if any parameter is not a numeric type or is null.

Error codes

Error codeDescription
91040An unsupported unit string was specified.

Usage examples

  1. Calculate the distance between two points in the default unit (km).

    json "{}" | eval d = geodist(37.5665, 126.9780, 35.6762, 139.6503)
    | # d: 1150.6201676325247
    
  2. Calculate the distance in miles.

    json "{}" | eval d = geodist(37.5665, 126.9780, 35.6762, 139.6503, "mile")
    | # d: 714.9622253741429
    
  3. Calculate the distance in nautical miles.

    json "{}" | eval d = geodist(37.5665, 126.9780, 35.6762, 139.6503, "nmile")
    | # d: 621.2851877065468
    
  4. NULL input

    json "{}" | eval d = geodist(null, 126.9780, 35.6762, 139.6503)
    | # d: null
    

Compatibility

The geodist() function is available since before Sonar 4.0.