]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/admin/query.php
268b87cc0e0bbe1e2e895cf655736e5cb53c80d9
[nominatim.git] / lib-php / admin / query.php
1 <?php
2 @define('CONST_LibDir', dirname(dirname(__FILE__)));
3
4 require_once(CONST_LibDir.'/init-cmd.php');
5 require_once(CONST_LibDir.'/Geocode.php');
6 require_once(CONST_LibDir.'/ParameterParser.php');
7 ini_set('memory_limit', '800M');
8
9 $aCMDOptions
10 = array(
11    'Query database from command line. Returns search result as JSON.',
12    array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
13    array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
14    array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
15
16    array('search', '', 0, 1, 1, 1, 'string', 'Search for given term or coordinate'),
17    array('country', '', 0, 1, 1, 1, 'string', 'Structured search: country'),
18    array('state', '', 0, 1, 1, 1, 'string', 'Structured search: state'),
19    array('county', '', 0, 1, 1, 1, 'string', 'Structured search: county'),
20    array('city', '', 0, 1, 1, 1, 'string', 'Structured search: city'),
21    array('street', '', 0, 1, 1, 1, 'string', 'Structured search: street'),
22    array('amenity', '', 0, 1, 1, 1, 'string', 'Structured search: amenity'),
23    array('postalcode', '', 0, 1, 1, 1, 'string', 'Structured search: postal code'),
24
25    array('accept-language', '', 0, 1, 1, 1, 'string', 'Preferred language order for showing search results'),
26    array('bounded', '', 0, 1, 0, 0, 'bool', 'Restrict results to given viewbox'),
27    array('nodedupe', '', 0, 1, 0, 0, 'bool', 'Do not remove duplicate results'),
28    array('limit', '', 0, 1, 1, 1, 'int', 'Maximum number of results returned (default: 10)'),
29    array('exclude_place_ids', '', 0, 1, 1, 1, 'string', 'Comma-separated list of place ids to exclude from results'),
30    array('featureType', '', 0, 1, 1, 1, 'string', 'Restrict results to certain features (country, state,city,settlement)'),
31    array('countrycodes', '', 0, 1, 1, 1, 'string', 'Comma-separated list of countries to restrict search to'),
32    array('viewbox', '', 0, 1, 1, 1, 'string', 'Prefer results in given view box'),
33
34    array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
35   );
36 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
37
38 loadSettings($aCMDResult['project-dir'] ?? getcwd());
39
40 @define('CONST_Database_DSN', getSetting('DATABASE_DSN'));
41 @define('CONST_Default_Language', getSetting('DEFAULT_LANGUAGE', false));
42 @define('CONST_Log_DB', getSettingBool('LOG_DB'));
43 @define('CONST_Log_File', getSetting('LOG_FILE', false));
44 @define('CONST_Max_Word_Frequency', getSetting('MAX_WORD_FREQUENCY'));
45 @define('CONST_NoAccessControl', getSettingBool('CORS_NOACCESSCONTROL'));
46 @define('CONST_Places_Max_ID_count', getSetting('LOOKUP_MAX_COUNT'));
47 @define('CONST_PolygonOutput_MaximumTypes', getSetting('POLYGON_OUTPUT_MAX_TYPES'));
48 @define('CONST_Search_BatchMode', getSettingBool('SEARCH_BATCH_MODE'));
49 @define('CONST_Search_NameOnlySearchFrequencyThreshold', getSetting('SEARCH_NAME_ONLY_THRESHOLD'));
50 @define('CONST_Term_Normalization_Rules', getSetting('TERM_NORMALIZATION'));
51 @define('CONST_Use_US_Tiger_Data', getSettingBool('USE_US_TIGER_DATA'));
52 @define('CONST_MapIcon_URL', getSetting('MAPICON_URL', false));
53
54
55 $oDB = new Nominatim\DB;
56 $oDB->connect();
57
58 if (isset($aCMDResult['nodedupe'])) $aCMDResult['dedupe'] = 'false';
59
60 $oParams = new Nominatim\ParameterParser($aCMDResult);
61
62 $aSearchParams = array(
63                      'search',
64                      'amenity',
65                      'street',
66                      'city',
67                      'county',
68                      'state',
69                      'country',
70                      'postalcode'
71                  );
72
73 if (!$oParams->hasSetAny($aSearchParams)) {
74     showUsage($aCMDOptions, true);
75     return 1;
76 }
77
78 $oGeocode = new Nominatim\Geocode($oDB);
79
80 $oGeocode->setLanguagePreference($oParams->getPreferredLanguages(false));
81 $oGeocode->loadParamArray($oParams);
82
83 if ($oParams->getBool('search')) {
84     $oGeocode->setQuery($aCMDResult['search']);
85 } else {
86     $oGeocode->setQueryFromParams($oParams);
87 }
88
89 $aSearchResults = $oGeocode->lookup();
90
91 echo json_encode($aSearchResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)."\n";