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 between 5 and ' .min(25, $iMaxRank);
117 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
118 $sSQL .= ' AND name IS NOT NULL ';
119 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
120 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 5.0)) p ';
121 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
122 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
125 if (CONST_Debug) var_dump($sSQL);
127 $this->oDB->getRow($sSQL),
128 'Could not determine place node.'
134 // still nothing, then return the country object
135 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
136 $sSQL .= ' FROM placex';
137 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
138 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
139 $sSQL .= ' AND class in (\'boundary\', \'place\')';
140 $sSQL .= ' ORDER BY distance ASC';
142 if (CONST_Debug) var_dump($sSQL);
144 $this->oDB->getRow($sSQL),
145 'Could not determine place node.'
153 protected function lookupPolygon($sPointSQL, $iMaxRank)
155 // searches for polygon where the searchpoint is within
156 // if a polygon is found, placenodes with a higher rank are searched inside the polygon
158 // polygon search begins at suburb-level
159 if ($iMaxRank > 25) $iMaxRank = 25;
160 // no polygon search over country-level
161 if ($iMaxRank < 5) $iMaxRank = 5;
162 // search for polygon
163 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
164 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
165 $sSQL .= ' FROM placex';
166 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
167 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
168 $sSQL .= ' AND geometry && '.$sPointSQL;
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 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
173 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
174 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
177 $this->oDB->getRow($sSQL),
178 'Could not determine polygon containing the point.'
181 // if a polygon is found, search for placenodes begins ...
182 $iParentPlaceID = $aPoly['parent_place_id'];
183 $iRankAddress = $aPoly['rank_address'];
184 $iRankSearch = $aPoly['rank_search'];
185 $iPlaceID = $aPoly['place_id'];
187 if ($iRankAddress != $iMaxRank) {
188 $sSQL = 'SELECT place_id FROM ';
189 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
190 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
191 $sSQL .= ' FROM placex';
192 $sSQL .= ' WHERE osm_type = \'N\'';
193 // using rank_search because of a better differentiation
194 // for place nodes at rank_address 16
195 $sSQL .= ' AND rank_search > '.$iRankSearch;
196 $sSQL .= ' AND rank_search <= '.$iMaxRank;
197 $sSQL .= ' AND class = \'place\'';
198 $sSQL .= ' AND type != \'postcode\'';
199 $sSQL .= ' AND name IS NOT NULL ';
200 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
201 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
202 $sSQL .= ' ORDER BY distance ASC,';
203 $sSQL .= ' rank_address DESC';
204 $sSQL .= ' limit 500) as a';
205 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
206 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
207 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
210 if (CONST_Debug) var_dump($sSQL);
212 $this->oDB->getRow($sSQL),
213 'Could not determine place node.'
224 public function lookup($fLat, $fLon, $bDoInterpolation = true)
226 return $this->lookupPoint(
227 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
232 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
234 // starts if the search is on POI or street level,
235 // searches for the nearest POI or street,
236 // if a street is found and a POI is searched for,
237 // the nearest POI which the found street is a parent of is choosen.
238 $iMaxRank = $this->iMaxRank;
240 // Find the nearest point
241 $fSearchDiam = 0.006;
244 $fMaxAreaDistance = 1;
245 $bIsTigerStreet = false;
247 // for POI or street level
248 if ($iMaxRank >= 26) {
249 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
250 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
253 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
256 if ($iMaxRank == 26) {
257 $sSQL .= ' rank_address = 26';
259 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
261 $sSQL .= ' and (name is not null or housenumber is not null';
262 $sSQL .= ' or rank_address between 26 and 27)';
263 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
264 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
265 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
266 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
267 $sSQL .= ' ORDER BY distance ASC limit 1';
268 if (CONST_Debug) var_dump($sSQL);
270 $this->oDB->getRow($sSQL),
271 'Could not determine closest place.'
275 $iDistance = $aPlace['distance'];
276 $iPlaceID = $aPlace['place_id'];
277 $oResult = new Result($iPlaceID);
278 $iParentPlaceID = $aPlace['parent_place_id'];
280 if ($bDoInterpolation && $iMaxRank >= 30) {
281 if ($aPlace['rank_address'] <=27) {
284 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
287 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
288 $oResult->iHouseNumber = closestHouseNumber($aHouse);
292 // if street and maxrank > streetlevel
293 if (($aPlace['rank_address'] <=27)&& $iMaxRank > 27) {
294 // find the closest object (up to a certain radius) of which the street is a parent of
295 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
296 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
300 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
301 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
302 $sSQL .= ' and rank_address != 28';
303 $sSQL .= ' and (name is not null or housenumber is not null)';
304 $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
305 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
306 $sSQL .= ' ORDER BY distance ASC limit 1';
307 if (CONST_Debug) var_dump($sSQL);
309 $this->oDB->getRow($sSQL),
310 'Could not determine closest place.'
313 $iDistance = $aStreet['distance'];
314 $iPlaceID = $aStreet['place_id'];
315 $oResult = new Result($iPlaceID);
316 $iParentPlaceID = $aStreet['parent_place_id'];
318 if ($bDoInterpolation && $iMaxRank >= 30) {
319 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
322 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
323 $oResult->iHouseNumber = closestHouseNumber($aHouse);
329 // In the US we can check TIGER data for nearest housenumber
330 if (CONST_Use_US_Tiger_Data && $aPlace['country_code'] == 'us' && $this->iMaxRank >= 28) {
331 $fSearchDiam = $aPlace['rank_address'] > 28 ? $aPlace['distance'] : 0.001;
332 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
333 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
334 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
335 $sSQL .= 'startnumber,endnumber,interpolationtype';
336 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
337 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
338 $sSQL .= ' ORDER BY distance ASC limit 1';
339 if (CONST_Debug) var_dump($sSQL);
340 $aPlaceTiger = chksql(
341 $this->oDB->getRow($sSQL),
342 'Could not determine closest Tiger place.'
345 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
346 $aPlace = $aPlaceTiger;
347 $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
348 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
351 // if no POI or street is found ...
353 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
355 // lower than street level ($iMaxRank < 26 )
357 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);