X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/8fd7d319b7ef6ee4d4f8cf3b61eb47903e7fb1eb..a50bf1c94495d84963c24648c1e9abffcf7adda6:/utils/setup.php diff --git a/utils/setup.php b/utils/setup.php index 22c5a533..a363fab3 100755 --- a/utils/setup.php +++ b/utils/setup.php @@ -1,7 +1,8 @@ #!/usr/bin/php -Cq getTotalMemoryMB()) + if (isset($aCMDResult['osm2pgsql-cache'])) + { + $iCacheMemory = $aCMDResult['osm2pgsql-cache']; + } + else { $iCacheMemory = getCacheMemoryMB(); - echo "WARNING: resetting cache memory to $iCacheMemory\n"; } $aDSNInfo = DB::parseDSN(CONST_Database_DSN); if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432; + $fPostgisVersion = (float) CONST_Postgis_Version; + if ($aCMDResult['create-db'] || $aCMDResult['all']) { echo "Create DB\n"; @@ -108,7 +115,7 @@ $oDB =& getDB(); $sVersionString = $oDB->getOne('select version()'); - preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[.]([0-9]+) #', $sVersionString, $aMatches); + preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[^0-9]#', $sVersionString, $aMatches); if (CONST_Postgresql_Version != $aMatches[1].'.'.$aMatches[2]) { echo "ERROR: PostgreSQL version is not correct. Expected ".CONST_Postgresql_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n"; @@ -124,7 +131,16 @@ pgsqlRunScript('CREATE EXTENSION hstore'); } - pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql'); + if ($fPostgisVersion < 2.0) { + pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql'); + pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql'); + } else { + pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis'); + } + if ($fPostgisVersion < 2.1) { + // Function was renamed in 2.1 and throws an annoying deprecation warning + pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint'); + } $sVersionString = $oDB->getOne('select postgis_full_version()'); preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches); if (CONST_Postgis_Version != $aMatches[1].'.'.$aMatches[2]) @@ -133,14 +149,33 @@ exit; } - pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql'); pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql'); pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql'); pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql'); - pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode.sql'); + pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql'); + if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz')) + { + pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz'); + } + else + { + echo "WARNING: external UK postcode table not found.\n"; + } pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql'); pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql'); pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql'); + + if ($aCMDResult['no-partitions']) + { + pgsqlRunScript('update country_name set partition = 0'); + } + + // the following will be needed by create_functions later but + // is only defined in the subsequently called create_tables. + // Create dummies here that will be overwritten by the proper + // versions in create-tables. + pgsqlRunScript('CREATE TABLE place_boundingbox ()'); + pgsqlRunScript('create type wikipedia_article_match as ()'); } if ($aCMDResult['import-data'] || $aCMDResult['all']) @@ -159,7 +194,15 @@ { $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File; } - $osm2pgsql .= ' -lsc -O gazetteer --hstore'; + if (CONST_Tablespace_Osm2pgsql_Data) + $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data; + if (CONST_Tablespace_Osm2pgsql_Index) + $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index; + if (CONST_Tablespace_Place_Data) + $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data; + if (CONST_Tablespace_Place_Index) + $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index; + $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1'; $osm2pgsql .= ' -C '.$iCacheMemory; $osm2pgsql .= ' -P '.$aDSNInfo['port']; $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file']; @@ -177,11 +220,21 @@ { echo "Functions\n"; $bDidSomething = true; - if (!file_exists(CONST_BasePath.'/module/nominatim.so')) fail("nominatim module not built"); + if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built"); $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql'); - $sTemplate = str_replace('{modulepath}', CONST_BasePath.'/module', $sTemplate); + $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate); if ($aCMDResult['enable-diff-updates']) $sTemplate = str_replace('RETURN NEW; -- @DIFFUPDATES@', '--', $sTemplate); if ($aCMDResult['enable-debug-statements']) $sTemplate = str_replace('--DEBUG:', '', $sTemplate); + if (CONST_Limit_Reindexing) $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate); + pgsqlRunScript($sTemplate); + + if ($fPostgisVersion < 2.0) { + echo "Helper functions for postgis < 2.0\n"; + $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_15_aux.sql'); + } else { + echo "Helper functions for postgis >= 2.0\n"; + $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_20_aux.sql'); + } pgsqlRunScript($sTemplate); } @@ -217,13 +270,30 @@ if ($aCMDResult['create-tables'] || $aCMDResult['all']) { - echo "Tables\n"; $bDidSomething = true; - pgsqlRunScriptFile(CONST_BasePath.'/sql/tables.sql'); + + echo "Tables\n"; + $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql'); + $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate); + $sTemplate = replace_tablespace('{ts:address-data}', + CONST_Tablespace_Address_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:address-index}', + CONST_Tablespace_Address_Index, $sTemplate); + $sTemplate = replace_tablespace('{ts:search-data}', + CONST_Tablespace_Search_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:search-index}', + CONST_Tablespace_Search_Index, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-data}', + CONST_Tablespace_Aux_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-index}', + CONST_Tablespace_Aux_Index, $sTemplate); + pgsqlRunScript($sTemplate, false); // re-run the functions + echo "Functions\n"; $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql'); - $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate); + $sTemplate = str_replace('{modulepath}', + CONST_InstallPath.'/module', $sTemplate); pgsqlRunScript($sTemplate); } @@ -232,15 +302,27 @@ echo "Partition Tables\n"; $bDidSomething = true; $oDB =& getDB(); - $sSQL = 'select partition from country_name order by country_code'; + $sSQL = 'select distinct partition from country_name'; $aPartitions = $oDB->getCol($sSQL); if (PEAR::isError($aPartitions)) { fail($aPartitions->getMessage()); } - $aPartitions[] = 0; + if (!$aCMDResult['no-partitions']) $aPartitions[] = 0; $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql'); + $sTemplate = replace_tablespace('{ts:address-data}', + CONST_Tablespace_Address_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:address-index}', + CONST_Tablespace_Address_Index, $sTemplate); + $sTemplate = replace_tablespace('{ts:search-data}', + CONST_Tablespace_Search_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:search-index}', + CONST_Tablespace_Search_Index, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-data}', + CONST_Tablespace_Aux_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-index}', + CONST_Tablespace_Aux_Index, $sTemplate); preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER); foreach($aMatches as $aMatch) { @@ -261,13 +343,13 @@ echo "Partition Functions\n"; $bDidSomething = true; $oDB =& getDB(); - $sSQL = 'select partition from country_name order by country_code'; + $sSQL = 'select distinct partition from country_name'; $aPartitions = $oDB->getCol($sSQL); if (PEAR::isError($aPartitions)) { fail($aPartitions->getMessage()); } - $aPartitions[] = 0; + if (!$aCMDResult['no-partitions']) $aPartitions[] = 0; $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql'); preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER); @@ -337,13 +419,13 @@ if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection)); echo '.'; - $sSQL = 'select partition from country_name order by country_code'; + $sSQL = 'select distinct partition from country_name'; $aPartitions = $oDB->getCol($sSQL); if (PEAR::isError($aPartitions)) { fail($aPartitions->getMessage()); } - $aPartitions[] = 0; + if (!$aCMDResult['no-partitions']) $aPartitions[] = 0; foreach($aPartitions as $sPartition) { if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection)); @@ -388,40 +470,17 @@ pgsqlRunScript('ANALYSE'); } - if ($aCMDResult['create-roads']) - { - $bDidSomething = true; - - $oDB =& getDB(); - $aDBInstances = array(); - for($i = 0; $i < $iInstances; $i++) - { - $aDBInstances[$i] =& getDB(true); - if (!pg_query($aDBInstances[$i]->connection, 'set enable_bitmapscan = off')) fail(pg_last_error($oDB->connection)); - $sSQL = 'select count(*) from (select insertLocationRoad(partition, place_id, calculated_country_code, geometry) from '; - $sSQL .= 'placex where osm_id % '.$iInstances.' = '.$i.' and rank_search between 26 and 27 and class = \'highway\') as x '; - if ($aCMDResult['verbose']) echo "$sSQL\n"; - if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection)); - } - $bAnyBusy = true; - while($bAnyBusy) - { - $bAnyBusy = false; - for($i = 0; $i < $iInstances; $i++) - { - if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true; - } - sleep(1); - echo '.'; - } - echo "\n"; - } - if ($aCMDResult['import-tiger-data']) { $bDidSomething = true; - pgsqlRunScriptFile(CONST_BasePath.'/sql/tiger_import_start.sql'); + $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql'); + $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-data}', + CONST_Tablespace_Aux_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-index}', + CONST_Tablespace_Aux_Index, $sTemplate); + pgsqlRunScript($sTemplate, false); $aDBInstances = array(); for($i = 0; $i < $iInstances; $i++) @@ -429,7 +488,7 @@ $aDBInstances[$i] =& getDB(true); } - foreach(glob(CONST_BasePath.'/data/tiger2011/*.sql') as $sFile) + foreach(glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) { echo $sFile.': '; $hFile = fopen($sFile, "r"); @@ -473,7 +532,13 @@ } echo "Creating indexes\n"; - pgsqlRunScriptFile(CONST_BasePath.'/sql/tiger_import_finish.sql'); + $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql'); + $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-data}', + CONST_Tablespace_Aux_Data, $sTemplate); + $sTemplate = replace_tablespace('{ts:aux-index}', + CONST_Tablespace_Aux_Index, $sTemplate); + pgsqlRunScript($sTemplate, false); } if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) @@ -494,7 +559,7 @@ if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection)); } - if ($aCMDResult['osmosis-init'] || $aCMDResult['all']) + if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) // no use doing osmosis-init when dropping update tables { $bDidSomething = true; $oDB =& getDB(); @@ -509,20 +574,20 @@ } else { - if (file_exists(CONST_BasePath.'/settings/configuration.txt')) + if (file_exists(CONST_InstallPath.'/settings/configuration.txt')) { echo "settings/configuration.txt already exists\n"; } else { - passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_BasePath.'/settings'); + passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings'); // update osmosis configuration.txt with our settings - passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_BasePath.'/settings/configuration.txt'); - passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_BasePath.'/settings/configuration.txt'); + passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt'); + passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt'); } // Find the last node in the DB - $iLastOSMID = $oDB->getOne("select max(id) from planet_osm_nodes"); + $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'"); // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed) $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1"; @@ -532,49 +597,59 @@ // Search for the correct state file - uses file timestamps so need to sort by date descending $sRepURL = CONST_Replication_Url."/"; - $sRep = file_get_contents($sRepURL."?C=M;O=D"); + $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1"); // download.geofabrik.de: 000/