ST_Azimuth — 北を基準とした2点間の線の方位角を返します。
float ST_Azimuth(geometry origin, geometry target);
float ST_Azimuth(geography origin, geography target);
原点から目標点に向いたラジアン単位の方位を返しますが、2点が一致する場合にはNULLを返します。方位角は、Y軸 (ジオメトリ)または子午線北向き (ジオグラフィ)から時計回りに増えていきます。すなわち、北は0、北東はπ/4、東はπ/2、南東は3π/4、南はπ、南西は5π/4、西は3π/2、北西は7π/4、となります。
ジオグラフィでは、方位角計算問題はinverse geodesic problemとして知られます。
方位角は参照ベクトルと一つのポイントとの間の角度と定義される数学的概念で、角度の単位はラジアンです。ラジアン単位の結果はPostgreSQL関数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 ジオグラフィ対応が導入されました。
Enhanced: 2.2.0 - 精度とロバスト性の向上のためにGeographicLibを使って回転楕円体面上での計測を行うようにしています。この新機能を使うには、Proj 4.9.0以上が必要です。
度単位のジオメトリの方位
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
Azimuth from Point(25, 45) to Point(75, 100), shown against the positive Y axis (north).
WITH points AS (
SELECT 'POINT(25 45)'::geometry AS origin,
'POINT(75 100)'::geometry AS target
)
SELECT round(degrees(ST_Azimuth(origin, target))::numeric, 6) AS degrees,
ST_MakeLine(origin, ST_Translate(origin, 0, 105)) AS north,
ST_MakeLine(origin, target) AS direction
FROM points;
42.273689 | LINESTRING(25 45,25 150) | LINESTRING(25 45,75 100)
Reverse azimuth from Point(75, 100) to Point(25, 45).
WITH points AS (
SELECT 'POINT(75 100)'::geometry AS origin,
'POINT(25 45)'::geometry AS target
)
SELECT round(degrees(ST_Azimuth(origin, target))::numeric, 6) AS degrees,
ST_MakeLine(origin, ST_Translate(origin, 0, 90)) AS north,
ST_MakeLine(origin, target) AS direction
FROM points;
222.273689 | LINESTRING(75 100,75 190) | LINESTRING(75 100,25 45)