You are here

function weather_get_icao_from_lat_lon in Weather 6.5

Same name and namespace in other branches
  1. 5.6 weather.module \weather_get_icao_from_lat_lon()

Return ICAO code of the nearest weather station.

The distance calculation is based on the spherical law of cosines. The bearing is converted from radians to degress and normalized to be between 0 and 360 degress. The returned value will range from -180° to 180°. All angles must be passed in radians for the trigonometry functions.

R = Earth's radius (using a mean radius of 6371 km) distance = R * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long2 - long1)) bearing = atan2(sin(long2 - long1) * cos(lat2), cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(long2- long1))

Parameters

Latitude to be searched.:

Longitude to be searched.:

Return value

array METAR station with two additional fields (distance and direction)

1 call to weather_get_icao_from_lat_lon()
weather_block in ./weather.module
Generate HTML for the weather block

File

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

Code

function weather_get_icao_from_lat_lon($latitude, $longitude) {
  $sql = "SELECT icao, name,\n    ROUND(6371 *\n      ACOS(\n        SIN(RADIANS(%f)) * SIN(RADIANS(latitude)) +\n        COS(RADIANS(%f)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude - %f))\n      ),\n    1) AS distance_in_km,\n    MOD(\n      ROUND(\n        DEGREES(\n          ATAN2(\n            SIN(RADIANS(longitude - %f)) * COS(RADIANS(latitude)),\n            COS(RADIANS(%f)) * SIN(RADIANS(latitude)) - SIN(RADIANS(%f)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude - %f))\n          )\n        )\n      ) + 360, 360\n    ) AS bearing\n    FROM {weather_icao} ORDER BY distance_in_km";
  $result = db_query($sql, array(
    $latitude,
    $latitude,
    $longitude,
    $longitude,
    $latitude,
    $latitude,
    $longitude,
  ));
  $station = db_fetch_array($result);
  $station['distance']['kilometers'] = $station['distance_in_km'];
  $station['distance']['miles'] = round($station['distance_in_km'] / 1.609344, 1);
  $station['distance']['direction'] = $station['bearing'];
  return $station;
}