wiki:UsersWikiCardinalDirection

Version 1 (modified by Mike Taves, 13 years ago) ( diff )

contribute

Determine Cardinal Direction

See Wikipedia for a description for cardinal direction.

-- Function: ST_Cardinal_Direction(azimuth float8)
-- Returns N, NW, W, SW, S, SE, E, or NE from an azimuth direction
-- By: Mike Toews
--
-- Usage:
-- postgis=# SELECT ST_Cardinal_Direction(
-- postgis(#          ST_Azimuth('POINT(3 5)', 'POINT(5 5)'));
--  st_cardinal_direction
-- -----------------------
--  E
-- (1 row)
--
-- DROP FUNCTION ST_Cardinal_Direction(float8);

CREATE OR REPLACE FUNCTION ST_Cardinal_Direction(azimuth float8)
  RETURNS character varying AS
$BODY$SELECT CASE
WHEN $1 < 0.0 THEN 'less than 0'
WHEN $1*180/pi() < 22.5 THEN 'N'
WHEN $1*180/pi() < 67.5 THEN 'NE'
WHEN $1*180/pi() < 112.5 THEN 'E'
WHEN $1*180/pi() < 157.5 THEN 'SE'
WHEN $1*180/pi() < 202.5 THEN 'S'
WHEN $1*180/pi() < 247.5 THEN 'SW'
WHEN $1*180/pi() < 292.5 THEN 'W'
WHEN $1*180/pi() < 337.5 THEN 'NW'
WHEN $1*180/pi() <= 360.0 THEN 'N'
END;$BODY$ LANGUAGE sql IMMUTABLE COST 100;
COMMENT ON FUNCTION ST_Cardinal_Direction(float8) IS 'input azimuth in radians; returns N, NW, W, SW, S, SE, E, or NE';
Note: See TracWiki for help on using the wiki.