You are here

gm3_region.install in Google Maps API V3 7

File

gm3_region/gm3_region.install
View source
<?php

/**
 * FIXME - Add a hook requirements to check for the GEOS extension.
 */

/**
 * Implementation of hook_install().
 * 
 * FIXME - The install should be done as a batch, as the insert takes a long
 * time to process every shape.
 */
function gm3_region_install() {

  // Load the data into the tables.
  // As the files are so huge, we'll read the file line by line and insert
  // each region one by one.  This may be a little slower, but it's less likely
  // to fuck up!
  // Set the maximum memory allocation as this appears to be causing memory
  // issues.

  //ini_set('memory_limit', '512M');
  gm3_load_geophp();
  for ($i = 1; $i <= 4; $i++) {
    $f = fopen(drupal_get_path('module', 'gm3_region') . '/region_data/tdwg_level_' . $i . '_data', 'r');
    $fields = array();
    while ($line = fgets($f)) {
      if (substr($line, 0, 2) == "  ") {
        switch (substr($line, 0, 11)) {
          case '  CONTINENT':
            $fields['continent'] = trim(array_pop(explode("=", $line)));
            break;
          case '  ISO_CODE ':
            $fields['iso_code'] = trim(array_pop(explode("=", $line)));
            break;
          case '  LEVEL1_CO':
          case '  LEVEL_1_C':
            $fields['level_1_code'] = (int) trim(array_pop(explode("=", $line)));
            break;
          case '  LEVEL2_CO':
          case '  LEVEL_2_R':
            $fields['level_2_code'] = (int) trim(array_pop(explode("=", $line)));
            break;
          case '  LEVEL3_CO':
          case '  LEVEL_3_C':
            $fields['level_3_code'] = trim(array_pop(explode("=", $line)));
            break;
          case '  LEVEL_4_C':
            $fields['level_4_code'] = trim(array_pop(explode("=", $line)));
            break;
          case '  LEVEL_4_N':
          case '  LEVEL3_NA':
          case '  LEVEL2_NA':
          case '  LEVEL1_NA':
            $fields['name'] = trim(array_pop(explode("=", $line)));
            break;
          case '  MULTIPOLY':
          case '  POLYGON (':

            // Do the insert
            $fields['polygons'] = trim($line);
            db_insert('gm3_region_data')
              ->fields($fields)
              ->execute();
            $fields = array();
            break;
        }
      }
    }
  }

  // Finally, we set the shape data.  This query is MySQL specific.
  db_query('UPDATE {gm3_region_data} SET mysql_polygons = POLYGONFROMTEXT(polygons)');
}

/**
 * Implementation of hook_schema().
 */
function gm3_region_schema() {
  return array(
    'cache_gm3_polygon' => drupal_get_schema_unprocessed('system', 'cache'),
    'gm3_region_data' => array(
      'description' => 'Stores the Polygon data for TDWG4 Regions',
      'fields' => array(
        'name' => array(
          'description' => "Level name",
          'type' => 'varchar',
          'not null' => TRUE,
          'length' => 64,
        ),
        'continent' => array(
          'description' => "Continent",
          'type' => 'varchar',
          'length' => 20,
          'default' => '',
        ),
        'iso_code' => array(
          'description' => "ISO Code",
          'type' => 'varchar',
          'length' => 2,
          'default' => '',
        ),
        'level_4_code' => array(
          'description' => "Level 4 code",
          'type' => 'varchar',
          'length' => 2,
          'not null' => TRUE,
          'default' => '',
        ),
        'level_3_code' => array(
          'description' => "Level 3 code",
          'type' => 'varchar',
          'length' => 3,
          'not null' => TRUE,
          'default' => '',
        ),
        'level_2_code' => array(
          'description' => "Level 2 id",
          'type' => 'int',
          'size' => 'tiny',
          'not null' => TRUE,
          'default' => 0,
        ),
        'level_1_code' => array(
          'description' => "Level 1 id",
          'type' => 'int',
          'not null' => TRUE,
          'size' => 'tiny',
          'default' => 0,
        ),
        'polygons' => array(
          'description' => "Polygons",
          'type' => 'text',
          'not null' => TRUE,
          'size' => 'medium',
        ),
        'mysql_polygons' => array(
          'description' => 'MySQL GEO extension',
          'mysql_type' => 'MULTIPOLYGON',
        ),
      ),
      'primary key' => array(
        'level_4_code',
        'level_3_code',
        'level_2_code',
        'level_1_code',
      ),
    ),
  );
}

/**
 * Add the mysql_polygons column.
 */
function gm3_region_update_7001() {
  db_add_field('gm3_region_data', 'mysql_polygons', array(
    'description' => 'MySQL GEO extension',
    'mysql_type' => 'MULTIPOLYGON',
  ));

  // Finally, we set the shape data.  This query is MySQL specific.
  db_query('UPDATE {gm3_region_data} SET mysql_polygons = POLYGONFROMTEXT(polygons)');
}

/**
 * Remove the binary_polygons field.
 */
function gm3_region_update_7002() {
  db_drop_field('gm3_region_data', 'binary_polygons');
}

Functions

Namesort descending Description
gm3_region_install Implementation of hook_install().
gm3_region_schema Implementation of hook_schema().
gm3_region_update_7001 Add the mysql_polygons column.
gm3_region_update_7002 Remove the binary_polygons field.