]> git.openstreetmap.org Git - nominatim.git/blob - lib/admin/warm.php
Merge pull request #2135 from lonvia/python-frontend
[nominatim.git] / lib / admin / warm.php
1 <?php
2 @define('CONST_LibDir', dirname(dirname(__FILE__)));
3
4 require_once(CONST_LibDir.'/init-cmd.php');
5 require_once(CONST_LibDir.'/log.php');
6 require_once(CONST_LibDir.'/Geocode.php');
7 require_once(CONST_LibDir.'/PlaceLookup.php');
8 require_once(CONST_LibDir.'/ReverseGeocode.php');
9
10 ini_set('memory_limit', '800M');
11
12 $aCMDOptions = array(
13                 'Tools to warm nominatim db',
14                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
15                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
16                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
17                 array('reverse-only', '', 0, 1, 0, 0, 'bool', 'Warm reverse only'),
18                 array('search-only', '', 0, 1, 0, 0, 'bool', 'Warm search only'),
19                 array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
20                );
21 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aResult, true, true);
22
23 loadSettings($aCMDResult['project-dir'] ?? getcwd());
24
25 @define('CONST_Database_DSN', getSetting('DATABASE_DSN'));
26 @define('CONST_Default_Language', getSetting('DEFAULT_LANGUAGE', false));
27 @define('CONST_Log_DB', getSettingBool('LOG_DB'));
28 @define('CONST_Log_File', getSetting('LOG_FILE', false));
29 @define('CONST_Max_Word_Frequency', getSetting('MAX_WORD_FREQUENCY'));
30 @define('CONST_NoAccessControl', getSettingBool('CORS_NOACCESSCONTROL'));
31 @define('CONST_Places_Max_ID_count', getSetting('LOOKUP_MAX_COUNT'));
32 @define('CONST_PolygonOutput_MaximumTypes', getSetting('POLYGON_OUTPUT_MAX_TYPES'));
33 @define('CONST_Search_BatchMode', getSettingBool('SEARCH_BATCH_MODE'));
34 @define('CONST_Search_NameOnlySearchFrequencyThreshold', getSetting('SEARCH_NAME_ONLY_THRESHOLD'));
35 @define('CONST_Term_Normalization_Rules', getSetting('TERM_NORMALIZATION'));
36 @define('CONST_Use_Aux_Location_data', getSettingBool('USE_AUX_LOCATION_DATA'));
37 @define('CONST_Use_US_Tiger_Data', getSettingBool('USE_US_TIGER_DATA'));
38 @define('CONST_MapIcon_URL', getSetting('MAPICON_URL', false));
39
40
41 $oDB = new Nominatim\DB();
42 $oDB->connect();
43
44 $bVerbose = $aResult['verbose'];
45
46 function print_results($aResults, $bVerbose)
47 {
48     if ($bVerbose) {
49         if ($aResults && count($aResults)) {
50             echo $aResults[0]['langaddress']."\n";
51         } else {
52             echo "<not found>\n";
53         }
54     } else {
55         echo '.';
56     }
57 }
58
59 if (!$aResult['search-only']) {
60     $oReverseGeocode = new Nominatim\ReverseGeocode($oDB);
61     $oReverseGeocode->setZoom(20);
62     $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
63     $oPlaceLookup->setIncludeAddressDetails(true);
64     $oPlaceLookup->setLanguagePreference(array('en'));
65
66     echo 'Warm reverse: ';
67     if ($bVerbose) echo "\n";
68     for ($i = 0; $i < 1000; $i++) {
69         $fLat = rand(-9000, 9000) / 100;
70         $fLon = rand(-18000, 18000) / 100;
71         if ($bVerbose) echo "$fLat, $fLon = ";
72
73         $oLookup = $oReverseGeocode->lookup($fLat, $fLon);
74         $aSearchResults = $oLookup ? $oPlaceLookup->lookup(array($oLookup->iId => $oLookup)) : null;
75         print_results($aSearchResults, $bVerbose);
76     }
77     echo "\n";
78 }
79
80 if (!$aResult['reverse-only']) {
81     $oGeocode = new Nominatim\Geocode($oDB);
82
83     echo 'Warm search: ';
84     if ($bVerbose) echo "\n";
85     $sSQL = 'SELECT word FROM word WHERE word is not null ORDER BY search_name_count DESC LIMIT 1000';
86     foreach ($oDB->getCol($sSQL) as $sWord) {
87         if ($bVerbose) echo "$sWord = ";
88
89         $oGeocode->setLanguagePreference(array('en'));
90         $oGeocode->setQuery($sWord);
91         $aSearchResults = $oGeocode->lookup();
92         print_results($aSearchResults, $bVerbose);
93     }
94     echo "\n";
95 }