2 @define('CONST_LibDir', dirname(dirname(__FILE__)));
4 require_once(CONST_LibDir.'/init-cmd.php');
5 require_once(CONST_LibDir.'/setup_functions.php');
7 ini_set('memory_limit', '800M');
9 // (long-opt, short-opt, min-occurs, max-occurs, num-arguments, num-arguments, type, help)
12 'Import / update / index osm data',
13 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
14 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
15 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
17 array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Update postcode centroid table'),
19 array('import-file', '', 0, 1, 1, 1, 'realpath', 'Re-import data from an OSM file'),
20 array('import-diff', '', 0, 1, 1, 1, 'realpath', 'Import a diff (osc) file from local file system'),
21 array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
23 array('import-node', '', 0, 1, 1, 1, 'int', 'Re-import node'),
24 array('import-way', '', 0, 1, 1, 1, 'int', 'Re-import way'),
25 array('import-relation', '', 0, 1, 1, 1, 'int', 'Re-import relation'),
26 array('import-from-main-api', '', 0, 1, 0, 0, 'bool', 'Use OSM API instead of Overpass to download objects'),
28 array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
31 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aResult, true, true);
33 loadSettings($aCMDResult['project-dir'] ?? getcwd());
36 date_default_timezone_set('Etc/UTC');
38 $oDB = new Nominatim\DB();
40 $fPostgresVersion = $oDB->getPostgresVersion();
42 $aDSNInfo = Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
43 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
45 // cache memory to be used by osm2pgsql, should not be more than the available memory
46 $iCacheMemory = (isset($aResult['osm2pgsql-cache'])?$aResult['osm2pgsql-cache']:2000);
47 if ($iCacheMemory + 500 > getTotalMemoryMB()) {
48 $iCacheMemory = getCacheMemoryMB();
49 echo "WARNING: resetting cache memory to $iCacheMemory\n";
52 $oOsm2pgsqlCmd = (new \Nominatim\Shell(getOsm2pgsqlBinary()))
53 ->addParams('--hstore')
54 ->addParams('--latlong')
55 ->addParams('--append')
57 ->addParams('--with-forward-dependencies', 'false')
58 ->addParams('--log-progress', 'true')
59 ->addParams('--number-processes', 1)
60 ->addParams('--cache', $iCacheMemory)
61 ->addParams('--output', 'gazetteer')
62 ->addParams('--style', getImportStyle())
63 ->addParams('--database', $aDSNInfo['database'])
64 ->addParams('--port', $aDSNInfo['port']);
66 if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
67 $oOsm2pgsqlCmd->addParams('--host', $aDSNInfo['hostspec']);
69 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
70 $oOsm2pgsqlCmd->addParams('--user', $aDSNInfo['username']);
72 if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
73 $oOsm2pgsqlCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
75 if (getSetting('FLATNODE_FILE')) {
76 $oOsm2pgsqlCmd->addParams('--flat-nodes', getSetting('FLATNODE_FILE'));
78 if ($fPostgresVersion >= 11.0) {
79 $oOsm2pgsqlCmd->addEnvPair(
81 '-c jit=off -c max_parallel_workers_per_gather=0'
85 if (isset($aResult['import-diff']) || isset($aResult['import-file'])) {
86 // import diffs and files directly (e.g. from osmosis --rri)
87 $sNextFile = isset($aResult['import-diff']) ? $aResult['import-diff'] : $aResult['import-file'];
89 if (!file_exists($sNextFile)) {
90 fail("Cannot open $sNextFile\n");
94 $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sNextFile);
95 echo $oCMD->escapedCmd()."\n";
99 fail("Error from osm2pgsql, $iRet\n");
102 // Don't update the import status - we don't know what this file contains
105 $sTemporaryFile = CONST_InstallDir.'/osmosischange.osc';
107 $bUseOSMApi = isset($aResult['import-from-main-api']) && $aResult['import-from-main-api'];
109 if (isset($aResult['import-node']) && $aResult['import-node']) {
111 $sContentURL = 'https://www.openstreetmap.org/api/0.6/node/'.$aResult['import-node'];
113 $sContentURL = 'https://overpass-api.de/api/interpreter?data=node('.$aResult['import-node'].');out%20meta;';
117 if (isset($aResult['import-way']) && $aResult['import-way']) {
119 $sContentURL = 'https://www.openstreetmap.org/api/0.6/way/'.$aResult['import-way'].'/full';
121 $sContentURL = 'https://overpass-api.de/api/interpreter?data=(way('.$aResult['import-way'].');%3E;);out%20meta;';
125 if (isset($aResult['import-relation']) && $aResult['import-relation']) {
127 $sContentURL = 'https://www.openstreetmap.org/api/0.6/relation/'.$aResult['import-relation'].'/full';
129 $sContentURL = 'https://overpass-api.de/api/interpreter?data=(rel(id:'.$aResult['import-relation'].');%3E;);out%20meta;';
134 file_put_contents($sTemporaryFile, file_get_contents($sContentURL));
139 // import generated change file
141 $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sTemporaryFile);
142 echo $oCMD->escapedCmd()."\n";
144 $iRet = $oCMD->run();
146 fail("osm2pgsql exited with error level $iRet\n");