You are here

function _weather_data_installation in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather.install \_weather_data_installation()

Helper function for installation and upgrades.

This function inserts data into the weather_places table.

The data file lists one table entry per line. Fields are separated by tab stops. The format is as follows:

GeoID Primary key, string. Currently supported keys are:

  • GeoNames ID, prefixed with geonames_
  • Sentralt stadnamnregister ID, prefixed with ssr_

(tab stop) Latitude -90 .. 90, Positive values for north, negative for south. (tab stop) Longitude -180 .. 180, Positive values for east, negative for west. (tab stop) Country (tab stop) Name (tab stop) Link to yr.no weather forecast (newline)

To save space, the link to the actual yr.no URL has been shortened. Every URL starts with "https://www.yr.no/place/", followed by the country.

In the country's name, every whitespace gets replaced with an underscore, all other characters are kept as they are. The only exception is the last dot in the following country:

"Virgin Islands, U.S." -> "Virgin_Islands,_U.S_"

3 calls to _weather_data_installation()
weather_install in ./weather.install
Implements hook_install().
weather_update_7200 in ./weather.install
Create new database tables for 7.x-2.x branch of the weather module.
weather_update_7209 in ./weather.install
Add 'status' column to table {weather_places} to support custom locations.

File

./weather.install, line 60
Install, update and uninstall functions for the weather module.

Code

function _weather_data_installation() {

  // Delete all original entries from the table, if any.
  db_delete('weather_places')
    ->condition('status', 'original')
    ->execute();

  // Get all remaining entries in the table.
  $changed_geoids = db_query('SELECT geoid FROM {weather_places}')
    ->fetchCol();

  // Prepare the SQL query.
  $query = db_insert('weather_places')
    ->fields(array(
    'geoid',
    'latitude',
    'longitude',
    'country',
    'name',
    'link',
    'status',
  ));

  // Read the data file and append values to query.
  $data = fopen(drupal_get_path('module', 'weather') . '/weather_data.txt', 'r');

  // Use a counter for regular commits, in order to minimize memory usage.
  $count = 0;
  while (!feof($data)) {
    $line = fgets($data);
    $fields = explode("\t", $line);

    // If we get to the last line, there won't be more than one field.
    if (isset($fields[1])) {

      // Check if the geoid has been modified, if so, skip it.
      if (in_array($fields[0], $changed_geoids)) {
        continue;
      }
      $query
        ->values(array(
        'geoid' => $fields[0],
        'latitude' => $fields[1],
        'longitude' => $fields[2],
        'country' => $fields[3],
        'name' => $fields[4],
        'link' => trim($fields[5]),
        'status' => 'original',
      ));
    }
    $count = $count + 1;

    // Execute the query after 500 lines to use less memory.
    if ($count % 500 == 0) {

      // Commit entries to database.
      $query
        ->execute();

      // Prepare the SQL query again.
      $query = db_insert('weather_places')
        ->fields(array(
        'geoid',
        'latitude',
        'longitude',
        'country',
        'name',
        'link',
        'status',
      ));
    }
  }
  fclose($data);
  $query
    ->execute();
}