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
52 * @param integer $iParentPlaceID Id of parent object
54 * @return Record of the interpolation or null.
56 protected function lookupInterpolation($sPointSQL, $fSearchDiam, $iParentPlaceID = null)
58 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
59 $sSQL .= ' ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
60 $sSQL .= ' startnumber, endnumber, interpolationtype,';
61 $sSQL .= ' ST_Distance(linegeo,'.$sPointSQL.') as distance';
62 $sSQL .= ' FROM location_property_osmline';
63 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
64 $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
65 if (isset($iParentPlaceID)) {
66 $sSQL .= ' and parent_place_id = '.$iParentPlaceID;
68 $sSQL .= ' ORDER BY distance ASC limit 1';
71 $this->oDB->getRow($sSQL),
72 'Could not determine closest housenumber on an osm interpolation line.'
76 protected function polygonFunctions($sPointSQL, $iMaxRank)
80 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
82 $oResult = new Result($aPlace['place_id']);
83 } elseif (!$aPlace && $iMaxRank > 4) {
84 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
86 $oResult = new Result($aPlace['place_id']);
92 protected function noPolygonFound($sPointSQL, $iMaxRank)
94 // searches for polygon in table country_osm_grid which contains the searchpoint
95 $sSQL = 'SELECT * FROM country_osm_grid';
96 $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
99 $this->oDB->getRow($sSQL),
100 'Could not determine polygon containing the point.'
103 $sCountryCode = $aPoly['country_code'];
105 $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
106 $sSQL .= ' FROM placex';
107 $sSQL .= ' WHERE osm_type = \'N\'';
108 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
109 $sSQL .= ' AND rank_address > 0';
110 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
111 $sSQL .= ' AND type != \'postcode\'';
112 $sSQL .= ' AND name IS NOT NULL ';
113 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
114 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
117 if (CONST_Debug) var_dump($sSQL);
119 $this->oDB->getRow($sSQL),
120 'Could not determine place node.'
128 protected function lookupPolygon($sPointSQL, $iMaxRank)
130 $sSQL = 'SELECT * FROM';
131 $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
132 $sSQL .= ' FROM placex';
133 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
134 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
135 $sSQL .= ' AND geometry && '.$sPointSQL;
136 $sSQL .= ' AND type != \'postcode\' ';
137 $sSQL .= ' AND name is not null';
138 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
139 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
140 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
141 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
144 $this->oDB->getRow($sSQL),
145 'Could not determine polygon containing the point.'
148 $iParentPlaceID = $aPoly['parent_place_id'];
149 $iRankAddress = $aPoly['rank_address'];
150 $iRankSearch = $aPoly['rank_search'];
151 $iPlaceID = $aPoly['place_id'];
153 if ($iRankAddress != $iMaxRank) {
156 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
157 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
158 $sSQL .= ' FROM placex';
159 $sSQL .= ' WHERE osm_type = \'N\'';
160 if ($iRankAddress = 16) {
161 // using rank_search because of a better differentiation for place nodes at rank_address 16
162 $sSQL .= ' AND rank_search > '.$iRankSearch;
163 $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
164 $sSQL .= ' AND class = \'place\'';
166 $sSQL .= ' AND rank_address > '.$iRankAddress;
167 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
169 $sSQL .= ' AND type != \'postcode\'';
170 $sSQL .= ' AND name IS NOT NULL ';
171 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
172 // preselection through bbox
173 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
174 $sSQL .= ' ORDER BY distance ASC,';
175 $sSQL .= ' rank_address DESC';
176 $sSQL .= ' limit 500) as a';
177 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
178 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
181 if (CONST_Debug) var_dump($sSQL);
183 $this->oDB->getRow($sSQL),
184 'Could not determine place node.'
195 public function lookup($fLat, $fLon, $bDoInterpolation = true)
197 return $this->lookupPoint(
198 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
203 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
206 $iMaxRank = $this->iMaxRank;
208 // Find the nearest point
209 $fSearchDiam = 0.006;
212 $fMaxAreaDistance = 1;
213 $bIsTigerStreet = false;
215 // for POI or street level
216 if ($iMaxRank >= 26) {
217 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
218 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
219 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
220 $sSQL .= ' END as distance';
223 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
226 if ($iMaxRank == 26) {
227 $sSQL .= ' rank_address = 26';
229 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
231 $sSQL .= ' and (name is not null or housenumber is not null';
232 $sSQL .= ' or rank_address between 26 and 27)';
233 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
234 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
235 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
236 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
237 $sSQL .= ' ORDER BY distance ASC limit 1';
238 if (CONST_Debug) var_dump($sSQL);
240 $this->oDB->getRow($sSQL),
241 'Could not determine closest place.'
246 $iDistance = $aPlace['distance'];
247 $iPlaceID = $aPlace['place_id'];
248 $oResult = new Result($iPlaceID);
249 $iParentPlaceID = $aPlace['parent_place_id'];
251 if ($bDoInterpolation && $iMaxRank >= 30) {
252 if ($aPlace['rank_address'] <=27) {
255 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
258 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
259 $oResult->iHouseNumber = closestHouseNumber($aHouse);
263 // if street and maxrank > streetlevel
264 if (($aPlace['rank_address'] <=27)&& $iMaxRank > 27) {
265 // find the closest object (up to a certain radius) of which the street is a parent of
266 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
267 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
271 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
272 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
273 $sSQL .= ' and rank_address != 28';
274 $sSQL .= ' and (name is not null or housenumber is not null)';
275 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
276 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
277 $sSQL .= ' ORDER BY distance ASC limit 1';
278 if (CONST_Debug) var_dump($sSQL);
280 $this->oDB->getRow($sSQL),
281 'Could not determine closest place.'
284 $iDistance = $aStreet['distance'];
285 $iPlaceID = $aStreet['place_id'];
286 $oResult = new Result($iPlaceID);
287 $iParentPlaceID = $aStreet['parent_place_id'];
289 if ($bDoInterpolation && $iMaxRank >= 30) {
290 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
293 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
294 $oResult->iHouseNumber = closestHouseNumber($aHouse);
300 // In the US we can check TIGER data for nearest housenumber
301 if (CONST_Use_US_Tiger_Data && $aPlace['country_code'] == 'us' && $this->iMaxRank >= 28) {
302 $fSearchDiam = $aPlace['rank_search'] > 28 ? $aPlace['distance'] : 0.001;
303 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
304 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
305 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
306 $sSQL .= 'startnumber,endnumber,interpolationtype';
307 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
308 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
309 $sSQL .= ' ORDER BY distance ASC limit 1';
310 if (CONST_Debug) var_dump($sSQL);
311 $aPlaceTiger = chksql(
312 $this->oDB->getRow($sSQL),
313 'Could not determine closest Tiger place.'
316 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
317 $aPlace = $aPlaceTiger;
318 $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
319 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
322 // if no POI or street is found ...
324 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
326 // lower than street level ($iMaxRank < 26 )
328 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);