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;
73 // Find the nearest point
74 $fSearchDiam = 0.0004;
77 $fMaxAreaDistance = 1;
78 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
80 $fSearchDiam = $fSearchDiam * 2;
82 // If we have to expand the search area by a large amount then we need a larger feature
83 // then there is a limit to how small the feature should be
84 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
85 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
86 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
87 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
88 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
89 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
90 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
91 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
93 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
94 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
95 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
96 $sSQL .= ' and (name is not null or housenumber is not null)';
97 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
98 $sSQL .= ' and indexed_status = 0 ';
99 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
100 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
101 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
102 if (CONST_Debug) var_dump($sSQL);
103 $aPlace = $this->oDB->getRow($sSQL);
104 if (PEAR::IsError($aPlace))
106 failInternalError("Could not determine closest place.", $sSQL, $aPlace);
108 $iPlaceID = $aPlace['place_id'];
109 $iParentPlaceID = $aPlace['parent_place_id'];
112 // The point we found might be too small - use the address to find what it is a child of
113 if ($iPlaceID && $iMaxRank < 28)
115 if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
117 $iPlaceID = $iParentPlaceID;
119 $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";
120 $iPlaceID = $this->oDB->getOne($sSQL);
121 if (PEAR::IsError($iPlaceID))
123 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
127 $iPlaceID = $aPlace['place_id'];
131 $oPlaceLookup = new PlaceLookup($this->oDB);
132 $oPlaceLookup->setLanguagePreference($this->aLangPrefOrder);
133 $oPlaceLookup->setIncludeAddressDetails($this->bAddressDetails);
134 $oPlaceLookup->setPlaceId($iPlaceID);
136 return $oPlaceLookup->lookup();