]> git.openstreetmap.org Git - nominatim.git/blob - utils/query.php
make phpcs happy
[nominatim.git] / utils / query.php
1 <?php
2
3 require_once(CONST_BasePath.'/lib/init-cmd.php');
4 require_once(CONST_BasePath.'/lib/Geocode.php');
5 require_once(CONST_BasePath.'/lib/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 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
34
35 $oDB = new Nominatim\DB;
36 $oDB->connect();
37
38 if (isset($aCMDResult['nodedupe'])) $aCMDResult['dedupe'] = 'false';
39
40 $oParams = new Nominatim\ParameterParser($aCMDResult);
41
42 $aSearchParams = array(
43                      'search',
44                      'amenity',
45                      'street',
46                      'city',
47                      'county',
48                      'state',
49                      'country',
50                      'postalcode'
51                  );
52
53 if (!$oParams->hasSetAny($aSearchParams)) {
54     showUsage($aCMDOptions, true);
55     return 1;
56 }
57
58 $oGeocode = new Nominatim\Geocode($oDB);
59
60 $oGeocode->setLanguagePreference($oParams->getPreferredLanguages(false));
61 $oGeocode->loadParamArray($oParams);
62
63 if ($oParams->getBool('search')) {
64     $oGeocode->setQuery($aCMDResult['search']);
65 } else {
66     $oGeocode->setQueryFromParams($oParams);
67 }
68
69 $aSearchResults = $oGeocode->lookup();
70
71 echo json_encode($aSearchResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)."\n";