function weather_get_icao_from_lat_lon in Weather 5.6
Same name and namespace in other branches
- 6.5 weather.module \weather_get_icao_from_lat_lon()
Given a latitude and longitude, return the details of the nearest known weather station.
This (pythagorean) proximity search here is inaccurate - it draws a circle around the base and target point ASSUMING A FLAT MAP GRID. The real maths (below) are probably too heavy for a DB query. So we take the top 5 near guesses, and then do the real calculation on just them. It's pretty extreme cases (like > 6 stations within miles of each other at the pole where that estimate won't be good enough.
Parameters
float Latitude to be searched:
float 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 1434 - 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 *,\n ((%d - latitude) * (%d - latitude) + (%d - longitude) * (%d - longitude))\n AS square_dist_away\n FROM {weather_icao} ORDER BY square_dist_away LIMIT 5";
$result = db_query($sql, $latitude, $latitude, $longitude, $longitude);
$nearest = 9999999;
while ($row = db_fetch_array($result)) {
$distance = earth_distance($longitude, $latitude, $row['longitude'], $row['latitude']);
if ($distance <= $nearest) {
$nearest = $distance;
$row['distance']['kilometers'] = round($distance / 1000, 1);
$row['distance']['miles'] = round($distance / 1609.344, 1);
// Calculate direction
$lat1 = deg2rad($latitude);
$lat2 = deg2rad($row['latitude']);
$dlon = deg2rad($row['longitude'] - $longitude);
$y = sin($dlon) * cos($lat2);
$x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon);
$row['distance']['direction'] = round(rad2deg(atan2($y, $x)));
$station = $row;
}
}
return $station;
}