Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Return Compass Ordinal Abbreviation From A Direction

This code will return a compass Intercardinal (or ordinal) direction based on a numeric direction. It supports the article here on returning a weather forecast with OpenWeatherMap. In some cases the cardinal directions or N, S, E, and W are suitable. Other times the 16 Intercardinal (NNE, NE, ENE etc.) are appropriate. We're returning a more full set of 32 secondary-intercardinal directions for the sake of accuracy.

By at least the middle of the eighteenth century the 32-point system had been further extended with the use of half- and quarter-points to give a total of 128 directions - this is well outside the comprehension of most people so we've avoided its use.

32 Point Compass Rose

The 32 points on the compass rose are returned in our PHP code based upon the values from the following table on Wikipedia.

32 Points of a Compass

PHP Code

When referenced by any of our shortcodes, the following should be copied into your custom functions file along with the shortcode function.

1
<?php
/*
Return Compass Ordinal Abbreviation From A Direction
http://www.beliefmedia.com/code/compass-cardinal-direction
http://www.beliefmedia.com/openweathermap-forecasts
*/

function beliefmedia_compass_cardinal($dir) {

$dir = abs($dir);

switch ($dir) {
case ($dir >= 354 && $dir < 6): $cardinal = 'N'; break; case ($dir >= 6 && $dir < 17): $cardinal = 'NbE'; break; case ($dir >= 17 && $dir < 28): $cardinal = 'NNE'; break; case ($dir >= 28 && $dir < 39): $cardinal = 'NEbN'; break; case ($dir >= 39 && $dir < 51): $cardinal = 'NE'; break; case ($dir >= 51 && $dir < 62): $cardinal = 'NEbE'; break; case ($dir >= 62 && $dir < 73): $cardinal = 'ENE'; break; case ($dir >= 73 && $dir < 84): $cardinal = 'EbN'; break; case ($dir >= 84 && $dir < 96): $cardinal = 'E'; break; case ($dir >= 96 && $dir < 107): $cardinal = 'EbS'; break; case ($dir >= 107 && $dir < 118): $cardinal = 'ESE'; break; case ($dir >= 118 && $dir < 129): $cardinal = 'SEbE'; break; case ($dir >= 129 && $dir < 141): $cardinal = 'SE'; break; case ($dir >= 141 && $dir < 152): $cardinal = 'SEbS'; break; case ($dir >= 152 && $dir < 163): $cardinal = 'SSE'; break; case ($dir >= 163 && $dir < 174): $cardinal = 'SbE'; break; case ($dir >= 174 && $dir < 186): $cardinal = 'S'; break; case ($dir >= 186 && $dir < 197): $cardinal = 'SbW'; break; case ($dir >= 197 && $dir < 208): $cardinal = 'SSW'; break; case ($dir >= 208 && $dir < 219): $cardinal = 'SWbS'; break; case ($dir >= 219 && $dir < 231): $cardinal = 'SW'; break; case ($dir >= 231 && $dir < 242): $cardinal = 'SWbW'; break; case ($dir >= 242 && $dir < 253): $cardinal = 'WSW'; break; case ($dir >= 253 && $dir < 264): $cardinal = 'WbS'; break; case ($dir >= 264 && $dir < 276): $cardinal = 'W'; break; case ($dir >= 276 && $dir < 287): $cardinal = 'WbN'; break; case ($dir >= 287 && $dir < 298): $cardinal = 'WNW'; break; case ($dir >= 298 && $dir < 309): $cardinal = 'NWbW'; break; case ($dir >= 309 && $dir < 321): $cardinal = 'NW'; break; case ($dir >= 321 && $dir < 332): $cardinal = 'NWbN'; break; case ($dir >= 332 && $dir < 343): $cardinal = 'NNW'; break; case ($dir >= 343 && $dir < 354): $cardinal = 'NbW'; break; } return $cardinal; }[/php] We'll likely update this post soon with a function to return just the 16-point Intercardinals. The following function returns the 16-point cardinal.

1
<?php
/*
Return Compass Ordinal Abbreviation From A Direction
http://www.beliefmedia.com/code/compass-cardinal-direction
http://www.beliefmedia.com/openweathermap-forecasts
*/

function beliefmedia_compass16_cardinal($deg) {

$cardinalDirections = array(
'N' => array(348.75, 360),
'N2' => array(0, 11.25),
'NNE' => array(11.25, 33.75),
'NE' => array(33.75, 56.25),
'ENE' => array(56.25, 78.75),
'E' => array(78.75, 101.25),
'ESE' => array(101.25, 123.75),
'SE' => array(123.75, 146.25),
'SSE' => array(146.25, 168.75),
'S' => array(168.75, 191.25),
'SSW' => array(191.25, 213.75),
'SW' => array(213.75, 236.25),
'WSW' => array(236.25, 258.75),
'W' => array(258.75, 281.25),
'WNW' => array(281.25, 303.75),
'NW' => array(303.75, 326.25),
'NNW' => array(326.25, 348.75)
);

foreach ($cardinalDirections as $dir => $angles) {
if ($deg >= $angles[0] && $deg < $angles[1]) { $cardinal = str_replace('2', '', $dir); } } return $cardinal; }[/php]

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment