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