]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/website/lookup.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / website / lookup.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
11 require_once(CONST_LibDir.'/init-website.php');
12 require_once(CONST_LibDir.'/log.php');
13 require_once(CONST_LibDir.'/PlaceLookup.php');
14 require_once(CONST_LibDir.'/output.php');
15 ini_set('memory_limit', '200M');
16
17 $oParams = new Nominatim\ParameterParser();
18
19 // Format for output
20 $sOutputFormat = $oParams->getSet('format', array('xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
21 set_exception_handler_by_format($sOutputFormat);
22
23 // Preferred language
24 $aLangPrefOrder = $oParams->getPreferredLanguages();
25
26 $oDB = new Nominatim\DB(CONST_Database_DSN);
27 $oDB->connect();
28
29 $hLog = logStart($oDB, 'place', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
30
31 $aSearchResults = array();
32 $aCleanedQueryParts = array();
33
34 $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
35 $oPlaceLookup->loadParamArray($oParams);
36 $oPlaceLookup->setIncludeAddressDetails($oParams->getBool('addressdetails', true));
37
38 $aOsmIds = explode(',', $oParams->getString('osm_ids', ''));
39
40 if (count($aOsmIds) > CONST_Places_Max_ID_count) {
41     userError('Bulk User: Only ' . CONST_Places_Max_ID_count . ' ids are allowed in one request.');
42 }
43
44 foreach ($aOsmIds as $sItem) {
45     // Skip empty sItem
46     if (empty($sItem)) {
47         continue;
48     }
49
50     $sType = $sItem[0];
51     $iId = (int) substr($sItem, 1);
52     if ($iId > 0 && ($sType == 'N' || $sType == 'W' || $sType == 'R')) {
53         $aCleanedQueryParts[] = $sType . $iId;
54         $oPlace = $oPlaceLookup->lookupOSMID($sType, $iId);
55         if ($oPlace) {
56             // we want to use the search-* output templates, so we need to fill
57             // $aSearchResults and slightly change the (reverse search) oPlace
58             // key names
59             $oResult = $oPlace;
60             unset($oResult['aAddress']);
61             if (isset($oPlace['aAddress'])) {
62                 $oResult['address'] = $oPlace['aAddress'];
63             }
64             if ($sOutputFormat != 'geocodejson') {
65                 unset($oResult['langaddress']);
66                 $oResult['name'] = $oPlace['langaddress'];
67             }
68
69             $aOutlineResult = $oPlaceLookup->getOutlines(
70                 $oPlace['place_id'],
71                 $oPlace['lon'],
72                 $oPlace['lat'],
73                 Nominatim\ClassTypes\getDefRadius($oPlace)
74             );
75
76             if ($aOutlineResult) {
77                 $oResult = array_merge($oResult, $aOutlineResult);
78             }
79
80             $aSearchResults[] = $oResult;
81         }
82     }
83 }
84
85
86 if (CONST_Debug) {
87     exit;
88 }
89
90 $sXmlRootTag = 'lookupresults';
91 $sQuery = join(',', $aCleanedQueryParts);
92 // we initialize these to avoid warnings in our logfile
93 $sViewBox = '';
94 $bShowPolygons = '';
95 $aExcludePlaceIDs = array();
96 $sMoreURL = '';
97
98 logEnd($oDB, $hLog, 1);
99
100 $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
101 include(CONST_LibDir.'/template/search-'.$sOutputTemplate.'.php');