Skip to content

Instantly share code, notes, and snippets.

@geozelot
Last active October 5, 2022 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geozelot/1d9be0abc3990be8bf34456977359866 to your computer and use it in GitHub Desktop.
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
-- 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