5 require_once(CONST_BasePath.'/lib/Result.php');
 
  10     protected $iMaxRank = 28;
 
  13     public function __construct(&$oDB)
 
  19     public function setZoom($iZoom)
 
  21         // Zoom to rank, this could probably be calculated but a lookup gives fine control
 
  23                       0 => 2, // Continent / Sea
 
  35                       12 => 18, // Town / Village
 
  39                       16 => 26, // Street, TODO: major street?
 
  41                       18 => 30, // or >, Building
 
  42                       19 => 30, // or >, Building
 
  44         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
 
  48      * Find the closest interpolation with the given search diameter.
 
  50      * @param string $sPointSQL   Reverse geocoding point as SQL
 
  51      * @param float  $fSearchDiam Search diameter
 
  53      * @return Record of the interpolation or null.
 
  55     protected function lookupInterpolation($sPointSQL, $fSearchDiam)
 
  57         $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
 
  58         $sSQL .= '  ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
 
  59         $sSQL .= '  startnumber, endnumber, interpolationtype,';
 
  60         $sSQL .= '  ST_Distance(linegeo,'.$sPointSQL.') as distance';
 
  61         $sSQL .= ' FROM location_property_osmline';
 
  62         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
 
  63         $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
 
  64         $sSQL .= ' ORDER BY distance ASC limit 1';
 
  67             $this->oDB->getRow($sSQL),
 
  68             'Could not determine closest housenumber on an osm interpolation line.'
 
  72     protected function lookupLargeArea($sPointSQL, $iMaxRank)
 
  77             $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
 
  79                 return new Result($aPlace['place_id']);
 
  83         // If no polygon which contains the searchpoint is found,
 
  84         // searches in the country_osm_grid table for a polygon.
 
  85         return  $this->lookupInCountry($sPointSQL, $iMaxRank);
 
  88     protected function lookupInCountry($sPointSQL, $iMaxRank)
 
  90         // searches for polygon in table country_osm_grid which contains the searchpoint
 
  91         // and searches for the nearest place node to the searchpoint in this polygon
 
  92         $sSQL = 'SELECT country_code FROM country_osm_grid';
 
  93         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
 
  95         $sCountryCode = chksql(
 
  96             $this->oDB->getOne($sSQL),
 
  97             'Could not determine country polygon containing the point.'
 
 101                 // look for place nodes with the given country code
 
 102                 $sSQL = 'SELECT place_id FROM';
 
 103                 $sSQL .= ' (SELECT place_id, rank_search,';
 
 104                 $sSQL .= '         ST_distance('.$sPointSQL.', geometry) as distance';
 
 105                 $sSQL .= ' FROM placex';
 
 106                 $sSQL .= ' WHERE osm_type = \'N\'';
 
 107                 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
 
 108                 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
 
 109                 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
 
 110                 $sSQL .= ' AND name IS NOT NULL ';
 
 111                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 112                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
 
 113                 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
 
 114                 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
 
 117                 if (CONST_Debug) var_dump($sSQL);
 
 119                     $this->oDB->getRow($sSQL),
 
 120                     'Could not determine place node.'
 
 123                     return new Result($aPlace['place_id']);
 
 127             // still nothing, then return the country object
 
 128             $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
 
 129             $sSQL .= ' FROM placex';
 
 130             $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
 
 131             $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
 
 132             $sSQL .= ' AND class in (\'boundary\',  \'place\')';
 
 133             $sSQL .= ' ORDER BY distance ASC';
 
 135             if (CONST_Debug) var_dump($sSQL);
 
 137                 $this->oDB->getRow($sSQL),
 
 138                 'Could not determine place node.'
 
 141                 return new Result($aPlace['place_id']);
 
 149      * Search for areas or nodes for areas or nodes between state and suburb level.
 
 151      * @param string $sPointSQL Search point as SQL string.
 
 152      * @param int    $iMaxRank  Maximum address rank of the feature.
 
 154      * @return Record of the found feature or null.
 
 156      * Searches first for polygon that contains the search point.
 
 157      * If such a polygon is found, place nodes with a higher rank are
 
 158      * searched inside the polygon.
 
 160     protected function lookupPolygon($sPointSQL, $iMaxRank)
 
 162         // polygon search begins at suburb-level
 
 163         if ($iMaxRank > 25) $iMaxRank = 25;
 
 164         // no polygon search over country-level
 
 165         if ($iMaxRank < 5) $iMaxRank = 5;
 
 166         // search for polygon
 
 167         $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
 
 168         $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
 
 169         $sSQL .= ' FROM placex';
 
 170         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
 
 171         $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
 
 172         $sSQL .= ' AND geometry && '.$sPointSQL;
 
 173         $sSQL .= ' AND type != \'postcode\' ';
 
 174         $sSQL .= ' AND name is not null';
 
 175         $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
 
 176         $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
 
 177         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
 
 178         $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
 
 181             $this->oDB->getRow($sSQL),
 
 182             'Could not determine polygon containing the point.'
 
 185         // if a polygon is found, search for placenodes begins ...
 
 186             $iParentPlaceID = $aPoly['parent_place_id'];
 
 187             $iRankAddress = $aPoly['rank_address'];
 
 188             $iRankSearch = $aPoly['rank_search'];
 
 189             $iPlaceID = $aPoly['place_id'];
 
 191             if ($iRankAddress != $iMaxRank) {
 
 192                 $sSQL = 'SELECT place_id FROM ';
 
 193                 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
 
 194                 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 195                 $sSQL .= ' FROM placex';
 
 196                 $sSQL .= ' WHERE osm_type = \'N\'';
 
 197                 // using rank_search because of a better differentiation
 
 198                 // for place nodes at rank_address 16
 
 199                 $sSQL .= ' AND rank_search > '.$iRankSearch;
 
 200                 $sSQL .= ' AND rank_search <= '.$iMaxRank;
 
 201                 $sSQL .= ' AND class = \'place\'';
 
 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 .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
 
 206                 $sSQL .= ' ORDER BY distance ASC,';
 
 207                 $sSQL .= ' rank_address DESC';
 
 208                 $sSQL .= ' limit 500) as a';
 
 209                 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
 
 210                 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
 
 211                 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
 
 214                 if (CONST_Debug) var_dump($sSQL);
 
 216                     $this->oDB->getRow($sSQL),
 
 217                     'Could not determine place node.'
 
 228     public function lookup($fLat, $fLon, $bDoInterpolation = true)
 
 230         return $this->lookupPoint(
 
 231             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
 
 236     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
 
 238         // starts if the search is on POI or street level,
 
 239         // searches for the nearest POI or street,
 
 240         // if a street is found and a POI is searched for,
 
 241         // the nearest POI which the found street is a parent of is choosen.
 
 242         $iMaxRank = $this->iMaxRank;
 
 244         // Find the nearest point
 
 245         $fSearchDiam = 0.006;
 
 249         // for POI or street level
 
 250         if ($iMaxRank >= 26) {
 
 251             $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
 
 252             $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 255             $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
 258             if ($iMaxRank == 26) {
 
 259                 $sSQL .= ' rank_address = 26';
 
 261                 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
 
 263             $sSQL .= ' and (name is not null or housenumber is not null';
 
 264             $sSQL .= ' or rank_address between 26 and 27)';
 
 265             $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
 266             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 267             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
 268             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
 269             $sSQL .= ' ORDER BY distance ASC limit 1';
 
 270             if (CONST_Debug) var_dump($sSQL);
 
 272                 $this->oDB->getRow($sSQL),
 
 273                 'Could not determine closest place.'
 
 276             if (CONST_Debug) var_dump($aPlace);
 
 278                 $iPlaceID = $aPlace['place_id'];
 
 279                 $oResult = new Result($iPlaceID);
 
 280                 $iRankAddress = $aPlace['rank_address'];
 
 281                 $iParentPlaceID = $aPlace['parent_place_id'];
 
 284             if ($bDoInterpolation && $iMaxRank >= 30) {
 
 285                 $fDistance = $fSearchDiam;
 
 287                     // We can't reliably go from the closest street to an
 
 288                     // interpolation line because the closest interpolation
 
 289                     // may have a different street segments as a parent.
 
 290                     // Therefore allow an interpolation line to take precendence
 
 291                     // even when the street is closer.
 
 292                     $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
 
 295                 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
 
 298                     $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
 
 299                     $oResult->iHouseNumber = closestHouseNumber($aHouse);
 
 306                 // if street and maxrank > streetlevel
 
 307                 if ($iRankAddress <= 27 && $iMaxRank > 27) {
 
 308                     // find the closest object (up to a certain radius) of which the street is a parent of
 
 309                     $sSQL = ' select place_id,';
 
 310                     $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 314                     $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
 
 315                     $sSQL .= ' AND parent_place_id = '.$iPlaceID;
 
 316                     $sSQL .= ' and rank_address != 28';
 
 317                     $sSQL .= ' and (name is not null or housenumber is not null)';
 
 318                     $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
 319                     $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 320                     $sSQL .= ' ORDER BY distance ASC limit 1';
 
 321                     if (CONST_Debug) var_dump($sSQL);
 
 323                         $this->oDB->getRow($sSQL),
 
 324                         'Could not determine closest place.'
 
 327                         if (CONST_Debug) var_dump($aStreet);
 
 328                         $oResult = new Result($aStreet['place_id']);
 
 332                   // In the US we can check TIGER data for nearest housenumber
 
 333                 if (CONST_Use_US_Tiger_Data
 
 334                     && $iRankAddress <= 27
 
 335                     && $aPlace['country_code'] == 'us'
 
 336                     && $this->iMaxRank >= 28
 
 338                     $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
 
 339                     $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
 
 340                     $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
 
 341                     $sSQL .= 'startnumber,endnumber,interpolationtype';
 
 342                     $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
 
 343                     $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
 
 344                     $sSQL .= ' ORDER BY distance ASC limit 1';
 
 345                     if (CONST_Debug) var_dump($sSQL);
 
 346                     $aPlaceTiger = chksql(
 
 347                         $this->oDB->getRow($sSQL),
 
 348                         'Could not determine closest Tiger place.'
 
 351                         if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
 
 352                         $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
 
 353                         $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
 
 357                 // if no POI or street is found ...
 
 358                 $oResult = $this->lookupLargeArea($sPointSQL, 25);
 
 361             // lower than street level ($iMaxRank < 26 )
 
 362             $oResult = $this->lookupLargeArea($sPointSQL, $iMaxRank);