5 protected $iCacheMemory; // set in constructor
\r
6 protected $iInstances; // set in constructor
\r
7 protected $sModulePath; // set in constructor
\r
8 protected $aDSNInfo; // set in constructor = DB::parseDSN(CONST_Database_DSN);
\r
9 protected $sVerbose; // set in constructor
\r
10 protected $sIgnoreErrors; // set in constructor
\r
11 protected $bEnableDiffUpdates; // set in constructor
\r
12 protected $bEnableDebugStatements; // set in constructor
\r
13 protected $bNoPartitions; // set in constructor
\r
14 protected $oDB = null; // set in setupDB (earliest) or later in loadData, importData, drop, createSqlFunctions, importTigerData
\r
15 // pgsqlRunPartitionScript, calculatePostcodes, ..if no already set
\r
17 public function __construct($aCMDResult) {
\r
18 // by default, use all but one processor, but never more than 15.
\r
19 $this->iInstances = isset($aCMDResult['threads'])
\r
20 ? $aCMDResult['threads']
\r
21 : (min(16, getProcessorCount()) - 1);
\r
24 if ($this->iInstances < 1) {
\r
25 $this->iInstances = 1;
\r
26 warn('resetting threads to '.$this->iInstances);
\r
29 // Assume we can steal all the cache memory in the box (unless told otherwise)
\r
30 if (isset($aCMDResult['osm2pgsql-cache'])) {
\r
31 $this->iCacheMemory = $aCMDResult['osm2pgsql-cache'];
\r
33 $this->iCacheMemory = getCacheMemoryMB();
\r
36 $this->sModulePath = CONST_Database_Module_Path;
\r
37 info('module path: ' . $this->sModulePath);
\r
39 // prepares DB for import or update, sets the Data Source Name
\r
40 $this->aDSNInfo = DB::parseDSN(CONST_Database_DSN);
\r
41 if (!isset($this->aDSNInfo['port']) || !$this->aDSNInfo['port']) $this->aDSNInfo['port'] = 5432;
\r
43 // setting member variables based on command line options stored in $aCMDResult
\r
44 $this->sVerbose = $aCMDResult['verbose'];
\r
45 $this->sIgnoreErrors = $aCMDResult['ignore-errors'];
\r
46 $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
\r
47 $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
\r
48 $this->bNoPartitions = $aCMDResult['no-partitions'];
\r
51 public function createDB()
\r
54 $sDB = DB::connect(CONST_Database_DSN, false);
\r
55 if (!PEAR::isError($sDB)) {
\r
56 fail('database already exists ('.CONST_Database_DSN.')');
\r
59 $sCreateDBCmd = 'createdb -E UTF-8 -p '.$this->aDSNInfo['port'].' '.$this->aDSNInfo['database'];
\r
60 if (isset($this->aDSNInfo['username']) && $this->aDSNInfo['username']) {
\r
61 $sCreateDBCmd .= ' -U ' . $this->aDSNInfo['username'];
\r
64 if (isset($this->aDSNInfo['hostspec']) && $this->aDSNInfo['hostspec']) {
\r
65 $sCreateDBCmd .= ' -h ' . $this->aDSNInfo['hostspec'];
\r
69 if (isset($this->aDSNInfo['password']) && $this->aDSNInfo['password']) {
\r
70 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
\r
73 $result = runWithEnv($sCreateDBCmd, $aProcEnv);
\r
74 if ($result != 0) fail('Error executing external command: '.$sCreateDBCmd);
\r
77 public function setupDB()
\r
80 $this->oDB =& getDB();
\r
82 $fPostgresVersion = getPostgresVersion($this->oDB);
\r
83 echo 'Postgres version found: '.$fPostgresVersion."\n";
\r
85 if ($fPostgresVersion < 9.1) {
\r
86 fail('Minimum supported version of Postgresql is 9.1.');
\r
89 $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
\r
90 $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
\r
92 // For extratags and namedetails the hstore_to_json converter is
\r
93 // needed which is only available from Postgresql 9.3+. For older
\r
94 // versions add a dummy function that returns nothing.
\r
95 $iNumFunc = chksql($this->oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"));
\r
97 if ($iNumFunc == 0) {
\r
98 $this->pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable");
\r
99 warn('Postgresql is too old. extratags and namedetails API not available.');
\r
103 $fPostgisVersion = getPostgisVersion($this->oDB);
\r
104 echo 'Postgis version found: '.$fPostgisVersion."\n";
\r
106 if ($fPostgisVersion < 2.1) {
\r
107 // Functions were renamed in 2.1 and throw an annoying deprecation warning
\r
108 $this->pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
\r
109 $this->pgsqlRunScript('ALTER FUNCTION ST_Line_Locate_Point(geometry, geometry) RENAME TO ST_LineLocatePoint');
\r
111 if ($fPostgisVersion < 2.2) {
\r
112 $this->pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, geometry, spheroid) RENAME TO ST_DistanceSpheroid');
\r
115 $i = chksql($this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'"));
\r
117 echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
\r
118 echo "\n createuser ".CONST_Database_Web_User."\n\n";
\r
122 if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
\r
123 echo 'Error: you need to download the country_osm_grid first:';
\r
124 echo "\n wget -O ".CONST_ExtraDataPath."/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz\n";
\r
127 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
\r
128 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
\r
129 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql.gz');
\r
130 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
\r
133 if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz')) {
\r
134 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
\r
136 warn('external UK postcode table not found.');
\r
139 if (CONST_Use_Extra_US_Postcodes) {
\r
140 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
\r
143 if ($this->bNoPartitions) {
\r
144 $this->pgsqlRunScript('update country_name set partition = 0');
\r
147 // the following will be needed by create_functions later but
\r
148 // is only defined in the subsequently called T
\r
149 // Create dummies here that will be overwritten by the proper
\r
150 // versions in create-tables.
\r
151 $this->pgsqlRunScript('CREATE TABLE IF NOT EXISTS place_boundingbox ()');
\r
152 $this->pgsqlRunScript('CREATE TYPE wikipedia_article_match AS ()', false);
\r
155 public function importData($sOSMFile)
\r
157 info('Import data');
\r
159 $osm2pgsql = CONST_Osm2pgsql_Binary;
\r
160 if (!file_exists($osm2pgsql)) {
\r
161 echo "Check CONST_Osm2pgsql_Binary in your local settings file.\n";
\r
162 echo "Normally you should not need to set this manually.\n";
\r
163 fail("osm2pgsql not found in '$osm2pgsql'");
\r
168 if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
\r
169 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
\r
172 if (CONST_Tablespace_Osm2pgsql_Data)
\r
173 $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
\r
174 if (CONST_Tablespace_Osm2pgsql_Index)
\r
175 $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
\r
176 if (CONST_Tablespace_Place_Data)
\r
177 $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
\r
178 if (CONST_Tablespace_Place_Index)
\r
179 $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
\r
180 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
\r
181 $osm2pgsql .= ' -C '.$this->iCacheMemory;
\r
182 $osm2pgsql .= ' -P '.$this->aDSNInfo['port'];
\r
183 if (isset($this->aDSNInfo['username']) && $this->aDSNInfo['username']) {
\r
184 $osm2pgsql .= ' -U ' . $this->aDSNInfo['username'];
\r
186 if (isset($this->aDSNInfo['hostspec']) && $this->aDSNInfo['hostspec']) {
\r
187 $osm2pgsql .= ' -H ' . $this->aDSNInfo['hostspec'];
\r
190 if (isset($this->aDSNInfo['password']) && $this->aDSNInfo['password']) {
\r
191 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
\r
193 $osm2pgsql .= ' -d '.$this->aDSNInfo['database'].' '.$sOSMFile;
\r
194 runWithEnv($osm2pgsql, $aProcEnv);
\r
195 if ($this->oDB == null) $this->oDB =& getDB();
\r
196 if (!$this->sIgnoreErrors && !chksql($this->oDB->getRow('select * from place limit 1'))) {
\r
201 public function createFunctions()
\r
203 info('Create Functions');
\r
205 $this->createSqlFunctions();
\r
208 public function createTables()
\r
210 info('Create Tables');
\r
212 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
\r
213 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
\r
214 $sTemplate = $this->replaceTablespace(
\r
215 '{ts:address-data}',
\r
216 CONST_Tablespace_Address_Data,
\r
219 $sTemplate = $this->replaceTablespace(
\r
220 '{ts:address-index}',
\r
221 CONST_Tablespace_Address_Index,
\r
224 $sTemplate = $this->replaceTablespace(
\r
225 '{ts:search-data}',
\r
226 CONST_Tablespace_Search_Data,
\r
229 $sTemplate = $this->replaceTablespace(
\r
230 '{ts:search-index}',
\r
231 CONST_Tablespace_Search_Index,
\r
234 $sTemplate = $this->replaceTablespace(
\r
236 CONST_Tablespace_Aux_Data,
\r
239 $sTemplate = $this->replaceTablespace(
\r
241 CONST_Tablespace_Aux_Index,
\r
245 $this->pgsqlRunScript($sTemplate, false);
\r
248 public function recreateFunction()
\r
250 // re-run the functions
\r
251 info('Recreate Functions');
\r
252 $this->createSqlFunctions();
\r
255 public function createPartitionTables()
\r
257 info('Create Partition Tables');
\r
259 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
\r
260 $sTemplate = $this->replaceTablespace(
\r
261 '{ts:address-data}',
\r
262 CONST_Tablespace_Address_Data,
\r
266 $sTemplate = $this->replaceTablespace(
\r
267 '{ts:address-index}',
\r
268 CONST_Tablespace_Address_Index,
\r
272 $sTemplate = $this->replaceTablespace(
\r
273 '{ts:search-data}',
\r
274 CONST_Tablespace_Search_Data,
\r
278 $sTemplate = $this->replaceTablespace(
\r
279 '{ts:search-index}',
\r
280 CONST_Tablespace_Search_Index,
\r
284 $sTemplate = $this->replaceTablespace(
\r
286 CONST_Tablespace_Aux_Data,
\r
290 $sTemplate = $this->replaceTablespace(
\r
292 CONST_Tablespace_Aux_Index,
\r
296 $this->pgsqlRunPartitionScript($sTemplate);
\r
299 public function createPartitionFunctions()
\r
301 info('Create Partition Functions');
\r
303 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
\r
304 $this->pgsqlRunPartitionScript($sTemplate);
\r
307 public function importWikipediaArticles()
\r
309 $sWikiArticlesFile = CONST_Wikipedia_Data_Path.'/wikipedia_article.sql.bin';
\r
310 $sWikiRedirectsFile = CONST_Wikipedia_Data_Path.'/wikipedia_redirect.sql.bin';
\r
311 if (file_exists($sWikiArticlesFile)) {
\r
312 info('Importing wikipedia articles');
\r
313 $this->pgsqlRunDropAndRestore($sWikiArticlesFile);
\r
315 warn('wikipedia article dump file not found - places will have default importance');
\r
317 if (file_exists($sWikiRedirectsFile)) {
\r
318 info('Importing wikipedia redirects');
\r
319 $this->pgsqlRunDropAndRestore($sWikiRedirectsFile);
\r
321 warn('wikipedia redirect dump file not found - some place importance values may be missing');
\r
323 echo ' finish wikipedia';
\r
326 public function loadData($bDisableTokenPrecalc)
\r
328 info('Drop old Data');
\r
330 if ($this->oDB == null) $this->oDB =& getDB();
\r
332 if (!pg_query($this->oDB->connection, 'TRUNCATE word')) fail(pg_last_error($this->oDB->connection));
\r
334 if (!pg_query($this->oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($this->oDB->connection));
\r
336 if (!pg_query($this->oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($this->oDB->connection));
\r
338 if (!pg_query($this->oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($this->oDB->connection));
\r
340 if (!pg_query($this->oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($this->oDB->connection));
\r
342 if (!pg_query($this->oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($this->oDB->connection));
\r
344 if (!pg_query($this->oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($this->oDB->connection));
\r
346 if (!pg_query($this->oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($this->oDB->connection));
\r
348 if (!pg_query($this->oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($this->oDB->connection));
\r
350 if (!pg_query($this->oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($this->oDB->connection));
\r
353 $sSQL = 'select distinct partition from country_name';
\r
354 $aPartitions = chksql($this->oDB->getCol($sSQL));
\r
355 if (!$this->bNoPartitions) $aPartitions[] = 0;
\r
356 foreach ($aPartitions as $sPartition) {
\r
357 if (!pg_query($this->oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($this->oDB->connection));
\r
361 // used by getorcreate_word_id to ignore frequent partial words
\r
362 $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
\r
363 $sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
\r
364 if (!pg_query($this->oDB->connection, $sSQL)) {
\r
365 fail(pg_last_error($this->oDB->connection));
\r
369 // pre-create the word list
\r
370 if (!$bDisableTokenPrecalc) {
\r
371 info('Loading word list');
\r
372 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
\r
376 $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
\r
377 $aDBInstances = array();
\r
378 $iLoadThreads = max(1, $this->iInstances - 1);
\r
379 for ($i = 0; $i < $iLoadThreads; $i++) {
\r
380 $aDBInstances[$i] =& getDB(true);
\r
381 $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
\r
382 $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
\r
383 $sSQL .= " and ST_GeometryType(geometry) = 'ST_LineString')";
\r
384 $sSQL .= ' and ST_IsValid(geometry)';
\r
385 if ($this->sVerbose) echo "$sSQL\n";
\r
386 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) {
\r
387 fail(pg_last_error($aDBInstances[$i]->connection));
\r
391 // last thread for interpolation lines
\r
392 $aDBInstances[$iLoadThreads] =& getDB(true);
\r
393 $sSQL = 'insert into location_property_osmline';
\r
394 $sSQL .= ' (osm_id, address, linegeo)';
\r
395 $sSQL .= ' SELECT osm_id, address, geometry from place where ';
\r
396 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
\r
397 if ($this->sVerbose) echo "$sSQL\n";
\r
398 if (!pg_send_query($aDBInstances[$iLoadThreads]->connection, $sSQL)) {
\r
399 fail(pg_last_error($aDBInstances[$iLoadThreads]->connection));
\r
403 for ($i = 0; $i <= $iLoadThreads; $i++) {
\r
404 while (($hPGresult = pg_get_result($aDBInstances[$i]->connection)) !== false) {
\r
405 $resultStatus = pg_result_status($hPGresult);
\r
406 // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
\r
407 // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
\r
408 // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
\r
409 echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
\r
410 if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
\r
411 $resultError = pg_result_error($hPGresult);
\r
412 echo '-- error text ' . $i . ': ' . $resultError . "\n";
\r
418 fail('SQL errors loading placex and/or location_property_osmline tables');
\r
421 info('Reanalysing database');
\r
422 $this->pgsqlRunScript('ANALYSE');
\r
424 $sDatabaseDate = getDatabaseDate($this->oDB);
\r
425 pg_query($this->oDB->connection, 'TRUNCATE import_status');
\r
426 if ($sDatabaseDate === false) {
\r
427 warn('could not determine database date.');
\r
429 $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
\r
430 pg_query($this->oDB->connection, $sSQL);
\r
431 echo "Latest data imported from $sDatabaseDate.\n";
\r
435 public function importTigerData()
\r
437 info('Import Tiger data');
\r
439 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
\r
440 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
\r
441 $sTemplate = $this->replaceTablespace(
\r
443 CONST_Tablespace_Aux_Data,
\r
446 $sTemplate = $this->replaceTablespace(
\r
448 CONST_Tablespace_Aux_Index,
\r
451 $this->pgsqlRunScript($sTemplate, false);
\r
453 $aDBInstances = array();
\r
454 for ($i = 0; $i < $this->iInstances; $i++) {
\r
455 $aDBInstances[$i] =& getDB(true);
\r
458 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
\r
460 $hFile = fopen($sFile, 'r');
\r
461 $sSQL = fgets($hFile, 100000);
\r
464 for ($i = 0; $i < $this->iInstances; $i++) {
\r
465 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
\r
466 while (pg_get_result($aDBInstances[$i]->connection));
\r
467 $sSQL = fgets($hFile, 100000);
\r
468 if (!$sSQL) break 2;
\r
469 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($this->oDB->connection));
\r
471 if ($iLines == 1000) {
\r
482 while ($bAnyBusy) {
\r
484 for ($i = 0; $i < $this->iInstances; $i++) {
\r
485 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
\r
492 info('Creating indexes on Tiger data');
\r
493 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
\r
494 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
\r
495 $sTemplate = $this->replaceTablespace(
\r
497 CONST_Tablespace_Aux_Data,
\r
500 $sTemplate = $this->replaceTablespace(
\r
502 CONST_Tablespace_Aux_Index,
\r
505 $this->pgsqlRunScript($sTemplate, false);
\r
508 public function calculatePostcodes($bCMDResultAll)
\r
510 info('Calculate Postcodes');
\r
511 if ($this->oDB == null) $this->oDB =& getDB();
\r
512 if (!pg_query($this->oDB->connection, 'TRUNCATE location_postcode')) {
\r
513 fail(pg_last_error($this->oDB->connection));
\r
517 $sSQL = 'INSERT INTO location_postcode';
\r
518 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
\r
519 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
\r
520 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
\r
521 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
\r
522 $sSQL .= ' FROM placex';
\r
523 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
\r
524 $sSQL .= ' AND geometry IS NOT null';
\r
525 $sSQL .= ' GROUP BY country_code, pc';
\r
527 if (!pg_query($this->oDB->connection, $sSQL)) {
\r
528 fail(pg_last_error($this->oDB->connection));
\r
531 if (CONST_Use_Extra_US_Postcodes) {
\r
532 // only add postcodes that are not yet available in OSM
\r
533 $sSQL = 'INSERT INTO location_postcode';
\r
534 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
\r
535 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
\r
536 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
\r
537 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
\r
538 $sSQL .= ' (SELECT postcode FROM location_postcode';
\r
539 $sSQL .= " WHERE country_code = 'us')";
\r
540 if (!pg_query($this->oDB->connection, $sSQL)) fail(pg_last_error($this->oDB->connection));
\r
543 // add missing postcodes for GB (if available)
\r
544 $sSQL = 'INSERT INTO location_postcode';
\r
545 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
\r
546 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
\r
547 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
\r
548 $sSQL .= ' (SELECT postcode FROM location_postcode';
\r
549 $sSQL .= " WHERE country_code = 'gb')";
\r
550 if (!pg_query($this->oDB->connection, $sSQL)) fail(pg_last_error($this->oDB->connection));
\r
552 if (!$bCMDResultAll) {
\r
553 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
\r
554 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
\r
555 if (!pg_query($this->oDB->connection, $sSQL)) {
\r
556 fail(pg_last_error($this->oDB->connection));
\r
559 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
\r
560 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
\r
562 if (!pg_query($this->oDB->connection, $sSQL)) {
\r
563 fail(pg_last_error($this->oDB->connection));
\r
567 public function index($bIndexNoanalyse)
\r
570 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$this->aDSNInfo['database'].' -P '
\r
571 .$this->aDSNInfo['port'].' -t '.$this->iInstances.$sOutputFile;
\r
572 if (isset($this->aDSNInfo['hostspec']) && $this->aDSNInfo['hostspec']) {
\r
573 $sBaseCmd .= ' -H ' . $this->aDSNInfo['hostspec'];
\r
575 if (isset($this->aDSNInfo['username']) && $this->aDSNInfo['username']) {
\r
576 $sBaseCmd .= ' -U ' . $this->aDSNInfo['username'];
\r
579 if (isset($this->aDSNInfo['password']) && $this->aDSNInfo['password']) {
\r
580 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
\r
583 info('Index ranks 0 - 4');
\r
584 $iStatus = runWithEnv($sBaseCmd.' -R 4', $aProcEnv);
\r
585 if ($iStatus != 0) {
\r
586 fail('error status ' . $iStatus . ' running nominatim!');
\r
588 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
\r
589 info('Index ranks 5 - 25');
\r
590 $iStatus = runWithEnv($sBaseCmd.' -r 5 -R 25', $aProcEnv);
\r
591 if ($iStatus != 0) {
\r
592 fail('error status ' . $iStatus . ' running nominatim!');
\r
594 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
\r
595 info('Index ranks 26 - 30');
\r
596 $iStatus = runWithEnv($sBaseCmd.' -r 26', $aProcEnv);
\r
597 if ($iStatus != 0) {
\r
598 fail('error status ' . $iStatus . ' running nominatim!');
\r
600 info('Index postcodes');
\r
601 if ($this->oDB == null) $this->oDB =& getDB();
\r
602 $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
\r
603 if (!pg_query($this->oDB->connection, $sSQL)) fail(pg_last_error($this->oDB->connection));
\r
606 public function createSearchIndices()
\r
608 info('Create Search indices');
\r
610 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
\r
611 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
\r
612 $sTemplate = $this->replaceTablespace(
\r
613 '{ts:address-index}',
\r
614 CONST_Tablespace_Address_Index,
\r
617 $sTemplate = $this->replaceTablespace(
\r
618 '{ts:search-index}',
\r
619 CONST_Tablespace_Search_Index,
\r
622 $sTemplate = $this->replaceTablespace(
\r
624 CONST_Tablespace_Aux_Index,
\r
627 $this->pgsqlRunScript($sTemplate);
\r
630 public function createCountryNames()
\r
632 info('Create search index for default country names');
\r
634 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
\r
635 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
\r
636 $this->pgsqlRunScript('select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x');
\r
637 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
\r
638 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
\r
639 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
\r
640 if (CONST_Languages) {
\r
643 foreach (explode(',', CONST_Languages) as $sLang) {
\r
644 $sSQL .= $sDelim."'name:$sLang'";
\r
649 // all include all simple name tags
\r
650 $sSQL .= "like 'name:%'";
\r
653 $this->pgsqlRunScript($sSQL);
\r
656 public function drop()
\r
658 info('Drop tables only required for updates');
\r
660 // The implementation is potentially a bit dangerous because it uses
\r
661 // a positive selection of tables to keep, and deletes everything else.
\r
662 // Including any tables that the unsuspecting user might have manually
\r
663 // created. USE AT YOUR OWN PERIL.
\r
664 // tables we want to keep. everything else goes.
\r
665 $aKeepTables = array(
\r
667 'import_polygon_*',
\r
669 'place_addressline',
\r
670 'location_postcode',
\r
671 'location_property*',
\r
680 'place_classtype_*'
\r
683 if ($this->oDB = null) $this->oDB =& getDB();
\r
684 $aDropTables = array();
\r
685 $aHaveTables = chksql($this->oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
\r
687 foreach ($aHaveTables as $sTable) {
\r
689 foreach ($aKeepTables as $sKeep) {
\r
690 if (fnmatch($sKeep, $sTable)) {
\r
695 if (!$bFound) array_push($aDropTables, $sTable);
\r
697 foreach ($aDropTables as $sDrop) {
\r
698 if ($this->sVerbose) echo "dropping table $sDrop\n";
\r
699 @pg_query($this->oDB->connection, "DROP TABLE $sDrop CASCADE");
\r
700 // ignore warnings/errors as they might be caused by a table having
\r
701 // been deleted already by CASCADE
\r
704 if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
\r
705 if ($sVerbose) echo 'deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
\r
706 unlink(CONST_Osm2pgsql_Flatnode_File);
\r
710 private function pgsqlRunDropAndRestore($sDumpFile)
\r
712 if (!isset($this->aDSNInfo['port']) || !$this->aDSNInfo['port']) $this->aDSNInfo['port'] = 5432;
\r
713 $sCMD = 'pg_restore -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
\r
714 if (isset($this->aDSNInfo['hostspec']) && $this->aDSNInfo['hostspec']) {
\r
715 $sCMD .= ' -h ' . $this->aDSNInfo['hostspec'];
\r
717 if (isset($this->aDSNInfo['username']) && $this->aDSNInfo['username']) {
\r
718 $sCMD .= ' -U ' . $this->aDSNInfo['username'];
\r
721 if (isset($this->aDSNInfo['password']) && $this->aDSNInfo['password']) {
\r
722 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
\r
724 $iReturn = runWithEnv($sCMD, $aProcEnv); // /lib/cmd.php "function runWithEnv($sCmd, $aEnv)"
\r
727 private function pgsqlRunScript($sScript, $bfatal = true)
\r
733 $this->sIgnoreErrors
\r
737 private function createSqlFunctions()
\r
739 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
\r
740 $sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate);
\r
741 if ($this->bEnableDiffUpdates) {
\r
742 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
\r
744 if ($this->bEnableDebugStatements) {
\r
745 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
\r
747 if (CONST_Limit_Reindexing) {
\r
748 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
\r
750 if (!CONST_Use_US_Tiger_Data) {
\r
751 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
\r
753 if (!CONST_Use_Aux_Location_data) {
\r
754 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
\r
756 $this->pgsqlRunScript($sTemplate);
\r
759 private function pgsqlRunPartitionScript($sTemplate)
\r
761 if ($this->oDB == null) $this->oDB =& getDB();
\r
763 $sSQL = 'select distinct partition from country_name';
\r
764 $aPartitions = chksql($this->oDB->getCol($sSQL));
\r
765 if (!$this->bNoPartitions) $aPartitions[] = 0;
\r
767 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
\r
768 foreach ($aMatches as $aMatch) {
\r
770 foreach ($aPartitions as $sPartitionName) {
\r
771 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
\r
773 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
\r
776 $this->pgsqlRunScript($sTemplate);
\r
779 private function pgsqlRunScriptFile($sFilename)
\r
781 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
\r
783 $sCMD = 'psql -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'];
\r
784 if (!$this->sVerbose) {
\r
787 if (isset($this->aDSNInfo['hostspec']) && $this->aDSNInfo['hostspec']) {
\r
788 $sCMD .= ' -h ' . $this->aDSNInfo['hostspec'];
\r
790 if (isset($this->aDSNInfo['username']) && $this->aDSNInfo['username']) {
\r
791 $sCMD .= ' -U ' . $this->aDSNInfo['username'];
\r
794 if (isset($this->aDSNInfo['password']) && $this->aDSNInfo['password']) {
\r
795 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
\r
797 $ahGzipPipes = null;
\r
798 if (preg_match('/\\.gz$/', $sFilename)) {
\r
799 $aDescriptors = array(
\r
800 0 => array('pipe', 'r'),
\r
801 1 => array('pipe', 'w'),
\r
802 2 => array('file', '/dev/null', 'a')
\r
804 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
\r
805 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
\r
806 $aReadPipe = $ahGzipPipes[1];
\r
807 fclose($ahGzipPipes[0]);
\r
809 $sCMD .= ' -f '.$sFilename;
\r
810 $aReadPipe = array('pipe', 'r');
\r
812 $aDescriptors = array(
\r
814 1 => array('pipe', 'w'),
\r
815 2 => array('file', '/dev/null', 'a')
\r
818 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
\r
819 if (!is_resource($hProcess)) fail('unable to start pgsql');
\r
820 // TODO: error checking
\r
821 while (!feof($ahPipes[1])) {
\r
822 echo fread($ahPipes[1], 4096);
\r
824 fclose($ahPipes[1]);
\r
825 $iReturn = proc_close($hProcess);
\r
826 if ($iReturn > 0) {
\r
827 fail("pgsql returned with error code ($iReturn)");
\r
829 if ($ahGzipPipes) {
\r
830 fclose($ahGzipPipes[1]);
\r
831 proc_close($hGzipProcess);
\r
835 private function replaceTablespace($sTemplate, $sTablespace, $sSql)
\r
837 if ($sTablespace) {
\r
838 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
\r
840 $sSql = str_replace($sTemplate, '', $sSql);
\r