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 lookupPolygon($sPointSQL, $iMaxRank)
 
  78         $sSQL = 'SELECT * FROM';
 
  79         $sSQL .= '(select place_id,parent_place_id,rank_address,country_code, geometry';
 
  80         $sSQL .= ' FROM placex';
 
  81         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
 
  82         $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
 
  83         $sSQL .= ' AND geometry && '.$sPointSQL;
 
  84         $sSQL .= ' AND type != \'postcode\' ';
 
  85         $sSQL .= ' AND name is not null';
 
  86         $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
 
  87         $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
 
  88         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
 
  89         $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
 
  92             $this->oDB->getRow($sSQL),
 
  93             'Could not determine polygon containing the point.'
 
  96             $iParentPlaceID = $aPoly['parent_place_id'];
 
  97             $iRankAddress = $aPoly['rank_address'];
 
  98             $iPlaceID = $aPoly['place_id'];
 
 100             if ($iRankAddress != $iMaxRank) {
 
 103                 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
 
 104                 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 105                 $sSQL .= ' FROM placex';
 
 106                 $sSQL .= ' WHERE osm_type = \'N\'';
 
 107                 $sSQL .= ' AND rank_address > '.$iRankAddress;
 
 108                 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
 
 109                 $sSQL .= ' AND type != \'postcode\'';
 
 110                 $sSQL .= ' AND name IS NOT NULL ';
 
 111                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 112                 // preselection through bbox
 
 113                 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
 
 114                 $sSQL .= ' ORDER BY distance ASC,';
 
 115                 $sSQL .= ' rank_address DESC';
 
 116                 $sSQL .= ' limit 500) as a';
 
 117                 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
 
 118                 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
 
 121                 if (CONST_Debug) var_dump($sSQL);
 
 123                     $this->oDB->getRow($sSQL),
 
 124                     'Could not determine place node.'
 
 134     public function lookup($fLat, $fLon, $bDoInterpolation = true)
 
 136         return $this->lookupPoint(
 
 137             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
 
 142     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
 
 145         $iMaxRank = $this->iMaxRank;
 
 147         // Find the nearest point
 
 148         $fSearchDiam = 0.006;
 
 151         $fMaxAreaDistance = 1;
 
 152         $bIsTigerStreet = false;
 
 154         // try with interpolations before continuing
 
 155         if ($bDoInterpolation && $iMaxRank >= 30 ) {
 
 156             $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/3);
 
 159                 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
 
 160                 $oResult->iHouseNumber = closestHouseNumber($aHouse);
 
 163                 $iParentPlaceID = $aHouse['parent_place_id']; // the street
 
 168         }// no interpolation found, continue search
 
 170         // for POI or street level
 
 171         if ($iMaxRank >= 26) {
 
 172             $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
 
 173             $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
 
 174             $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
 
 175             $sSQL .= ' END as distance';
 
 178             $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
 181             if ($iMaxRank == 26) {
 
 182                 $sSQL .= ' rank_address = 26';
 
 184                 $sSQL .= ' rank_address >= 26';
 
 186             $sSQL .= ' and (name is not null or housenumber is not null';
 
 187             $sSQL .= ' or rank_address between 26 and 27)';
 
 188             $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
 189             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 190             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
 191             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
 192             $sSQL .= ' ORDER BY distance ASC limit 1';
 
 193             if (CONST_Debug) var_dump($sSQL);
 
 195                 $this->oDB->getRow($sSQL),
 
 196                 'Could not determine closest place.'
 
 200                     $iPlaceID = $aPlace['place_id'];
 
 201                     $oResult = new Result($iPlaceID);
 
 202                     $iParentPlaceID = $aPlace['parent_place_id'];
 
 203                     // if street and maxrank > streetlevel
 
 204                 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
 
 205                     // find the closest object (up to a certain radius) of which the street is a parent of
 
 206                     $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
 
 207                     $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
 
 211                     $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
 
 212                     $sSQL .= ' AND parent_place_id = '.$iPlaceID;
 
 213                     $sSQL .= ' and rank_address != 28';
 
 214                     $sSQL .= ' and (name is not null or housenumber is not null';
 
 215                     $sSQL .= ' or rank_address between 26 and 27)';
 
 216                     $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
 217                     $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 218                     $sSQL .= ' ORDER BY distance ASC limit 1';
 
 219                     if (CONST_Debug) var_dump($sSQL);
 
 221                         $this->oDB->getRow($sSQL),
 
 222                         'Could not determine closest place.'
 
 225                         $iPlaceID = $aStreet['place_id'];
 
 226                         $oResult = new Result($iPlaceID);
 
 227                         $iParentPlaceID = $aStreet['parent_place_id'];
 
 231                 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
 
 233                     $oResult = new Result($aPlace['place_id']);
 
 236             // lower than street level ($iMaxRank < 26 )
 
 238             $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
 
 240                 $oResult = new Result($aPlace['place_id']);