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('all', '', 0, 1, 1, 1, 'realpath', 'Do the complete process'),
15 array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
16 array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
17 array('import-data', '', 0, 1, 1, 1, 'realpath', 'Import a osm file'),
18 array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
19 array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
20 array('create-partitions', '', 0, 1, 0, 0, 'bool', 'Create required partition tables and triggers'),
21 array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
23 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
25 $bDidSomething = false;
27 if ($aCMDResult['create-db'] || isset($aCMDResult['all']))
29 $bDidSomething = true;
30 $oDB =& DB::connect(CONST_Database_DSN, false);
31 if (!PEAR::isError($oDB))
33 fail('database already exists');
35 passthru('createdb nominatim');
38 if ($aCMDResult['create-db'] || isset($aCMDResult['all']))
40 $bDidSomething = true;
41 // TODO: path detection, detection memory, etc.
44 passthru('createlang plpgsql nominatim');
45 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/_int.sql');
46 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql');
47 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql');
48 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql');
49 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
50 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
51 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode.sql');
52 pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql');
53 pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql');
54 pgsqlRunScriptFile(CONST_BasePath.'/data/worldboundaries.sql');
57 if (isset($aCMDResult['all']) && !isset($aCMDResult['import-data'])) $aCMDResult['import-data'] = $aCMDResult['all'];
58 if (isset($aCMDResult['import-data']) && $aCMDResult['import-data'])
60 $bDidSomething = true;
61 passthru(CONST_BasePath.'/osm2pgsql/osm2pgsql -lsc -O gazetteer -C 10000 --hstore -d nominatim '.$aCMDResult['import-data']);
64 if ($aCMDResult['create-functions'] || isset($aCMDResult['all']))
66 $bDidSomething = true;
67 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
68 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
69 pgsqlRunScript($sTemplate);
72 if ($aCMDResult['create-tables'] || isset($aCMDResult['all']))
74 $bDidSomething = true;
75 pgsqlRunScriptFile(CONST_BasePath.'/sql/tables.sql');
77 // re-run the functions
78 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
79 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
80 pgsqlRunScript($sTemplate);
83 if ($aCMDResult['create-partitions'] || isset($aCMDResult['all']))
85 $bDidSomething = true;
87 $sSQL = 'select distinct country_code from country_name order by country_code';
88 $aPartitions = $oDB->getCol($sSQL);
89 if (PEAR::isError($aPartitions))
91 fail($aPartitions->getMessage());
93 $aPartitions[] = 'none';
95 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partitions.src.sql');
96 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
97 foreach($aMatches as $aMatch)
100 foreach($aPartitions as $sPartitionName)
102 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
104 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
106 pgsqlRunScript($sTemplate);
109 if ($aCMDResult['load-data'] || isset($aCMDResult['all']))
111 $bDidSomething = true;
114 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
116 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
118 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
120 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
122 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
126 $aDBInstances = array();
127 for($i = 0; $i < $iInstances; $i++)
129 $aDBInstances[$i] =& getDB(true);
130 $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
131 $sSQL .= 'housenumber, street, isin, postcode, country_code, extratags, ';
132 $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances.' = '.$i;
133 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
139 for($i = 0; $i < $iInstances; $i++)
141 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
151 showUsage($aCMDOptions, true);
154 function pgsqlRunScriptFile($sFilename)
156 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
158 // Convert database DSN to psql paramaters
159 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
160 $sCMD = 'psql -f '.$sFilename.' '.$aDSNInfo['database'];
162 $aDescriptors = array(
163 0 => array('pipe', 'r'),
164 1 => array('pipe', 'w'),
165 2 => array('file', '/dev/null', 'a')
168 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
169 if (!is_resource($hProcess)) fail('unable to start pgsql');
173 // TODO: error checking
174 while(!feof($ahPipes[1]))
176 echo fread($ahPipes[1], 4096);
180 proc_close($hProcess);
183 function pgsqlRunScript($sScript)
185 // Convert database DSN to psql paramaters
186 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
187 $sCMD = 'psql '.$aDSNInfo['database'];
189 $aDescriptors = array(
190 0 => array('pipe', 'r'),
191 1 => array('pipe', 'w'),
192 2 => array('file', '/dev/null', 'a')
195 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
196 if (!is_resource($hProcess)) fail('unable to start pgsql');
198 fwrite($ahPipes[0], $sScript);
201 // TODO: error checking
202 while(!feof($ahPipes[1]))
204 echo fread($ahPipes[1], 4096);
208 proc_close($hProcess);