Skip to content

Geolocation Functions

Geographic distance, bearing, and geohash functions.

Summary

Function Signature Description
geo_bearing number, number, number, number -> number Bearing between coordinates
geo_bounding_box array -> object Bounding box from an array of [lat, lon] points
geo_distance number, number, number, number -> number Haversine distance in meters
geo_distance_km number, number, number, number -> number Haversine distance in kilometers
geo_distance_miles number, number, number, number -> number Haversine distance in miles
geo_in_bbox number, number, number, number, number, number -> boolean Check if a point is inside a bounding box
geo_in_radius number, number, number, number, number -> boolean Check if a point is within a radius (km) of a center point
geo_midpoint array -> array Geographic midpoint of an array of [lat, lon] points
geohash_decode string -> object Decode a geohash string to
geohash_encode number, number[, number] -> string Encode lat/lon as a geohash string with optional precision (default 12)

Functions

geo_bearing

Bearing between coordinates

Signature: number, number, number, number -> number

Examples:

# NYC to London
geo_bearing(`40.7128`, `-74.0060`, `51.5074`, `-0.1278`) -> 51.2
# Due east
geo_bearing(`0`, `0`, `0`, `90`) -> 90.0
# Due north
geo_bearing(`0`, `0`, `90`, `0`) -> 0.0

geo_bounding_box

Bounding box from an array of [lat, lon] points

Signature: array -> object

Examples:

# NYC and LA bounding box
geo_bounding_box(`[[40.7, -74.0], [34.0, -118.2]]`) -> {"max_lat": 40.7, "max_lon": -74.0, "min_lat": 34.0, "min_lon": -118.2}

geo_distance

Haversine distance in meters

Signature: number, number, number, number -> number

Examples:

# NYC to London
geo_distance(`40.7128`, `-74.0060`, `51.5074`, `-0.1278`) -> 5570222
# LA to SF
geo_distance(`34.0522`, `-118.2437`, `37.7749`, `-122.4194`) -> 559044
# Same point
geo_distance(`0`, `0`, `0`, `0`) -> 0

geo_distance_km

Haversine distance in kilometers

Signature: number, number, number, number -> number

Examples:

# NYC to London
geo_distance_km(`40.7128`, `-74.0060`, `51.5074`, `-0.1278`) -> 5570.2
# LA to SF
geo_distance_km(`34.0522`, `-118.2437`, `37.7749`, `-122.4194`) -> 559.0
# Paris to London
geo_distance_km(`48.8566`, `2.3522`, `51.5074`, `-0.1278`) -> 343.5

geo_distance_miles

Haversine distance in miles

Signature: number, number, number, number -> number

Examples:

# NYC to London
geo_distance_miles(`40.7128`, `-74.0060`, `51.5074`, `-0.1278`) -> 3461.0
# LA to SF
geo_distance_miles(`34.0522`, `-118.2437`, `37.7749`, `-122.4194`) -> 347.4
# Paris to London
geo_distance_miles(`48.8566`, `2.3522`, `51.5074`, `-0.1278`) -> 213.4

geo_in_bbox

Check if a point is inside a bounding box

Signature: number, number, number, number, number, number -> boolean

Examples:

# Point inside box
geo_in_bbox(`40.0`, `-75.0`, `39.0`, `41.0`, `-76.0`, `-74.0`) -> true
# Point outside box
geo_in_bbox(`42.0`, `-75.0`, `39.0`, `41.0`, `-76.0`, `-74.0`) -> false

geo_in_radius

Check if a point is within a radius (km) of a center point

Signature: number, number, number, number, number -> boolean

Examples:

# Times Square within 2km of Empire State
geo_in_radius(`40.758`, `-73.985`, `40.748`, `-73.986`, `2`) -> true
# LA not within 100km of NYC
geo_in_radius(`34.052`, `-118.244`, `40.713`, `-74.006`, `100`) -> false

geo_midpoint

Geographic midpoint of an array of [lat, lon] points

Signature: array -> array

Examples:

# Midpoint on equator
geo_midpoint(`[[0, 0], [0, 90]]`) -> [0.0, 45.0]

geohash_decode

Decode a geohash string to {lat, lon}

Signature: string -> object

Examples:

# Decode NYC geohash
geohash_decode('dr5ru7') -> {"lat": 40.71, "lon": -73.99}

geohash_encode

Encode lat/lon as a geohash string with optional precision (default 12)

Signature: number, number[, number] -> string

Examples:

# Encode NYC coordinates
geohash_encode(`40.7128`, `-74.0060`) -> "dr5ru6j1yz56"
# Encode with precision 5
geohash_encode(`40.7128`, `-74.0060`, `5`) -> "dr5ru"