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 .= '  (endnumber - startnumber) * ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fhnr,';
 
  68         $sSQL .= '  startnumber, endnumber, step,';
 
  69         $sSQL .= '  ST_Distance(linegeo,'.$sPointSQL.') as distance';
 
  70         $sSQL .= ' FROM location_property_osmline';
 
  71         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
 
  72         $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
 
  73         $sSQL .= ' ORDER BY distance ASC limit 1';
 
  74         Debug::printSQL($sSQL);
 
  76         return $this->oDB->getRow(
 
  79             'Could not determine closest housenumber on an osm interpolation line.'
 
  83     protected function lookupLargeArea($sPointSQL, $iMaxRank)
 
  86             $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
 
  88                 return new Result($aPlace['place_id']);
 
  92         // If no polygon which contains the searchpoint is found,
 
  93         // searches in the country_osm_grid table for a polygon.
 
  94         return  $this->lookupInCountry($sPointSQL, $iMaxRank);
 
  97     protected function lookupInCountry($sPointSQL, $iMaxRank)
 
  99         Debug::newFunction('lookupInCountry');
 
 100         // searches for polygon in table country_osm_grid which contains the searchpoint
 
 101         // and searches for the nearest place node to the searchpoint in this polygon
 
 102         $sSQL = 'SELECT country_code FROM country_osm_grid';
 
 103         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
 
 104         Debug::printSQL($sSQL);
 
 106         $sCountryCode = $this->oDB->getOne(
 
 109             'Could not determine country polygon containing the point.'
 
 111         Debug::printVar('Country code', $sCountryCode);
 
 115                 // look for place nodes with the given country code
 
 116                 $sSQL = 'SELECT place_id FROM';
 
 117                 $sSQL .= ' (SELECT place_id, rank_search,';
 
 118                 $sSQL .= '         ST_distance('.$sPointSQL.', geometry) as distance';
 
 119                 $sSQL .= ' FROM placex';
 
 120                 $sSQL .= ' WHERE osm_type = \'N\'';
 
 121                 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
 
 122                 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
 
 123                 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
 
 124                 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
 
 125                 $sSQL .= ' AND name IS NOT NULL ';
 
 126                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 127                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
 
 128                 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
 
 129                 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
 
 131                 Debug::printSQL($sSQL);
 
 133                 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
 
 134                 Debug::printVar('Country node', $aPlace);
 
 137                     return new Result($aPlace['place_id']);
 
 141             // still nothing, then return the country object
 
 142             $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
 
 143             $sSQL .= ' FROM placex';
 
 144             $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
 
 145             $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
 
 146             $sSQL .= ' AND class in (\'boundary\',  \'place\')';
 
 147             $sSQL .= ' AND linked_place_id is null';
 
 148             $sSQL .= ' ORDER BY distance ASC';
 
 149             Debug::printSQL($sSQL);
 
 151             $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
 
 152             Debug::printVar('Country place', $aPlace);
 
 154                 return new Result($aPlace['place_id']);
 
 162      * Search for areas or nodes for areas or nodes between state and suburb level.
 
 164      * @param string $sPointSQL Search point as SQL string.
 
 165      * @param int    $iMaxRank  Maximum address rank of the feature.
 
 167      * @return Record of the found feature or null.
 
 169      * Searches first for polygon that contains the search point.
 
 170      * If such a polygon is found, place nodes with a higher rank are
 
 171      * searched inside the polygon.
 
 173     protected function lookupPolygon($sPointSQL, $iMaxRank)
 
 175         Debug::newFunction('lookupPolygon');
 
 176         // polygon search begins at suburb-level
 
 177         if ($iMaxRank > 25) {
 
 180         // no polygon search over country-level
 
 184         // search for polygon
 
 185         $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
 
 186         $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
 
 187         $sSQL .= ' FROM placex';
 
 188         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
 
 189         $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
 
 190         $sSQL .= ' AND geometry && '.$sPointSQL;
 
 191         $sSQL .= ' AND type != \'postcode\' ';
 
 192         $sSQL .= ' AND name is not null';
 
 193         $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
 
 194         $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
 
 195         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
 
 196         $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
 
 197         Debug::printSQL($sSQL);
 
 199         $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
 
 200         Debug::printVar('Polygon result', $aPoly);
 
 203         // if a polygon is found, search for placenodes begins ...
 
 204             $iRankAddress = $aPoly['rank_address'];
 
 205             $iRankSearch = $aPoly['rank_search'];
 
 206             $iPlaceID = $aPoly['place_id'];
 
 208             if ($iRankAddress != $iMaxRank) {
 
 209                 $sSQL = 'SELECT place_id FROM ';
 
 210                 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
 
 211                 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 212                 $sSQL .= ' FROM placex';
 
 213                 $sSQL .= ' WHERE osm_type = \'N\'';
 
 214                 // using rank_search because of a better differentiation
 
 215                 // for place nodes at rank_address 16
 
 216                 $sSQL .= ' AND rank_search > '.$iRankSearch;
 
 217                 $sSQL .= ' AND rank_search <= '.$iMaxRank;
 
 218                 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
 
 219                 $sSQL .= ' AND rank_address > 0';
 
 220                 $sSQL .= ' AND class = \'place\'';
 
 221                 $sSQL .= ' AND type != \'postcode\'';
 
 222                 $sSQL .= ' AND name IS NOT NULL ';
 
 223                 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
 
 224                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
 
 225                 $sSQL .= ' ORDER BY distance ASC,';
 
 226                 $sSQL .= ' rank_address DESC';
 
 227                 $sSQL .= ' limit 500) as a';
 
 228                 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
 
 229                 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
 
 230                 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
 
 232                 Debug::printSQL($sSQL);
 
 234                 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
 
 235                 Debug::printVar('Nearest place node', $aPlaceNode);
 
 245     public function lookup($fLat, $fLon, $bDoInterpolation = true)
 
 247         return $this->lookupPoint(
 
 248             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
 
 253     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
 
 255         Debug::newFunction('lookupPoint');
 
 256         // Find the nearest point
 
 257         $fSearchDiam = 0.006;
 
 261         // for POI or street level
 
 262         if ($this->iMaxRank >= 26) {
 
 263             // starts if the search is on POI or street level,
 
 264             // searches for the nearest POI or street,
 
 265             // if a street is found and a POI is searched for,
 
 266             // the nearest POI which the found street is a parent of is choosen.
 
 267             $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
 
 268             $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 271             $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
 273             $sSQL .= ' rank_address between 26 and '.$this->iMaxRank;
 
 274             $sSQL .= ' and (name is not null or housenumber is not null';
 
 275             $sSQL .= ' or rank_address between 26 and 27)';
 
 276             $sSQL .= ' and (rank_address between 26 and 27';
 
 277             $sSQL .= '      or ST_GeometryType(geometry) != \'ST_LineString\')';
 
 278             $sSQL .= ' and class not in (\'boundary\')';
 
 279             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 280             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
 281             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
 282             $sSQL .= ' ORDER BY distance ASC limit 1';
 
 283             Debug::printSQL($sSQL);
 
 285             $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
 
 287             Debug::printVar('POI/street level result', $aPlace);
 
 289                 $iPlaceID = $aPlace['place_id'];
 
 290                 $oResult = new Result($iPlaceID);
 
 291                 $iRankAddress = $aPlace['rank_address'];
 
 295                 // if street and maxrank > streetlevel
 
 296                 if ($iRankAddress <= 27 && $this->iMaxRank > 27) {
 
 297                     // find the closest object (up to a certain radius) of which the street is a parent of
 
 298                     $sSQL = ' select place_id,';
 
 299                     $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 303                     $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
 
 304                     $sSQL .= ' AND parent_place_id = '.$iPlaceID;
 
 305                     $sSQL .= ' and rank_address > 28';
 
 306                     $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
 
 307                     $sSQL .= ' and (name is not null or housenumber is not null)';
 
 308                     $sSQL .= ' and class not in (\'boundary\')';
 
 309                     $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 310                     $sSQL .= ' ORDER BY distance ASC limit 1';
 
 311                     Debug::printSQL($sSQL);
 
 313                     $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
 
 314                     Debug::printVar('Closest POI result', $aStreet);
 
 318                         $oResult = new Result($aStreet['place_id']);
 
 323                   // In the US we can check TIGER data for nearest housenumber
 
 324                 if (CONST_Use_US_Tiger_Data
 
 325                     && $iRankAddress <= 27
 
 326                     && $aPlace['country_code'] == 'us'
 
 327                     && $this->iMaxRank >= 28
 
 329                     $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
 
 330                     $sSQL .= '      (endnumber - startnumber) * ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fhnr,';
 
 331                     $sSQL .= '      startnumber, endnumber, step,';
 
 332                     $sSQL .= '      ST_Distance('.$sPointSQL.', linegeo) as distance';
 
 333                     $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
 
 334                     $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
 
 335                     $sSQL .= ' ORDER BY distance ASC limit 1';
 
 336                     Debug::printSQL($sSQL);
 
 338                     $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
 
 339                     Debug::printVar('Tiger house number result', $aPlaceTiger);
 
 342                         $aPlace = $aPlaceTiger;
 
 343                         $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
 
 344                         $iRndNum = max(0, round($aPlaceTiger['fhnr'] / $aPlaceTiger['step']) * $aPlaceTiger['step']);
 
 345                         $oResult->iHouseNumber = $aPlaceTiger['startnumber'] + $iRndNum;
 
 346                         if ($oResult->iHouseNumber > $aPlaceTiger['endnumber']) {
 
 347                             $oResult->iHouseNumber = $aPlaceTiger['endnumber'];
 
 354             if ($bDoInterpolation && $this->iMaxRank >= 30) {
 
 355                 $fDistance = $fSearchDiam;
 
 357                     // We can't reliably go from the closest street to an
 
 358                     // interpolation line because the closest interpolation
 
 359                     // may have a different street segments as a parent.
 
 360                     // Therefore allow an interpolation line to take precendence
 
 361                     // even when the street is closer.
 
 362                     $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
 
 365                 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
 
 366                 Debug::printVar('Interpolation result', $aPlace);
 
 369                     $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
 
 370                     $iRndNum = max(0, round($aHouse['fhnr'] / $aHouse['step']) * $aHouse['step']);
 
 371                     $oResult->iHouseNumber = $aHouse['startnumber'] + $iRndNum;
 
 372                     if ($oResult->iHouseNumber > $aHouse['endnumber']) {
 
 373                         $oResult->iHouseNumber = $aHouse['endnumber'];
 
 380                 // if no POI or street is found ...
 
 381                 $oResult = $this->lookupLargeArea($sPointSQL, 25);
 
 384             // lower than street level ($iMaxRank < 26 )
 
 385             $oResult = $this->lookupLargeArea($sPointSQL, $this->iMaxRank);
 
 388         Debug::printVar('Final result', $oResult);