Last active
October 5, 2022 13:47
-
-
Save geozelot/1d9be0abc3990be8bf34456977359866 to your computer and use it in GitHub Desktop.
PostgreSQL - Get absolute difference between azimuths (angular distance) in degrees depending on direction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- clockwise angular distance in degrees | |
CREATE OR REPLACE FUNCTION CWAngle( | |
IN sdeg FLOAT, | |
IN edeg FLOAT, | |
OUT ddeg FLOAT | |
) LANGUAGE SQL AS | |
$$ | |
SELECT | |
CASE (sdeg <= edeg) | |
WHEN TRUE THEN | |
edeg - sdeg | |
ELSE | |
360.0 - sdeg + edeg | |
END | |
; | |
$$ | |
; | |
-- counterclockwise angular distance in degrees | |
CREATE OR REPLACE FUNCTION CCWAngle( | |
IN sdeg FLOAT, | |
IN edeg FLOAT, | |
OUT ddeg FLOAT | |
) LANGUAGE SQL AS | |
$$ | |
SELECT 360.0 - CWAngle(sdeg, edeg); | |
$$ | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment