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
43 12 => 18, // Town / Village
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)
89 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
91 return new Result($aPlace['place_id']);
95 // If no polygon which contains the searchpoint is found,
96 // searches in the country_osm_grid table for a polygon.
97 return $this->lookupInCountry($sPointSQL, $iMaxRank);
100 protected function lookupInCountry($sPointSQL, $iMaxRank)
102 Debug::newFunction('lookupInCountry');
103 // searches for polygon in table country_osm_grid which contains the searchpoint
104 // and searches for the nearest place node to the searchpoint in this polygon
105 $sSQL = 'SELECT country_code FROM country_osm_grid';
106 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
107 Debug::printSQL($sSQL);
109 $sCountryCode = $this->oDB->getOne(
112 'Could not determine country polygon containing the point.'
114 Debug::printVar('Country code', $sCountryCode);
118 // look for place nodes with the given country code
119 $sSQL = 'SELECT place_id FROM';
120 $sSQL .= ' (SELECT place_id, rank_search,';
121 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
122 $sSQL .= ' FROM placex';
123 $sSQL .= ' WHERE osm_type = \'N\'';
124 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
125 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
126 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
127 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
128 $sSQL .= ' AND name IS NOT NULL ';
129 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
130 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
131 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
132 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
134 Debug::printSQL($sSQL);
136 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
137 Debug::printVar('Country node', $aPlace);
140 return new Result($aPlace['place_id']);
144 // still nothing, then return the country object
145 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
146 $sSQL .= ' FROM placex';
147 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
148 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
149 $sSQL .= ' AND class in (\'boundary\', \'place\')';
150 $sSQL .= ' AND linked_place_id is null';
151 $sSQL .= ' ORDER BY distance ASC';
152 Debug::printSQL($sSQL);
154 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
155 Debug::printVar('Country place', $aPlace);
157 return new Result($aPlace['place_id']);
165 * Search for areas or nodes for areas or nodes between state and suburb level.
167 * @param string $sPointSQL Search point as SQL string.
168 * @param int $iMaxRank Maximum address rank of the feature.
170 * @return Record of the found feature or null.
172 * Searches first for polygon that contains the search point.
173 * If such a polygon is found, place nodes with a higher rank are
174 * searched inside the polygon.
176 protected function lookupPolygon($sPointSQL, $iMaxRank)
178 Debug::newFunction('lookupPolygon');
179 // polygon search begins at suburb-level
180 if ($iMaxRank > 25) {
183 // no polygon search over country-level
187 // search for polygon
188 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
189 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
190 $sSQL .= ' FROM placex';
191 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
192 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
193 $sSQL .= ' AND geometry && '.$sPointSQL;
194 $sSQL .= ' AND type != \'postcode\' ';
195 $sSQL .= ' AND name is not null';
196 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
197 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
198 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
199 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
200 Debug::printSQL($sSQL);
202 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
203 Debug::printVar('Polygon result', $aPoly);
206 // if a polygon is found, search for placenodes begins ...
207 $iRankAddress = $aPoly['rank_address'];
208 $iRankSearch = $aPoly['rank_search'];
209 $iPlaceID = $aPoly['place_id'];
211 if ($iRankAddress != $iMaxRank) {
212 $sSQL = 'SELECT place_id FROM ';
213 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
214 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
215 $sSQL .= ' FROM placex';
216 $sSQL .= ' WHERE osm_type = \'N\'';
217 // using rank_search because of a better differentiation
218 // for place nodes at rank_address 16
219 $sSQL .= ' AND rank_search > '.$iRankSearch;
220 $sSQL .= ' AND rank_search <= '.$iMaxRank;
221 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
222 $sSQL .= ' AND rank_address > 0';
223 $sSQL .= ' AND class = \'place\'';
224 $sSQL .= ' AND type != \'postcode\'';
225 $sSQL .= ' AND name IS NOT NULL ';
226 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
227 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
228 $sSQL .= ' ORDER BY distance ASC,';
229 $sSQL .= ' rank_address DESC';
230 $sSQL .= ' limit 500) as a';
231 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
232 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
233 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
235 Debug::printSQL($sSQL);
237 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
238 Debug::printVar('Nearest place node', $aPlaceNode);
248 public function lookup($fLat, $fLon, $bDoInterpolation = true)
250 return $this->lookupPoint(
251 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
256 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
258 Debug::newFunction('lookupPoint');
259 // Find the nearest point
260 $fSearchDiam = 0.006;
264 // for POI or street level
265 if ($this->iMaxRank >= 26) {
266 // starts if the search is on POI or street level,
267 // searches for the nearest POI or street,
268 // if a street is found and a POI is searched for,
269 // the nearest POI which the found street is a parent of is chosen.
270 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
271 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
274 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
276 $sSQL .= ' rank_address between 26 and '.$this->iMaxRank;
277 $sSQL .= ' and (name is not null or housenumber is not null';
278 $sSQL .= ' or rank_address between 26 and 27)';
279 $sSQL .= ' and (rank_address between 26 and 27';
280 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
281 $sSQL .= ' and class not in (\'boundary\')';
282 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
283 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
284 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
285 $sSQL .= ' ORDER BY distance ASC limit 1';
286 Debug::printSQL($sSQL);
288 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
290 Debug::printVar('POI/street level result', $aPlace);
292 $iPlaceID = $aPlace['place_id'];
293 $oResult = new Result($iPlaceID);
294 $iRankAddress = $aPlace['rank_address'];
298 // if street and maxrank > streetlevel
299 if ($iRankAddress <= 27 && $this->iMaxRank > 27) {
300 // find the closest object (up to a certain radius) of which the street is a parent of
301 $sSQL = ' select place_id,';
302 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
306 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
307 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
308 $sSQL .= ' and rank_address > 28';
309 $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
310 $sSQL .= ' and (name is not null or housenumber is not null)';
311 $sSQL .= ' and class not in (\'boundary\')';
312 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
313 $sSQL .= ' ORDER BY distance ASC limit 1';
314 Debug::printSQL($sSQL);
316 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
317 Debug::printVar('Closest POI result', $aStreet);
321 $oResult = new Result($aStreet['place_id']);
326 // In the US we can check TIGER data for nearest housenumber
327 if (CONST_Use_US_Tiger_Data
328 && $iRankAddress <= 27
329 && $aPlace['country_code'] == 'us'
330 && $this->iMaxRank >= 28
332 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
333 $sSQL .= ' (endnumber - startnumber) * ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fhnr,';
334 $sSQL .= ' startnumber, endnumber, step,';
335 $sSQL .= ' ST_Distance('.$sPointSQL.', linegeo) as distance';
336 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
337 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
338 $sSQL .= ' ORDER BY distance ASC limit 1';
339 Debug::printSQL($sSQL);
341 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
342 Debug::printVar('Tiger house number result', $aPlaceTiger);
345 $aPlace = $aPlaceTiger;
346 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
347 $iRndNum = max(0, round($aPlaceTiger['fhnr'] / $aPlaceTiger['step']) * $aPlaceTiger['step']);
348 $oResult->iHouseNumber = $aPlaceTiger['startnumber'] + $iRndNum;
349 if ($oResult->iHouseNumber > $aPlaceTiger['endnumber']) {
350 $oResult->iHouseNumber = $aPlaceTiger['endnumber'];
357 if ($bDoInterpolation && $this->iMaxRank >= 30) {
358 $fDistance = $fSearchDiam;
360 // We can't reliably go from the closest street to an
361 // interpolation line because the closest interpolation
362 // may have a different street segments as a parent.
363 // Therefore allow an interpolation line to take precedence
364 // even when the street is closer.
365 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
368 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
369 Debug::printVar('Interpolation result', $aPlace);
372 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
373 $iRndNum = max(0, round($aHouse['fhnr'] / $aHouse['step']) * $aHouse['step']);
374 $oResult->iHouseNumber = $aHouse['startnumber'] + $iRndNum;
375 if ($oResult->iHouseNumber > $aHouse['endnumber']) {
376 $oResult->iHouseNumber = $aHouse['endnumber'];
383 // if no POI or street is found ...
384 $oResult = $this->lookupLargeArea($sPointSQL, 25);
387 // lower than street level ($iMaxRank < 26 )
388 $oResult = $this->lookupLargeArea($sPointSQL, $this->iMaxRank);
391 Debug::printVar('Final result', $oResult);