3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
13 require_once(CONST_LibDir.'/Result.php');
18 protected $iMaxRank = 28;
21 public function __construct(&$oDB)
27 public function setZoom($iZoom)
29 // Zoom to rank, this could probably be calculated but a lookup gives fine control
31 0 => 2, // Continent / Sea
45 14 => 22, // Neighbourhood
47 16 => 26, // major street
48 17 => 27, // minor street
49 18 => 30, // or >, Building
50 19 => 30, // or >, Building
52 $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
56 * Find the closest interpolation with the given search diameter.
58 * @param string $sPointSQL Reverse geocoding point as SQL
59 * @param float $fSearchDiam Search diameter
61 * @return Record of the interpolation or null.
63 protected function lookupInterpolation($sPointSQL, $fSearchDiam)
65 Debug::newFunction('lookupInterpolation');
66 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
67 $sSQL .= ' (CASE WHEN endnumber != startnumber';
68 $sSQL .= ' THEN (endnumber - startnumber) * ST_LineLocatePoint(linegeo,'.$sPointSQL.')';
69 $sSQL .= ' ELSE startnumber END) as fhnr,';
70 $sSQL .= ' startnumber, endnumber, step,';
71 $sSQL .= ' ST_Distance(linegeo,'.$sPointSQL.') as distance';
72 $sSQL .= ' FROM location_property_osmline';
73 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
74 $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
75 $sSQL .= ' and parent_place_id != 0';
76 $sSQL .= ' ORDER BY distance ASC limit 1';
77 Debug::printSQL($sSQL);
79 return $this->oDB->getRow(
82 'Could not determine closest housenumber on an osm interpolation line.'
86 protected function lookupLargeArea($sPointSQL, $iMaxRank)
88 if(CONST_Search_WithinCountries
89 and $this->lookupInCountry($sPointSQL, $iMaxRank) == null){
94 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
96 return new Result($aPlace['place_id']);
100 // If no polygon which contains the searchpoint is found,
101 // searches in the country_osm_grid table for a polygon.
102 return $this->lookupInCountry($sPointSQL, $iMaxRank);
105 protected function lookupInCountry($sPointSQL, $iMaxRank)
107 Debug::newFunction('lookupInCountry');
108 // searches for polygon in table country_osm_grid which contains the searchpoint
109 // and searches for the nearest place node to the searchpoint in this polygon
110 $sSQL = 'SELECT country_code FROM country_osm_grid';
111 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
112 Debug::printSQL($sSQL);
114 $sCountryCode = $this->oDB->getOne(
117 'Could not determine country polygon containing the point.'
119 Debug::printVar('Country code', $sCountryCode);
123 // look for place nodes with the given country code
124 $sSQL = 'SELECT place_id FROM';
125 $sSQL .= ' (SELECT place_id, rank_search,';
126 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
127 $sSQL .= ' FROM placex';
128 $sSQL .= ' WHERE osm_type = \'N\'';
129 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
130 $sSQL .= ' AND rank_address between 4 and 25'; // needed to select right index
131 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
132 $sSQL .= ' AND type != \'postcode\'';
133 $sSQL .= ' AND name IS NOT NULL ';
134 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
135 $sSQL .= ' AND ST_Buffer(geometry, reverse_place_diameter(rank_search)) && '.$sPointSQL;
137 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
138 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
140 Debug::printSQL($sSQL);
142 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
143 Debug::printVar('Country node', $aPlace);
146 return new Result($aPlace['place_id']);
150 // still nothing, then return the country object
151 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
152 $sSQL .= ' FROM placex';
153 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
154 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
155 $sSQL .= ' AND class in (\'boundary\', \'place\')';
156 $sSQL .= ' AND linked_place_id is null';
157 $sSQL .= ' ORDER BY distance ASC';
158 Debug::printSQL($sSQL);
160 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
161 Debug::printVar('Country place', $aPlace);
163 return new Result($aPlace['place_id']);
171 * Search for areas or nodes for areas or nodes between state and suburb level.
173 * @param string $sPointSQL Search point as SQL string.
174 * @param int $iMaxRank Maximum address rank of the feature.
176 * @return Record of the found feature or null.
178 * Searches first for polygon that contains the search point.
179 * If such a polygon is found, place nodes with a higher rank are
180 * searched inside the polygon.
182 protected function lookupPolygon($sPointSQL, $iMaxRank)
184 Debug::newFunction('lookupPolygon');
185 // polygon search begins at suburb-level
186 if ($iMaxRank > 25) {
189 // no polygon search over country-level
193 // search for polygon
194 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
195 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
196 $sSQL .= ' FROM placex';
197 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
198 // Ensure that query planner doesn't use the index on rank_search.
199 $sSQL .= ' AND coalesce(rank_search, 0) between 5 and ' .$iMaxRank;
200 $sSQL .= ' AND rank_address between 4 and 25'; // needed for index selection
201 $sSQL .= ' AND geometry && '.$sPointSQL;
202 $sSQL .= ' AND type != \'postcode\' ';
203 $sSQL .= ' AND name is not null';
204 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
205 $sSQL .= ' ORDER BY rank_search DESC LIMIT 50 ) as a';
206 $sSQL .= ' WHERE ST_Contains(geometry, '.$sPointSQL.' )';
207 $sSQL .= ' ORDER BY rank_search DESC LIMIT 1';
208 Debug::printSQL($sSQL);
210 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
211 Debug::printVar('Polygon result', $aPoly);
214 // if a polygon is found, search for placenodes begins ...
215 $iRankAddress = $aPoly['rank_address'];
216 $iRankSearch = $aPoly['rank_search'];
217 $iPlaceID = $aPoly['place_id'];
219 if ($iRankSearch != $iMaxRank) {
220 $sSQL = 'SELECT place_id FROM ';
221 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
222 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
223 $sSQL .= ' FROM placex';
224 $sSQL .= ' WHERE osm_type = \'N\'';
225 $sSQL .= ' AND rank_search > '.$iRankSearch;
226 $sSQL .= ' AND rank_search <= '.$iMaxRank;
227 $sSQL .= ' AND rank_address between 4 and 25'; // needed to select right index
228 $sSQL .= ' AND type != \'postcode\'';
229 $sSQL .= ' AND name IS NOT NULL ';
230 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
231 $sSQL .= ' AND ST_Buffer(geometry, reverse_place_diameter(rank_search)) && '.$sPointSQL;
232 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
233 $sSQL .= ' limit 100) as a';
234 $sSQL .= ' WHERE ST_Contains((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
235 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
236 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
238 Debug::printSQL($sSQL);
240 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
241 Debug::printVar('Nearest place node', $aPlaceNode);
251 public function lookup($fLat, $fLon, $bDoInterpolation = true)
253 return $this->lookupPoint(
254 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
259 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
261 Debug::newFunction('lookupPoint');
262 // Find the nearest point
263 $fSearchDiam = 0.006;
267 // for POI or street level
268 if ($this->iMaxRank >= 26) {
269 // starts if the search is on POI or street level,
270 // searches for the nearest POI or street,
271 // if a street is found and a POI is searched for,
272 // the nearest POI which the found street is a parent of is chosen.
273 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
274 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
277 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
279 $sSQL .= ' rank_address between 26 and '.$this->iMaxRank;
280 $sSQL .= ' and (name is not null or housenumber is not null';
281 $sSQL .= ' or rank_address between 26 and 27)';
282 $sSQL .= ' and (rank_address between 26 and 27';
283 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
284 $sSQL .= ' and class not in (\'boundary\')';
285 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
286 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
287 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
288 $sSQL .= ' ORDER BY distance ASC limit 1';
289 Debug::printSQL($sSQL);
291 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
293 Debug::printVar('POI/street level result', $aPlace);
295 $iPlaceID = $aPlace['place_id'];
296 $oResult = new Result($iPlaceID);
297 $iRankAddress = $aPlace['rank_address'];
301 // if street and maxrank > streetlevel
302 if ($iRankAddress <= 27 && $this->iMaxRank > 27) {
303 // find the closest object (up to a certain radius) of which the street is a parent of
304 $sSQL = ' select place_id,';
305 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
309 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
310 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
311 $sSQL .= ' and rank_address > 28';
312 $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
313 $sSQL .= ' and (name is not null or housenumber is not null)';
314 $sSQL .= ' and class not in (\'boundary\')';
315 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
316 $sSQL .= ' ORDER BY distance ASC limit 1';
317 Debug::printSQL($sSQL);
319 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
320 Debug::printVar('Closest POI result', $aStreet);
324 $oResult = new Result($aStreet['place_id']);
329 // In the US we can check TIGER data for nearest housenumber
330 if (CONST_Use_US_Tiger_Data
331 && $iRankAddress <= 27
332 && $aPlace['country_code'] == 'us'
333 && $this->iMaxRank >= 28
335 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
336 $sSQL .= ' (endnumber - startnumber) * ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fhnr,';
337 $sSQL .= ' startnumber, endnumber, step,';
338 $sSQL .= ' ST_Distance('.$sPointSQL.', linegeo) as distance';
339 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
340 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
341 $sSQL .= ' ORDER BY distance ASC limit 1';
342 Debug::printSQL($sSQL);
344 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
345 Debug::printVar('Tiger house number result', $aPlaceTiger);
348 $aPlace = $aPlaceTiger;
349 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
350 $iRndNum = max(0, round($aPlaceTiger['fhnr'] / $aPlaceTiger['step']) * $aPlaceTiger['step']);
351 $oResult->iHouseNumber = $aPlaceTiger['startnumber'] + $iRndNum;
352 if ($oResult->iHouseNumber > $aPlaceTiger['endnumber']) {
353 $oResult->iHouseNumber = $aPlaceTiger['endnumber'];
360 if ($bDoInterpolation && $this->iMaxRank >= 30) {
361 $fDistance = $fSearchDiam;
363 // We can't reliably go from the closest street to an
364 // interpolation line because the closest interpolation
365 // may have a different street segments as a parent.
366 // Therefore allow an interpolation line to take precedence
367 // even when the street is closer.
368 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
371 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
372 Debug::printVar('Interpolation result', $aPlace);
375 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
376 $iRndNum = max(0, round($aHouse['fhnr'] / $aHouse['step']) * $aHouse['step']);
377 $oResult->iHouseNumber = $aHouse['startnumber'] + $iRndNum;
378 if ($oResult->iHouseNumber > $aHouse['endnumber']) {
379 $oResult->iHouseNumber = $aHouse['endnumber'];
386 // if no POI or street is found ...
387 $oResult = $this->lookupLargeArea($sPointSQL, 25);
390 // lower than street level ($iMaxRank < 26 )
391 $oResult = $this->lookupLargeArea($sPointSQL, $this->iMaxRank);
394 Debug::printVar('Final result', $oResult);