]> git.openstreetmap.org Git - nominatim.git/blob - utils/query.php
429b30ff30117427e30f9b4ce51fe015bc965300
[nominatim.git] / utils / query.php
1 <?php
2
3 require_once(CONST_LibDir.'/init-cmd.php');
4 require_once(CONST_LibDir.'/Geocode.php');
5 require_once(CONST_LibDir.'/ParameterParser.php');
6 ini_set('memory_limit', '800M');
7
8 $aCMDOptions
9 = array(
10    'Query database from command line. Returns search result as JSON.',
11    array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
12    array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
13    array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
14
15    array('search', '', 0, 1, 1, 1, 'string', 'Search for given term or coordinate'),
16    array('country', '', 0, 1, 1, 1, 'string', 'Structured search: country'),
17    array('state', '', 0, 1, 1, 1, 'string', 'Structured search: state'),
18    array('county', '', 0, 1, 1, 1, 'string', 'Structured search: county'),
19    array('city', '', 0, 1, 1, 1, 'string', 'Structured search: city'),
20    array('street', '', 0, 1, 1, 1, 'string', 'Structured search: street'),
21    array('amenity', '', 0, 1, 1, 1, 'string', 'Structured search: amenity'),
22    array('postalcode', '', 0, 1, 1, 1, 'string', 'Structured search: postal code'),
23
24    array('accept-language', '', 0, 1, 1, 1, 'string', 'Preferred language order for showing search results'),
25    array('bounded', '', 0, 1, 0, 0, 'bool', 'Restrict results to given viewbox'),
26    array('nodedupe', '', 0, 1, 0, 0, 'bool', 'Do not remove duplicate results'),
27    array('limit', '', 0, 1, 1, 1, 'int', 'Maximum number of results returned (default: 10)'),
28    array('exclude_place_ids', '', 0, 1, 1, 1, 'string', 'Comma-separated list of place ids to exclude from results'),
29    array('featureType', '', 0, 1, 1, 1, 'string', 'Restrict results to certain features (country, state,city,settlement)'),
30    array('countrycodes', '', 0, 1, 1, 1, 'string', 'Comma-separated list of countries to restrict search to'),
31    array('viewbox', '', 0, 1, 1, 1, 'string', 'Prefer results in given view box'),
32
33    array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
34   );
35 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
36
37 loadSettings($aCMDResult['project-dir'] ?? getcwd());
38
39 @define('CONST_Database_DSN', getSetting('DATABASE_DSN'));
40 @define('CONST_Default_Language', getSetting('DEFAULT_LANGUAGE', false));
41 @define('CONST_Log_DB', getSettingBool('LOG_DB'));
42 @define('CONST_Log_File', getSetting('LOG_FILE', false));
43 @define('CONST_Max_Word_Frequency', getSetting('MAX_WORD_FREQUENCY'));
44 @define('CONST_NoAccessControl', getSettingBool('CORS_NOACCESSCONTROL'));
45 @define('CONST_Places_Max_ID_count', getSetting('LOOKUP_MAX_COUNT'));
46 @define('CONST_PolygonOutput_MaximumTypes', getSetting('POLYGON_OUTPUT_MAX_TYPES'));
47 @define('CONST_Search_BatchMode', getSettingBool('SEARCH_BATCH_MODE'));
48 @define('CONST_Search_NameOnlySearchFrequencyThreshold', getSetting('SEARCH_NAME_ONLY_THRESHOLD'));
49 @define('CONST_Term_Normalization_Rules', getSetting('TERM_NORMALIZATION'));
50 @define('CONST_Use_Aux_Location_data', getSettingBool('USE_AUX_LOCATION_DATA'));
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->setReverseInPlan(true);
82 $oGeocode->loadParamArray($oParams);
83
84 if ($oParams->getBool('search')) {
85     $oGeocode->setQuery($aCMDResult['search']);
86 } else {
87     $oGeocode->setQueryFromParams($oParams);
88 }
89
90 $aSearchResults = $oGeocode->lookup();
91
92 echo json_encode($aSearchResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)."\n";