4         require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
 
   5         ini_set('memory_limit', '800M');
 
   8                 "Create and setup nominatim search system",
 
   9                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
 
  10                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
 
  11                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
 
  13                 array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
 
  14                 array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
 
  16                 array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
 
  18                 array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
 
  19                 array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
 
  20                 array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
 
  21                 array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
 
  22                 array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
 
  23                 array('create-partitions', '', 0, 1, 0, 0, 'bool', 'Create required partition tables and triggers'),
 
  24                 array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
 
  25                 array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
 
  26                 array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
 
  27                 array('create-roads', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
 
  28                 array('osmosis-init', '', 0, 1, 0, 0, 'bool', 'Generate default osmosis configuration'),
 
  29                 array('osmosis-init-date', '', 0, 1, 1, 1, 'string', 'Generate default osmosis configuration'),
 
  30                 array('index', '', 0, 1, 0, 0, 'bool', 'Index the data'),
 
  31                 array('index-output', '', 0, 1, 1, 1, 'string', 'File to dump index information to'),
 
  33         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
 
  35         $bDidSomething = false;
 
  37         // This is a pretty hard core defult - the number of processors in the box - 1
 
  38         $iInstances = isset($aCMDResult['threads'])?$aCMDResult['threads']:(getProcessorCount()-1);
 
  42                 echo "WARNING: resetting threads to $iInstances\n";
 
  44         if ($iInstances > getProcessorCount())
 
  46                 $iInstances = getProcessorCount();
 
  47                 echo "WARNING: resetting threads to $iInstances\n";
 
  49         if (isset($aCMDResult['osm-file']) && !isset($aCMDResult['osmosis-init-date']))
 
  51                 $sBaseFile = basename($aCMDResult['osm-file']);
 
  52                 if (preg_match('#^planet-([0-9]{2})([0-9]{2})([0-9]{2})[.]#', $sBaseFile, $aMatch))
 
  54                         $iTime = mktime(0, 0, 0, $aMatch[2], $aMatch[3], '20'.$aMatch[1]);
 
  56                         $aCMDResult['osmosis-init-date'] = date('Y-m-d', $iTime).'T22:00:00Z';
 
  60         if ($aCMDResult['create-db'] || $aCMDResult['all'])
 
  63                 $bDidSomething = true;
 
  64                 $oDB =& DB::connect(CONST_Database_DSN, false);
 
  65                 if (!PEAR::isError($oDB))
 
  67                         fail('database already exists');
 
  69                 passthru('createdb nominatim');
 
  72         if ($aCMDResult['create-db'] || $aCMDResult['all'])
 
  74                 echo "Create DB (2)\n";
 
  75                 $bDidSomething = true;
 
  76                 // TODO: path detection, detection memory, etc.
 
  79                 passthru('createlang plpgsql nominatim');
 
  80                 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/_int.sql');
 
  81                 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql');
 
  82                 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql');
 
  83                 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql');
 
  84                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
 
  85                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
 
  86                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
 
  87                 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode.sql');
 
  88                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql');
 
  89                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql');
 
  90                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
 
  91                 pgsqlRunScriptFile(CONST_BasePath.'/data/worldboundaries.sql');
 
  94         if ($aCMDResult['import-data'] || $aCMDResult['all'])
 
  97                 $bDidSomething = true;
 
  99                 if (!file_exists(CONST_BasePath.'/osm2pgsql/osm2pgsql')) fail("please download and build osm2pgsql");
 
 100                 passthru(CONST_BasePath.'/osm2pgsql/osm2pgsql -lsc -O gazetteer -C 10000 --hstore -d nominatim '.$aCMDResult['osm-file']);
 
 103                 $x = $oDB->getRow('select * from place limit 1');
 
 104                 if (!$x || PEAR::isError($x)) fail('No Data');
 
 107         if ($aCMDResult['create-functions'] || $aCMDResult['all'])
 
 110                 $bDidSomething = true;
 
 111                 if (!file_exists(CONST_BasePath.'/module/nominatim.so')) fail("nominatim module not built");
 
 112                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
 
 113                 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
 
 114                 pgsqlRunScript($sTemplate);
 
 117         if ($aCMDResult['create-tables'] || $aCMDResult['all'])
 
 120                 $bDidSomething = true;
 
 121                 pgsqlRunScriptFile(CONST_BasePath.'/sql/tables.sql');
 
 123                 // re-run the functions
 
 124                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
 
 125                 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
 
 126                 pgsqlRunScript($sTemplate);
 
 129         if ($aCMDResult['create-partitions'] || $aCMDResult['all'])
 
 132                 $bDidSomething = true;
 
 134                 $sSQL = 'select partition from country_name order by country_code';
 
 135                 $aPartitions = $oDB->getCol($sSQL);
 
 136                 if (PEAR::isError($aPartitions))
 
 138                         fail($aPartitions->getMessage());
 
 142                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partitions.src.sql');
 
 143                 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
 
 144                 foreach($aMatches as $aMatch)
 
 147                         foreach($aPartitions as $sPartitionName)
 
 149                                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
 
 151                         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
 
 154                 pgsqlRunScript($sTemplate);
 
 157         if ($aCMDResult['load-data'] || $aCMDResult['all'])
 
 160                 $bDidSomething = true;
 
 163                 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
 
 165                 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
 
 167                 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
 
 169                 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
 
 171                 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
 
 173                 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
 
 175                 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
 
 177                 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
 
 179                 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
 
 182                 $aDBInstances = array();
 
 183                 for($i = 0; $i < $iInstances; $i++)
 
 185                         $aDBInstances[$i] =& getDB(true);
 
 186                         $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
 
 187                         $sSQL .= 'housenumber, street, isin, postcode, country_code, extratags, ';
 
 188                         $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances.' = '.$i;
 
 189                         if ($aCMDResult['verbose']) echo "$sSQL\n";
 
 190                         if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 196                         for($i = 0; $i < $iInstances; $i++)
 
 198                                 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
 
 206         if ($aCMDResult['create-roads'])
 
 208                 $bDidSomething = true;
 
 211                 $aDBInstances = array();
 
 212                 for($i = 0; $i < $iInstances; $i++)
 
 214                         $aDBInstances[$i] =& getDB(true);
 
 215                         if (!pg_query($aDBInstances[$i]->connection, 'set enable_bitmapscan = off')) fail(pg_last_error($oDB->connection));
 
 216                         $sSQL = 'select count(*) from (select insertLocationRoad(partition, place_id, country_code, geometry) from ';
 
 217                         $sSQL .= 'placex where osm_id % '.$iInstances.' = '.$i.' and rank_search between 26 and 27 and class = \'highway\') as x ';
 
 218                         if ($aCMDResult['verbose']) echo "$sSQL\n";
 
 219                         if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 225                         for($i = 0; $i < $iInstances; $i++)
 
 227                                 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
 
 235         if ($aCMDResult['import-tiger-data'])
 
 237                 $bDidSomething = true;
 
 239                 $aDBInstances = array();
 
 240                 for($i = 0; $i < $iInstances; $i++)
 
 242                         $aDBInstances[$i] =& getDB(true);
 
 245                 foreach(glob(CONST_BasePath.'/data/tiger2009/*.sql') as $sFile)
 
 248                         $hFile = fopen($sFile, "r");
 
 249                         $sSQL = fgets($hFile, 100000);
 
 254                                 for($i = 0; $i < $iInstances; $i++)
 
 256                                         if (!pg_connection_busy($aDBInstances[$i]->connection))
 
 258                                                 while(pg_get_result($aDBInstances[$i]->connection));
 
 259                                                 $sSQL = fgets($hFile, 100000);
 
 261                                                 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 279                                 for($i = 0; $i < $iInstances; $i++)
 
 281                                         if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
 
 289         if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all'])
 
 292                 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
 
 293                 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,country_code,geometry) ";
 
 294                 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,country_code,";
 
 295                 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select country_code,postcode,";
 
 296                 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
 
 297                 $sSQL .= "from placex where postcode is not null group by country_code,postcode) as x";
 
 298                 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 300                 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,country_code,geometry) ";
 
 301                 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
 
 302                 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
 
 303                 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 306         if (($aCMDResult['osmosis-init'] || $aCMDResult['all']) && isset($aCMDResult['osmosis-init-date']))
 
 308                 $bDidSomething = true;
 
 310                 if (!file_exists(CONST_BasePath.'/osmosis-0.38/bin/osmosis')) fail("please download osmosis");
 
 311                 if (file_exists(CONST_BasePath.'/settings/configuration.txt')) echo "settings/configuration.txt already exists\n";
 
 312                 else passthru(CONST_BasePath.'/osmosis-0.38/bin/osmosis --read-replication-interval-init '.CONST_BasePath.'/settings');
 
 314                 $sDate = $aCMDResult['osmosis-init-date'];
 
 315                 $sURL = 'http://toolserver.org/~mazder/replicate-sequences/?'.$sDate;
 
 316                 echo "Getting state file: $sURL\n";
 
 317                 $sStateFile = file_get_contents($sURL);
 
 318                 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
 
 319                 file_put_contents(CONST_BasePath.'/settings/state.txt', $sStateFile);
 
 322         if ($aCMDResult['index'] || $aCMDResult['all'])
 
 324                 $bDidSomething = true;
 
 326                 if (isset($aCMDResult['index-output'])) $sOutputFile = ' -F '.$aCMDResult['index-output'];
 
 327                 passthru(CONST_BasePath.'/nominatim/nominatim -i -d nominatim -t '.$iInstances.$sOutputFile);
 
 332                 showUsage($aCMDOptions, true);
 
 335         function pgsqlRunScriptFile($sFilename)
 
 337                 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
 
 339                 // Convert database DSN to psql paramaters
 
 340                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 341                 $sCMD = 'psql -f '.$sFilename.' '.$aDSNInfo['database'];
 
 343                 $aDescriptors = array(
 
 344                         0 => array('pipe', 'r'),
 
 345                         1 => array('pipe', 'w'),
 
 346                         2 => array('file', '/dev/null', 'a')
 
 349                 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
 
 350                 if (!is_resource($hProcess)) fail('unable to start pgsql');
 
 354                 // TODO: error checking
 
 355                 while(!feof($ahPipes[1]))
 
 357                         echo fread($ahPipes[1], 4096);
 
 361                 proc_close($hProcess);
 
 364         function pgsqlRunScript($sScript)
 
 366                 // Convert database DSN to psql paramaters
 
 367                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 368                 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
 369                 $sCMD = 'psql -p '.$aDSNInfo['port'].' '.$aDSNInfo['database'];
 
 370                 $aDescriptors = array(
 
 371                         0 => array('pipe', 'r'),
 
 372                         1 => array('pipe', 'w'),
 
 373                         2 => array('file', '/dev/null', 'a')
 
 376                 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
 
 377                 if (!is_resource($hProcess)) fail('unable to start pgsql');
 
 379                 fwrite($ahPipes[0], $sScript);
 
 382                 // TODO: error checking
 
 383                 while(!feof($ahPipes[1]))
 
 385                         echo fread($ahPipes[1], 4096);
 
 389                 proc_close($hProcess);