| 1 | = Determine Cardinal Direction = |
| 2 | |
| 3 | See [http://en.wikipedia.org/wiki/Cardinal_direction Wikipedia] for a description for cardinal direction. |
| 4 | |
| 5 | {{{ |
| 6 | -- Function: ST_Cardinal_Direction(azimuth float8) |
| 7 | -- Returns N, NW, W, SW, S, SE, E, or NE from an azimuth direction |
| 8 | -- By: Mike Toews |
| 9 | -- |
| 10 | -- Usage: |
| 11 | -- postgis=# SELECT ST_Cardinal_Direction( |
| 12 | -- postgis(# ST_Azimuth('POINT(3 5)', 'POINT(5 5)')); |
| 13 | -- st_cardinal_direction |
| 14 | -- ----------------------- |
| 15 | -- E |
| 16 | -- (1 row) |
| 17 | -- |
| 18 | -- DROP FUNCTION ST_Cardinal_Direction(float8); |
| 19 | |
| 20 | CREATE OR REPLACE FUNCTION ST_Cardinal_Direction(azimuth float8) |
| 21 | RETURNS character varying AS |
| 22 | $BODY$SELECT CASE |
| 23 | WHEN $1 < 0.0 THEN 'less than 0' |
| 24 | WHEN $1*180/pi() < 22.5 THEN 'N' |
| 25 | WHEN $1*180/pi() < 67.5 THEN 'NE' |
| 26 | WHEN $1*180/pi() < 112.5 THEN 'E' |
| 27 | WHEN $1*180/pi() < 157.5 THEN 'SE' |
| 28 | WHEN $1*180/pi() < 202.5 THEN 'S' |
| 29 | WHEN $1*180/pi() < 247.5 THEN 'SW' |
| 30 | WHEN $1*180/pi() < 292.5 THEN 'W' |
| 31 | WHEN $1*180/pi() < 337.5 THEN 'NW' |
| 32 | WHEN $1*180/pi() <= 360.0 THEN 'N' |
| 33 | END;$BODY$ LANGUAGE sql IMMUTABLE COST 100; |
| 34 | COMMENT ON FUNCTION ST_Cardinal_Direction(float8) IS 'input azimuth in radians; returns N, NW, W, SW, S, SE, E, or NE'; |
| 35 | }}} |