]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/admin/query.php
beb2f9ef32c034e516aaad8266b932d685b03745
[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_Aux_Location_data', getSettingBool('USE_AUX_LOCATION_DATA'));
52 @define('CONST_Use_US_Tiger_Data', getSettingBool('USE_US_TIGER_DATA'));
53 @define('CONST_MapIcon_URL', getSetting('MAPICON_URL', false));
54
55
56 $oDB = new Nominatim\DB;
57 $oDB->connect();
58
59 if (isset($aCMDResult['nodedupe'])) $aCMDResult['dedupe'] = 'false';
60
61 $oParams = new Nominatim\ParameterParser($aCMDResult);
62
63 $aSearchParams = array(
64                      'search',
65                      'amenity',
66                      'street',
67                      'city',
68                      'county',
69                      'state',
70                      'country',
71                      'postalcode'
72                  );
73
74 if (!$oParams->hasSetAny($aSearchParams)) {
75     showUsage($aCMDOptions, true);
76     return 1;
77 }
78
79 $oGeocode = new Nominatim\Geocode($oDB);
80
81 $oGeocode->setLanguagePreference($oParams->getPreferredLanguages(false));
82 $oGeocode->setReverseInPlan(true);
83 $oGeocode->loadParamArray($oParams);
84
85 if ($oParams->getBool('search')) {
86     $oGeocode->setQuery($aCMDResult['search']);
87 } else {
88     $oGeocode->setQueryFromParams($oParams);
89 }
90
91 $aSearchResults = $oGeocode->lookup();
92
93 echo json_encode($aSearchResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)."\n";