View source
<?php
function gm3_region_install() {
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 (':
$fields['polygons'] = trim($line);
db_insert('gm3_region_data')
->fields($fields)
->execute();
$fields = array();
break;
}
}
}
}
db_query('UPDATE {gm3_region_data} SET mysql_polygons = POLYGONFROMTEXT(polygons)');
}
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',
),
),
);
}
function gm3_region_update_7001() {
db_add_field('gm3_region_data', 'mysql_polygons', array(
'description' => 'MySQL GEO extension',
'mysql_type' => 'MULTIPOLYGON',
));
db_query('UPDATE {gm3_region_data} SET mysql_polygons = POLYGONFROMTEXT(polygons)');
}
function gm3_region_update_7002() {
db_drop_field('gm3_region_data', 'binary_polygons');
}