ST_Azimuth — Returns the north-based azimuth of a line between two points.
float ST_Azimuth(geometry origin, geometry target);
float ST_Azimuth(geography origin, geography target);
Returns the azimuth in radians of the target point from the origin point, or NULL if the two points are coincident. The azimuth angle is a positive clockwise angle referenced from the positive Y axis (geometry) or the North meridian (geography): North = 0; Northeast = π/4; East = π/2; Southeast = 3π/4; South = π; Southwest 5π/4; West = 3π/2; Northwest = 7π/4.
For the geography type, the azimuth solution is known as the inverse geodesic problem.
The azimuth is a mathematical concept defined as the angle between a reference vector and a point, with angular units in radians.
The result value in radians can be converted to degrees using the PostgreSQL function degrees().
For offsetting lines relative to their direction, use ST_OffsetCurve. For projecting a point by distance and bearing, use ST_Project.
Availability: 1.1.0
Enhanced: 2.0.0 support for geography was introduced.
Enhanced: 2.2.0 measurement on spheroid performed with GeographicLib for improved accuracy and robustness. Requires PROJ >= 4.9.0 to take advantage of the new feature.
Geometry Azimuth in degrees
SELECT degrees(ST_Azimuth(ST_Point(25, 45), ST_Point(75, 100))) AS degA_B,
degrees(ST_Azimuth(ST_Point(75, 100), ST_Point(25, 45) )) AS degB_A;
dega_b | degb_a ------------------+------------------ 42.27368900609371 | 222.27368900609372
Compass direction labels can be computed from the azimuth.
WITH directions(deg) AS (VALUES
(0), (45), (90), (135), (180), (225), (270), (315)
),
bearings AS (
SELECT deg,
degrees(ST_Azimuth(ST_Point(0, 0),
ST_MakePoint(sin(radians(deg)),
cos(radians(deg))))) AS azimuth
FROM directions
)
SELECT deg,
round(azimuth::numeric, 1) AS azimuth,
(ARRAY['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'])
[floor((azimuth + 22.5) / 45)::int + 1] AS direction
FROM bearings
ORDER BY deg;
deg | azimuth | direction -----+---------+----------- 0 | 0.0 | N 45 | 45.0 | NE 90 | 90.0 | E 135 | 135.0 | SE 180 | 180.0 | S 225 | 225.0 | SW 270 | 270.0 | W 315 | 315.0 | NW
Blue: origin Point(25,45); Green: target Point(75, 100); Yellow: Y axis or North; Red: azimuth angle.
|
Blue: origin Point(75, 100); Green: target Point(25, 45); Yellow: Y axis or North; Red: azimuth angle.
|
ST_Angle, ST_Point, ST_Translate, ST_Project, PostgreSQL Math Functions