You are here

function _weather_update_places in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather_parser.inc \_weather_update_places()

Handle updates to the weather_places table.

1 call to _weather_update_places()
_weather_parse_forecast in ./weather_parser.inc
Parses an XML forecast supplied by yr.no.

File

./weather_parser.inc, line 249
Retrieves and parses raw METAR data and stores result in database.

Code

function _weather_update_places($fc) {
  module_load_include('inc', 'weather', 'weather.common');

  // Extract GeoID and latitude/longitude of returned XML data.
  // This might differ from the data we have in the database. An example
  // was Heraklion (ID 261745), which got the forecast for
  // Nomós Irakleíou (ID 261741).
  // Data to extract are:
  // geoid, latitude, longitude, country, name.
  $place['geoid'] = $fc->location->location['geobase'] . "_" . $fc->location->location['geobaseid'];
  $place['latitude'] = (string) $fc->location->location['latitude'];
  $place['latitude'] = round($place['latitude'], 5);
  $place['longitude'] = (string) $fc->location->location['longitude'];
  $place['longitude'] = round($place['longitude'], 5);
  $place['country'] = (string) $fc->location->country;
  $place['name'] = (string) $fc->location->name;
  $url = (string) $fc->credit->link['url'];

  // Remove "https://www.yr.no/place/" from the URL.
  $link = substr($url, 24);

  // Split by slashes and remove country (first item) and "forecast.xml" (last item)
  $link_parts = explode('/', $link);

  // Remove country.
  array_shift($link_parts);

  // Remove "forecast.xml".
  array_pop($link_parts);
  $place['link'] = implode('/', $link_parts);

  // Fetch stored information about geoid.
  $info = weather_get_information_about_geoid($place['geoid']);

  // If the geoid is not in the database, add it.
  if ($info === FALSE) {
    $place['status'] = 'added';
    db_insert('weather_places')
      ->fields($place)
      ->execute();
  }
  else {

    // Compare the stored information with the downloaded information.
    // If they differ, update the database.
    $stored_info = (array) $info;
    unset($stored_info['status']);
    $diff = array_diff_assoc($stored_info, $place);
    if (!empty($diff)) {
      $place['status'] = 'modified';
      db_update('weather_places')
        ->condition('geoid', $place['geoid'])
        ->fields($place)
        ->execute();
    }
  }
}