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)
78 // starts the nopolygonFound function if no polygon is found with the lookupPolygon function
81 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
83 $oResult = new Result($aPlace['place_id']);
84 // if no polygon which contains the searchpoint is found,
85 // the noPolygonFound function searches in the country_osm_grid table for a polygon
86 } elseif (!$aPlace && $iMaxRank > 4) {
87 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
89 $oResult = new Result($aPlace['place_id']);
95 protected function noPolygonFound($sPointSQL, $iMaxRank)
97 // searches for polygon in table country_osm_grid which contains the searchpoint
98 // and searches for the nearest place node to the searchpoint in this polygon
99 $sSQL = 'SELECT country_code FROM country_osm_grid';
100 $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.') limit 1';
103 $this->oDB->getRow($sSQL),
104 'Could not determine polygon containing the point.'
107 $sCountryCode = $aPoly['country_code'];
109 // look for place nodes with the given country code
110 $sSQL = 'SELECT place_id FROM';
111 $sSQL .= ' (SELECT place_id, rank_search,';
112 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
113 $sSQL .= ' FROM placex';
114 $sSQL .= ' WHERE osm_type = \'N\'';
115 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
116 $sSQL .= ' AND rank_search > 4';
117 $sSQL .= ' AND rank_search <= ' .min(25, $iMaxRank);
118 $sSQL .= ' AND type != \'postcode\'';
119 $sSQL .= ' AND name IS NOT NULL ';
120 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
121 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 5.0)) p ';
122 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
123 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
126 if (CONST_Debug) var_dump($sSQL);
128 $this->oDB->getRow($sSQL),
129 'Could not determine place node.'
135 // still nothing, then return the country object
136 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
137 $sSQL .= ' FROM placex';
138 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
139 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
140 $sSQL .= ' AND class in (\'boundary\', \'place\')';
141 $sSQL .= ' ORDER BY distance ASC';
143 if (CONST_Debug) var_dump($sSQL);
145 $this->oDB->getRow($sSQL),
146 'Could not determine place node.'
154 protected function lookupPolygon($sPointSQL, $iMaxRank)
156 // searches for polygon where the searchpoint is within
157 // if a polygon is found, placenodes with a higher rank are searched inside the polygon
159 // polygon search begins at suburb-level
160 if ($iMaxRank > 25) $iMaxRank = 25;
161 // no polygon search over country-level
162 if ($iMaxRank < 5) $iMaxRank = 5;
163 // search for polygon
164 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
165 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
166 $sSQL .= ' FROM placex';
167 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
168 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
169 $sSQL .= ' AND geometry && '.$sPointSQL;
170 $sSQL .= ' AND type != \'postcode\' ';
171 $sSQL .= ' AND name is not null';
172 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
173 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
174 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
175 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
178 $this->oDB->getRow($sSQL),
179 'Could not determine polygon containing the point.'
182 // if a polygon is found, search for placenodes begins ...
183 $iParentPlaceID = $aPoly['parent_place_id'];
184 $iRankAddress = $aPoly['rank_address'];
185 $iRankSearch = $aPoly['rank_search'];
186 $iPlaceID = $aPoly['place_id'];
188 if ($iRankAddress != $iMaxRank) {
189 $sSQL = 'SELECT place_id FROM ';
190 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
191 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
192 $sSQL .= ' FROM placex';
193 $sSQL .= ' WHERE osm_type = \'N\'';
194 // using rank_search because of a better differentiation
195 // for place nodes at rank_address 16
196 $sSQL .= ' AND rank_search > '.$iRankSearch;
197 $sSQL .= ' AND rank_search <= ' .$iMaxRank;
198 $sSQL .= ' AND class = \'place\'';
199 $sSQL .= ' AND type != \'postcode\'';
200 $sSQL .= ' AND name IS NOT NULL ';
201 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
202 // preselection through bbox
203 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
204 $sSQL .= ' ORDER BY distance ASC,';
205 $sSQL .= ' rank_address DESC';
206 $sSQL .= ' limit 500) as a';
207 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
208 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
209 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
212 if (CONST_Debug) var_dump($sSQL);
214 $this->oDB->getRow($sSQL),
215 'Could not determine place node.'
226 public function lookup($fLat, $fLon, $bDoInterpolation = true)
228 return $this->lookupPoint(
229 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
234 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
236 // starts if the search is on POI or street level,
237 // searches for the nearest POI or street,
238 // if a street is found and a POI is searched for,
239 // the nearest POI which the found street is a parent of is choosen.
240 $iMaxRank = $this->iMaxRank;
242 // Find the nearest point
243 $fSearchDiam = 0.006;
246 $fMaxAreaDistance = 1;
247 $bIsTigerStreet = false;
249 // for POI or street level
250 if ($iMaxRank >= 26) {
251 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
252 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
253 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
254 $sSQL .= ' END as distance';
257 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
260 if ($iMaxRank == 26) {
261 $sSQL .= ' rank_address = 26';
263 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
265 $sSQL .= ' and (name is not null or housenumber is not null';
266 $sSQL .= ' or rank_address between 26 and 27)';
267 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
268 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
269 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
270 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
271 $sSQL .= ' ORDER BY distance ASC limit 1';
272 if (CONST_Debug) var_dump($sSQL);
274 $this->oDB->getRow($sSQL),
275 'Could not determine closest place.'
279 $iDistance = $aPlace['distance'];
280 $iPlaceID = $aPlace['place_id'];
281 $oResult = new Result($iPlaceID);
282 $iParentPlaceID = $aPlace['parent_place_id'];
284 if ($bDoInterpolation && $iMaxRank >= 30) {
285 if ($aPlace['rank_address'] <=27) {
288 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
291 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
292 $oResult->iHouseNumber = closestHouseNumber($aHouse);
296 // if street and maxrank > streetlevel
297 if (($aPlace['rank_address'] <=27)&& $iMaxRank > 27) {
298 // find the closest object (up to a certain radius) of which the street is a parent of
299 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
300 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
304 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
305 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
306 $sSQL .= ' and rank_address != 28';
307 $sSQL .= ' and (name is not null or housenumber is not null)';
308 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
309 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
310 $sSQL .= ' ORDER BY distance ASC limit 1';
311 if (CONST_Debug) var_dump($sSQL);
313 $this->oDB->getRow($sSQL),
314 'Could not determine closest place.'
317 $iDistance = $aStreet['distance'];
318 $iPlaceID = $aStreet['place_id'];
319 $oResult = new Result($iPlaceID);
320 $iParentPlaceID = $aStreet['parent_place_id'];
322 if ($bDoInterpolation && $iMaxRank >= 30) {
323 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
326 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
327 $oResult->iHouseNumber = closestHouseNumber($aHouse);
333 // In the US we can check TIGER data for nearest housenumber
334 if (CONST_Use_US_Tiger_Data && $aPlace['country_code'] == 'us' && $this->iMaxRank >= 28) {
335 $fSearchDiam = $aPlace['rank_address'] > 28 ? $aPlace['distance'] : 0.001;
336 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
337 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
338 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
339 $sSQL .= 'startnumber,endnumber,interpolationtype';
340 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
341 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
342 $sSQL .= ' ORDER BY distance ASC limit 1';
343 if (CONST_Debug) var_dump($sSQL);
344 $aPlaceTiger = chksql(
345 $this->oDB->getRow($sSQL),
346 'Could not determine closest Tiger place.'
349 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
350 $aPlace = $aPlaceTiger;
351 $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
352 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
355 // if no POI or street is found ...
357 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
359 // lower than street level ($iMaxRank < 26 )
361 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);