ST_BEARING

Compute the initial great-circle bearing in compass degrees from one point to another on a spherical Earth.

Category: geospatialReturns: DOUBLEDialect: Standard

Syntax

ST_BEARING(lat1, lng1, lat2, lng2)

Description

## Overview Returns the initial great-circle bearing from the starting point to the destination point, measured clockwise from true north on a spherical Earth. The result is in compass degrees in the range [0, 360), where 0 is north, 90 is east, 180 is south, and 270 is west. ST_BEARING is the compass-degree companion to ST_AZIMUTH, which returns the same initial heading in radians. Use ST_BEARING when downstream consumers (dashboards, routing algorithms, map rendering) expect compass degrees, and ST_AZIMUTH when you want raw radians for trigonometric chaining. ## When to use ST_BEARING vs. alternatives - **ST_BEARING** (this function): initial heading at the origin, in compass degrees [0, 360). Best for routing, logistics, and dashboards that already work in degrees. - **ST_AZIMUTH**: same initial heading but in radians. Use when chaining with other trigonometric SQL functions. - **ST_FINAL_BEARING**: heading at the destination, in compass degrees. Differs from ST_BEARING on long paths because great-circle routes curve across the globe. - **ST_DISTANCE_HAVERSINE / ST_DISTANCE_VINCENTY**: complementary distance measures. Pair with ST_BEARING to fully describe a route. ## Behavior - The result is in compass degrees in [0, 360). Multiply by PI/180 to get radians if needed. - Uses a spherical Earth model with mean radius 6,371,008.8 meters, matching ST_DISTANCE_HAVERSINE. - Arguments are ordered (lat, lng), not (lng, lat). - Returns NULL if any argument is NULL. - Returns 0 when the two points are identical because the bearing is undefined. - Handles the antimeridian correctly; longitudes do not need to be pre-normalized. - Bearings converge toward 0 or 180 as paths approach the poles, so expect numerical sensitivity very close to +-90 latitude. ## Accuracy and performance - Spherical accuracy is sufficient for most routing and visualization use cases; accuracy relative to the WGS 84 ellipsoid is within roughly 0.2 degrees of bearing for typical long paths. - Pure arithmetic with a single atan2 plus unit conversion; performance matches ST_DISTANCE_HAVERSINE. - On very long great-circle paths the bearing changes continuously. If you need a bearing at an intermediate waypoint, compute it from that waypoint rather than interpolating. ## Compatibility - Matches the initial-bearing convention used by most WGS 84 SQL geospatial libraries that expose a degree-valued bearing function. - Output range [0, 360) is normalized for direct comparison with compass-degree thresholds.

Parameters

NameTypeDescription
lat1Specifies the WGS 84 latitude of the starting point in decimal degrees, in the range -90 to 90.
lng1Specifies the WGS 84 longitude of the starting point in decimal degrees, in the range -180 to 180.
lat2Specifies the WGS 84 latitude of the destination point in decimal degrees, in the range -90 to 90.
lng2Specifies the WGS 84 longitude of the destination point in decimal degrees, in the range -180 to 180.

Examples

-- Initial great-circle bearing in compass degrees, in [0, 360).
-- 0 is north, 90 is east, 180 is south, 270 is west.
SELECT ST_BEARING(51.5074, -0.1278, 48.8566, 2.3522) AS bearing_deg;
-- A transcontinental flight heading roughly west-southwest.
SELECT ST_BEARING(40.7128, -74.0060, 34.0522, -118.2437) AS bearing_deg;
-- Bucket shipments into cardinal directions using the initial bearing.
SELECT shipment_id,
       CASE
         WHEN ST_BEARING(o_lat, o_lng, d_lat, d_lng) BETWEEN 315 AND 360
           OR ST_BEARING(o_lat, o_lng, d_lat, d_lng) BETWEEN 0 AND 45 THEN 'N'
         WHEN ST_BEARING(o_lat, o_lng, d_lat, d_lng) BETWEEN 45 AND 135 THEN 'E'
         WHEN ST_BEARING(o_lat, o_lng, d_lat, d_lng) BETWEEN 135 AND 225 THEN 'S'
         ELSE 'W'
       END AS heading
FROM logistics.catalog.shipments;
-- Return a compact description of each route.
SELECT route_id,
       ST_DISTANCE_HAVERSINE(o_lat, o_lng, d_lat, d_lng) AS distance_m,
       ST_BEARING(o_lat, o_lng, d_lat, d_lng) AS initial_bearing_deg,
       ST_FINAL_BEARING(o_lat, o_lng, d_lat, d_lng) AS final_bearing_deg
FROM logistics.catalog.routes;
-- Keep flights whose initial bearing is within 15 degrees of due west (270).
SELECT flight_id
FROM aviation.catalog.flights
WHERE ABS(ST_BEARING(o_lat, o_lng, d_lat, d_lng) - 270.0) < 15.0;
-- Multiply by PI/180 to feed into other trigonometric functions.
SELECT ST_BEARING(51.5074, -0.1278, 48.8566, 2.3522) * 3.141592653589793 / 180.0 AS bearing_rad;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →