You are here

function weather_bearing_to_text in Weather 6.5

Same name and namespace in other branches
  1. 5.6 weather.module \weather_bearing_to_text()
  2. 7.3 weather_theme.inc \weather_bearing_to_text()
  3. 7 weather_theme.inc \weather_bearing_to_text()
  4. 7.2 weather_theme.inc \weather_bearing_to_text()

Converts a compass bearing to a text direction (e.g. 0° North, 86° East, ...)

Parameters

int Compass bearing in degrees:

boolean If true, return abbreviated directions (N, NNW): instead of full text (North, North-Northwest) Defaults to full text directions

Return value

string The translated text direction

2 calls to weather_bearing_to_text()
_weather_format_closest_station in ./weather.module
Convert information about nearest METAR station in the location block
_weather_format_wind in ./weather.module
Convert wind

File

./weather.module, line 1859
Display <acronym title="METeorological Aerodrome Report">METAR</acronym> weather data from anywhere in the world

Code

function weather_bearing_to_text($bearing, $abbreviated = FALSE) {

  // Ensure the bearing to be from 0° to 359°
  while ($bearing < 0) {
    $bearing += 360;
  }
  while ($bearing >= 360) {
    $bearing -= 360;
  }

  // Determine the sector. This works for 0° up to 348.75°
  // If the bearing was greater than 348.75°, perform a wrap (%16)
  $sector = floor(($bearing + 11.25) / 22.5) % 16;
  if (!$abbreviated) {
    $direction = array(
      t('North'),
      t('North-Northeast'),
      t('Northeast'),
      t('East-Northeast'),
      t('East'),
      t('East-Southeast'),
      t('Southeast'),
      t('South-Southeast'),
      t('South'),
      t('South-Southwest'),
      t('Southwest'),
      t('West-Southwest'),
      t('West'),
      t('West-Northwest'),
      t('Northwest'),
      t('North-Northwest'),
    );
  }
  else {
    $direction = array(
      t('N'),
      t('NNE'),
      t('NE'),
      t('ENE'),
      t('E'),
      t('ESE'),
      t('SE'),
      t('SSE'),
      t('S'),
      t('SSW'),
      t('SW'),
      t('WSW'),
      t('W'),
      t('WNW'),
      t('NW'),
      t('NNW'),
    );
  }
  return $direction[$sector];
}