Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#4840 closed defect (fixed)

ST_Azimuth with geography provides wrong results

Reported by: postlude Owned by: pramsey
Priority: medium Milestone: PostGIS 3.1.2
Component: postgis Version: 3.1.x
Keywords: Cc:

Description

Hi, I noticed that the azimuth angles reported by ST_Azimuth are obviously wrong in some cases.

The following simple example demonstrates this. The central point C is located at 5°E,55°N. All other points are shifted by 1° in the respective directions:

SELECT 
degrees(ST_azimuth(C,N))  AS az_n,
degrees(ST_azimuth(C,NE)) AS az_ne,
degrees(ST_azimuth(C,E))  AS az_e,
degrees(ST_azimuth(C,SE)) AS az_se,
degrees(ST_azimuth(C,S))  AS az_s,
degrees(ST_azimuth(C,SW)) AS az_sw,
degrees(ST_azimuth(C,W))  AS az_w,
degrees(ST_azimuth(C,NW)) AS az_nw
FROM (SELECT 
ST_GeogFromText('SRID=4326;POINT(5 55)') AS C,
ST_GeogFromText('SRID=4326;POINT(5 56)') AS N,
ST_GeogFromText('SRID=4326;POINT(6 56)') AS NE,
ST_GeogFromText('SRID=4326;POINT(6 55)') AS E,
ST_GeogFromText('SRID=4326;POINT(6 54)') AS SE,
ST_GeogFromText('SRID=4326;POINT(5 54)') AS S,
ST_GeogFromText('SRID=4326;POINT(4 54)') AS SW,
ST_GeogFromText('SRID=4326;POINT(4 55)') AS W,
ST_GeogFromText('SRID=4326;POINT(4 56)') AS NW ) points

The result is (I manually removed some decimals for easier readability)

 az_n | az_ne | az_e  | az_se  | az_s | az_sw  | az_w   | az_nw 
------+-------+-------+--------+------+--------+--------+-------
  180 | 29.16 | 89.59 | 149.39 |  360 | 329.39 | 269.59 | 209.16

This is not what I expect. I am well aware that a difference of 1° in N and E does not result in an azimuth of 45° due to the convergence of the meridians. The azimuth angles az_ne, az_e, az_se and az_w seem correct (I checked these angles in a conformal map projection in QGIS). However, what I expect is

 az_n | az_ne | az_e  | az_se  | az_s | az_sw  | az_w   | az_nw 
------+-------+-------+--------+------+--------+--------+-------
  0 ! | 29.16 | 89.59 | 149.39 | 180 !|  211 ! | 269.59 | 331 !

What was going wrong for az_n, az_s, az_sw and az_nw?

The version of my DB is

                                                             version                                                             |            postgis_version            
---------------------------------------------------------------------------------------------------------------------------------+---------------------------------------
 PostgreSQL 13.1 (Ubuntu 13.1-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit | 3.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1

installed via the apt.postgresql.org repositories for Ubuntu 18.04.

Change History (3)

comment:1 Changed 4 months ago by pramsey

Something has changed in 3.1 and up, I get results as expected in 3.0 (modulo negative angles)

az_n  | 0
az_ne | 29.169426168649625
az_e  | 89.59042054976433
az_se | 149.39160891085518
az_s  | 180
az_sw | -149.39160891085518
az_w  | -89.59042054976433
az_nw | -29.169426168649625

Come to think of it, the wrong results in 3.1+ may well be a result of a bad fix trying address those negative angles. (Mine are even worse that yours?)

az_n  | 180
az_ne | 29.169426168649625
az_e  | 89.59042054976433
az_se | 149.39160891085518
az_s  | 180
az_sw | 329.39160891085515
az_w  | 269.5904205497643
az_nw | 209.16942616864964

comment:2 Changed 4 months ago by Paul Ramsey <pramsey@…>

Resolution: fixed
Status: newclosed

In a97223f/git:

Mistake in converting negative azimuth values to positive
results in incorrect azimuths in geography.
Closes #4840

comment:3 Changed 4 months ago by Paul Ramsey <pramsey@…>

In 4ba2883a/git:

Mistake in converting negative azimuth values to positive
results in incorrect azimuths in geography.
Closes #4840

Note: See TracTickets for help on using tickets.