Calculate Distance Between Geographical Coordinates

Calculate the distance between two geographical coordinates using the geography:distance() function

Query

logscale
londonLat := 51.507222
| londonLon := -0.1275
| tokyoLat := 35.689722
| tokyoLon := 139.692222
| distance := geography:distance(lat1=londonLat, lon1=londonLon, lat2=tokyoLat, lon2=tokyoLon)

Introduction

The geography:distance() function can be used to calculate the distance between two geographical coordinates (lat1, lon1) and (lat2, lon2) along an ideal earth surface.

.

It uses the Haversine approximation, which is accurate to about 0.5%, to calculate the great-circle distance between the two points. Note that it does not take the altitude into account.

In this example, the geography:distance() function is used to calculate the distance in meters between London and Tokyo using geographical coordinates.

Note that the values of latitude and longitude coordinates must be expressed in decimal degrees.

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0>Augment Data] result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    londonLat := 51.507222
    | londonLon := -0.1275
    | tokyoLat := 35.689722
    | tokyoLon := 139.692222
    | distance := geography:distance(lat1=londonLat, lon1=londonLon, lat2=tokyoLat, lon2=tokyoLon)

    Calculates the distance between two points on Earth (in this example, London and Tokyo) given their latitude and longitude, and returns the result in a field named distance. By default, the distance is returned in meters.

    Using the eval() function, it is possible to convert the result to kilometers for easier interpretation by adding the following to the query: | eval(_distanceKm = distance / 1000). This will return the results in a field named _distanceKm.

  3. Event Result set.

Summary and Results

The query is used to calculate the distance between two geographical coordinates (lat1, lon1) and (lat2, lon2) along an ideal earth surface.

In this example, the parameters lat1 and lon1 are the coordinates of the first point (London), and the parameters lat2 and lon2 are the coordinates of the second point (Tokyo).

Sample output from the incoming example data:

distancelondonLatlondonLontokyoLattokyoLon
9558729.51452489451.507222-0.127535.689722139.692222

Sample output from the incoming example data when converted to kilometers:

londonLatlondonLontokyoLattokyoLon_distanceKm
51.507222-0.127535.689722139.6922229558.73 km

Geographical calculations can be useful in log analysis involving location data, network traffic analysis across global infrastructure, or any scenario where geographical distance is relevant.