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     public function lookup($fLat, $fLon, $bDoInterpolation = true)
 
  74         return $this->lookupPoint(
 
  75             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
 
  80     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
 
  82         $iMaxRank = $this->iMaxRank;
 
  84         // Find the nearest point
 
  85         $fSearchDiam = 0.0004;
 
  88         $fMaxAreaDistance = 1;
 
  89         $bIsTigerStreet = false;
 
  90         while ($oResult === null && $fSearchDiam < $fMaxAreaDistance) {
 
  91             $fSearchDiam = $fSearchDiam * 2;
 
  93             // If we have to expand the search area by a large amount then we need a larger feature
 
  94             // then there is a limit to how small the feature should be
 
  95             if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
 
  96             if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
 
  97             if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
 
  98             if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
 
  99             if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
 
 100             if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
 
 101             if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
 
 102             if ($fSearchDiam > 0.001 && $iMaxRank > 26) {
 
 103                 // try with interpolations before continuing
 
 104                 if ($bDoInterpolation) {
 
 105                     $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/2);
 
 108                         $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
 
 109                         $oResult->iHouseNumber = closestHouseNumber($aHouse);
 
 112                         $iParentPlaceID = $aHouse['parent_place_id']; // the street
 
 118                 // no interpolation found, continue search
 
 122             $sSQL = 'select place_id,parent_place_id,rank_search,country_code,';
 
 123             $sSQL .= '  ST_distance('.$sPointSQL.', geometry) as distance';
 
 125             if ($fSearchDiam < 0.01) {
 
 127                 $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
 130                 $sSQL .= ' (SELECT * FROM placex ';
 
 131                 $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
 132                 $sSQL .= '   LIMIT 1000) as p WHERE';
 
 134             $sSQL .= ' rank_search != 28 and rank_search >= '.$iMaxRank;
 
 135             $sSQL .= ' and (name is not null or housenumber is not null';
 
 136             $sSQL .= '      or rank_search between 26 and 27)';
 
 137             $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
 
 138             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
 
 139             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
 140             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
 141             $sSQL .= ' ORDER BY distance ASC limit 1';
 
 142             if (CONST_Debug) var_dump($sSQL);
 
 144                 $this->oDB->getRow($sSQL),
 
 145                 'Could not determine closest place.'
 
 148                 $oResult = new Result($aPlace['place_id']);
 
 149                 $iParentPlaceID = $aPlace['parent_place_id'];
 
 150                 if ($bDoInterpolation) {
 
 151                     if ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27) {
 
 152                         $bIsTigerStreet = ($aPlace['country_code'] == 'us');
 
 153                     } elseif ($aPlace['rank_search'] == 30) {
 
 154                         // If a house was found, make sure there isn't an
 
 155                         // interpolation line that is closer.
 
 156                         $aHouse = $this->lookupInterpolation(
 
 160                         if ($aHouse && $aPlace['distance'] < $aHouse['distance']) {
 
 161                             $oResult = new Result(
 
 163                                 Result::TABLE_OSMLINE
 
 165                             $oResult->iHouseNumber = closestHouseNumber($aHouse);
 
 168                             $iParentPlaceID = $aHouse['parent_place_id'];
 
 175         // Only street found? In the US we can check TIGER data for nearest housenumber
 
 176         if (CONST_Use_US_Tiger_Data && $bIsTigerStreet && $this->iMaxRank >= 28) {
 
 177             $fSearchDiam = $aPlace['rank_search'] > 28 ? $aPlace['distance'] : 0.001;
 
 178             $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
 
 179             $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
 
 180             $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
 
 181             $sSQL .= 'startnumber,endnumber,interpolationtype';
 
 182             $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
 
 183             $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
 
 184             $sSQL .= ' ORDER BY distance ASC limit 1';
 
 186             if (CONST_Debug) var_dump($sSQL);
 
 188             $aPlaceTiger = chksql(
 
 189                 $this->oDB->getRow($sSQL),
 
 190                 'Could not determine closest Tiger place.'
 
 193                 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
 
 194                 $aPlace = $aPlaceTiger;
 
 195                 $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
 
 196                 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
 
 197                 $iParentPlaceID = $aPlace['parent_place_id'];
 
 202         // The point we found might be too small - use the address to find what it is a child of
 
 203         if ($oResult !== null && $iMaxRank < 28) {
 
 204             if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
 
 205                 $iPlaceID = $iParentPlaceID;
 
 207                 $iPlaceID = $oResult->iId;
 
 209             $sSQL  = 'select a.address_place_id';
 
 210             $sSQL .= ' FROM place_addressline a, placex p';
 
 211             $sSQL .= " WHERE a.place_id = $iPlaceID and a.address_place_id = p.place_id";
 
 212             $sSQL .= '   AND p.linked_place_id is null';
 
 213             $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
 
 215             $iPlaceID = chksql($this->oDB->getOne($sSQL), 'Could not get parent for place.');
 
 217                 $oResult = new Result($iPlaceID);