8                 protected $iMaxRank = 28;
 
  10                 protected $aLangPrefOrder = array();
 
  12                 protected $bShowAddressDetails = true;
 
  14                 function ReverseGeocode(&$oDB)
 
  19                 function setLanguagePreference($aLangPref)
 
  21                         $this->aLangPrefOrder = $aLangPref;
 
  24                 function setIncludeAddressDetails($bAddressDetails = true)
 
  26                         $this->bAddressDetails = $bAddressDetails;
 
  29                 function setLatLon($fLat, $fLon)
 
  31                         $this->fLat = (float)$fLat;
 
  32                         $this->fLon = (float)$fLon;
 
  35                 function setRank($iRank)
 
  37                         $this->iMaxRank = $iRank;
 
  40                 function setZoom($iZoom)
 
  42                         // Zoom to rank, this could probably be calculated but a lookup gives fine control
 
  44                                 0 => 2, // Continent / Sea
 
  56                                 12 => 18, // Town / Village
 
  60                                 16 => 26, // Street, TODO: major street?
 
  62                                 18 => 30, // or >, Building
 
  63                                 19 => 30, // or >, Building
 
  65                         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
 
  70                         $sPointSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
 
  71                         $iMaxRank = $this->iMaxRank;
 
  72                         $iMaxRank_orig = $this->iMaxRank;
 
  74                         // Find the nearest point
 
  75                         $fSearchDiam = 0.0004;
 
  78                         $fMaxAreaDistance = 1;
 
  79                         $bIsInUnitedStates = false;
 
  80                         $bPlaceIsTiger = false;
 
  81                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
 
  83                                 $fSearchDiam = $fSearchDiam * 2;
 
  85                                 // If we have to expand the search area by a large amount then we need a larger feature
 
  86                                 // then there is a limit to how small the feature should be
 
  87                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
 
  88                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
 
  89                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
 
  90                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
 
  91                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
 
  92                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
 
  93                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
 
  94                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
 
  96                                 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code from placex';
 
  97                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
  98                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
 
  99                                 $sSQL .= ' and (name is not null or housenumber is not null)';
 
 100                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
 
 101                                 $sSQL .= ' and indexed_status = 0 ';
 
 102                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
 103                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
 104                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
 
 105                                 if (CONST_Debug) var_dump($sSQL);
 
 106                                 $aPlace = $this->oDB->getRow($sSQL);
 
 107                                 if (PEAR::IsError($aPlace))
 
 109                                         failInternalError("Could not determine closest place.", $sSQL, $aPlace);
 
 111                                 $iPlaceID = $aPlace['place_id'];
 
 112                                 $iParentPlaceID = $aPlace['parent_place_id'];
 
 113                                 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
 
 116                         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
 
 117                         if ($bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) 
 
 119                                 $fSearchDiam = 0.001;
 
 120                                 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search ';
 
 121                                 if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
 
 122                                 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
 
 123                                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.')';
 
 124                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', centroid) ASC limit 1';
 
 127                                 // print all house numbers in the parent (street)
 
 130                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
 
 133                                         $aAllHouses = $this->oDB->getAll($sSQL);
 
 134                                         foreach($aAllHouses as $i)
 
 136                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
 
 140                                 $aPlaceTiger = $this->oDB->getRow($sSQL);
 
 141                                 if (PEAR::IsError($aPlace))
 
 143                                         failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
 
 147                                         if (CONST_Debug) var_dump('found Tiger place', $aPlaceTiger);
 
 148                                         $bPlaceIsTiger = true;
 
 149                                         $aPlace = $aPlaceTiger;
 
 150                                         $iPlaceID = $aPlaceTiger['place_id'];
 
 151                                         $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
 
 155                         // The point we found might be too small - use the address to find what it is a child of
 
 156                         if ($iPlaceID && $iMaxRank < 28)
 
 158                                 if ($aPlace['rank_search'] > 28 && $iParentPlaceID && !$bPlaceIsTiger)
 
 160                                         $iPlaceID = $iParentPlaceID;
 
 162                                 $sSQL = "select address_place_id from place_addressline where place_id = $iPlaceID order by abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc limit 1";
 
 163                                 $iPlaceID = $this->oDB->getOne($sSQL);
 
 164                                 if (PEAR::IsError($iPlaceID))
 
 166                                         failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
 
 170                                         $iPlaceID = $aPlace['place_id'];
 
 174                         $oPlaceLookup = new PlaceLookup($this->oDB);
 
 175                         $oPlaceLookup->setLanguagePreference($this->aLangPrefOrder);
 
 176                         $oPlaceLookup->setIncludeAddressDetails($this->bAddressDetails);
 
 177                         $oPlaceLookup->setPlaceId($iPlaceID);
 
 178                         $oPlaceLookup->setIsTiger($bPlaceIsTiger);
 
 180                         return $oPlaceLookup->lookup();