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']) {
44 $aDSNInfo['port'] = 5432;
47 // cache memory to be used by osm2pgsql, should not be more than the available memory
48 $iCacheMemory = (isset($aResult['osm2pgsql-cache'])?$aResult['osm2pgsql-cache']:2000);
49 if ($iCacheMemory + 500 > getTotalMemoryMB()) {
50 $iCacheMemory = getCacheMemoryMB();
51 echo "WARNING: resetting cache memory to $iCacheMemory\n";
54 $oOsm2pgsqlCmd = (new \Nominatim\Shell(getOsm2pgsqlBinary()))
55 ->addParams('--hstore')
56 ->addParams('--latlong')
57 ->addParams('--append')
59 ->addParams('--with-forward-dependencies', 'false')
60 ->addParams('--log-progress', 'true')
61 ->addParams('--number-processes', 1)
62 ->addParams('--cache', $iCacheMemory)
63 ->addParams('--output', 'gazetteer')
64 ->addParams('--style', getImportStyle())
65 ->addParams('--database', $aDSNInfo['database'])
66 ->addParams('--port', $aDSNInfo['port']);
68 if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
69 $oOsm2pgsqlCmd->addParams('--host', $aDSNInfo['hostspec']);
71 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
72 $oOsm2pgsqlCmd->addParams('--user', $aDSNInfo['username']);
74 if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
75 $oOsm2pgsqlCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
77 if (getSetting('FLATNODE_FILE')) {
78 $oOsm2pgsqlCmd->addParams('--flat-nodes', getSetting('FLATNODE_FILE'));
80 if ($fPostgresVersion >= 11.0) {
81 $oOsm2pgsqlCmd->addEnvPair(
83 '-c jit=off -c max_parallel_workers_per_gather=0'
87 if (isset($aResult['import-diff']) || isset($aResult['import-file'])) {
88 // import diffs and files directly (e.g. from osmosis --rri)
89 $sNextFile = isset($aResult['import-diff']) ? $aResult['import-diff'] : $aResult['import-file'];
91 if (!file_exists($sNextFile)) {
92 fail("Cannot open $sNextFile\n");
96 $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sNextFile);
97 echo $oCMD->escapedCmd()."\n";
101 fail("Error from osm2pgsql, $iRet\n");
104 // Don't update the import status - we don't know what this file contains
107 $sTemporaryFile = CONST_InstallDir.'/osmosischange.osc';
109 $bUseOSMApi = isset($aResult['import-from-main-api']) && $aResult['import-from-main-api'];
111 if (isset($aResult['import-node']) && $aResult['import-node']) {
113 $sContentURL = 'https://www.openstreetmap.org/api/0.6/node/'.$aResult['import-node'];
115 $sContentURL = 'https://overpass-api.de/api/interpreter?data=node('.$aResult['import-node'].');out%20meta;';
119 if (isset($aResult['import-way']) && $aResult['import-way']) {
121 $sContentURL = 'https://www.openstreetmap.org/api/0.6/way/'.$aResult['import-way'].'/full';
123 $sContentURL = 'https://overpass-api.de/api/interpreter?data=(way('.$aResult['import-way'].');%3E;);out%20meta;';
127 if (isset($aResult['import-relation']) && $aResult['import-relation']) {
129 $sContentURL = 'https://www.openstreetmap.org/api/0.6/relation/'.$aResult['import-relation'].'/full';
131 $sContentURL = 'https://overpass-api.de/api/interpreter?data=(rel(id:'.$aResult['import-relation'].');%3E;);out%20meta;';
136 file_put_contents($sTemporaryFile, file_get_contents($sContentURL));
141 // import generated change file
143 $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sTemporaryFile);
144 echo $oCMD->escapedCmd()."\n";
146 $iRet = $oCMD->run();
148 fail("osm2pgsql exited with error level $iRet\n");