You are here

function gm3_region_install in Google Maps API V3 7

Implementation of hook_install().

FIXME - The install should be done as a batch, as the insert takes a long time to process every shape.

File

gm3_region/gm3_region.install, line 12

Code

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)');
}