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, // major street
40 17 => 27, // minor 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
53 * @return Record of the interpolation or null.
55 protected function lookupInterpolation($sPointSQL, $fSearchDiam)
57 Debug::newFunction('lookupInterpolation');
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 $sSQL .= ' ORDER BY distance ASC limit 1';
66 Debug::printSQL($sSQL);
68 return $this->oDB->getRow(
71 'Could not determine closest housenumber on an osm interpolation line.'
75 protected function lookupLargeArea($sPointSQL, $iMaxRank)
80 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
82 return new Result($aPlace['place_id']);
86 // If no polygon which contains the searchpoint is found,
87 // searches in the country_osm_grid table for a polygon.
88 return $this->lookupInCountry($sPointSQL, $iMaxRank);
91 protected function lookupInCountry($sPointSQL, $iMaxRank)
93 Debug::newFunction('lookupInCountry');
94 // searches for polygon in table country_osm_grid which contains the searchpoint
95 // and searches for the nearest place node to the searchpoint in this polygon
96 $sSQL = 'SELECT country_code FROM country_osm_grid';
97 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
98 Debug::printSQL($sSQL);
100 $sCountryCode = $this->oDB->getOne(
103 'Could not determine country polygon containing the point.'
105 Debug::printVar('Country code', $sCountryCode);
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, 1.8)) p ';
121 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
122 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
124 Debug::printSQL($sSQL);
126 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
127 Debug::printVar('Country node', $aPlace);
130 return new Result($aPlace['place_id']);
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 .= ' AND linked_place_id is null';
141 $sSQL .= ' ORDER BY distance ASC';
142 Debug::printSQL($sSQL);
144 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
145 Debug::printVar('Country place', $aPlace);
147 return new Result($aPlace['place_id']);
155 * Search for areas or nodes for areas or nodes between state and suburb level.
157 * @param string $sPointSQL Search point as SQL string.
158 * @param int $iMaxRank Maximum address rank of the feature.
160 * @return Record of the found feature or null.
162 * Searches first for polygon that contains the search point.
163 * If such a polygon is found, place nodes with a higher rank are
164 * searched inside the polygon.
166 protected function lookupPolygon($sPointSQL, $iMaxRank)
168 Debug::newFunction('lookupPolygon');
169 // polygon search begins at suburb-level
170 if ($iMaxRank > 25) $iMaxRank = 25;
171 // no polygon search over country-level
172 if ($iMaxRank < 5) $iMaxRank = 5;
173 // search for polygon
174 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
175 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
176 $sSQL .= ' FROM placex';
177 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
178 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
179 $sSQL .= ' AND geometry && '.$sPointSQL;
180 $sSQL .= ' AND type != \'postcode\' ';
181 $sSQL .= ' AND name is not null';
182 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
183 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
184 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
185 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
186 Debug::printSQL($sSQL);
188 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
189 Debug::printVar('Polygon result', $aPoly);
192 // if a polygon is found, search for placenodes begins ...
193 $iParentPlaceID = $aPoly['parent_place_id'];
194 $iRankAddress = $aPoly['rank_address'];
195 $iRankSearch = $aPoly['rank_search'];
196 $iPlaceID = $aPoly['place_id'];
198 if ($iRankAddress != $iMaxRank) {
199 $sSQL = 'SELECT place_id FROM ';
200 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
201 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
202 $sSQL .= ' FROM placex';
203 $sSQL .= ' WHERE osm_type = \'N\'';
204 // using rank_search because of a better differentiation
205 // for place nodes at rank_address 16
206 $sSQL .= ' AND rank_search > '.$iRankSearch;
207 $sSQL .= ' AND rank_search <= '.$iMaxRank;
208 $sSQL .= ' AND rank_address > 0';
209 $sSQL .= ' AND class = \'place\'';
210 $sSQL .= ' AND type != \'postcode\'';
211 $sSQL .= ' AND name IS NOT NULL ';
212 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
213 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
214 $sSQL .= ' ORDER BY distance ASC,';
215 $sSQL .= ' rank_address DESC';
216 $sSQL .= ' limit 500) as a';
217 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
218 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
219 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
221 Debug::printSQL($sSQL);
223 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
224 Debug::printVar('Nearest place node', $aPlaceNode);
234 public function lookup($fLat, $fLon, $bDoInterpolation = true)
236 return $this->lookupPoint(
237 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
242 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
244 Debug::newFunction('lookupPoint');
245 // starts if the search is on POI or street level,
246 // searches for the nearest POI or street,
247 // if a street is found and a POI is searched for,
248 // the nearest POI which the found street is a parent of is choosen.
249 $iMaxRank = $this->iMaxRank;
251 // Find the nearest point
252 $fSearchDiam = 0.006;
256 // for POI or street level
257 if ($iMaxRank >= 26) {
258 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
259 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
262 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
264 $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 (rank_address between 26 and 27';
268 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
269 $sSQL .= ' and class not in (\'boundary\')';
270 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
271 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
272 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
273 $sSQL .= ' ORDER BY distance ASC limit 1';
274 Debug::printSQL($sSQL);
276 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
278 Debug::printVar('POI/street level result', $aPlace);
280 $iPlaceID = $aPlace['place_id'];
281 $oResult = new Result($iPlaceID);
282 $iRankAddress = $aPlace['rank_address'];
283 $iParentPlaceID = $aPlace['parent_place_id'];
286 if ($bDoInterpolation && $iMaxRank >= 30) {
287 $fDistance = $fSearchDiam;
289 // We can't reliably go from the closest street to an
290 // interpolation line because the closest interpolation
291 // may have a different street segments as a parent.
292 // Therefore allow an interpolation line to take precendence
293 // even when the street is closer.
294 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
297 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
298 Debug::printVar('Interpolation result', $aPlace);
301 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
302 $oResult->iHouseNumber = closestHouseNumber($aHouse);
309 // if street and maxrank > streetlevel
310 if ($iRankAddress <= 27 && $iMaxRank > 27) {
311 // find the closest object (up to a certain radius) of which the street is a parent of
312 $sSQL = ' select place_id,';
313 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
317 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
318 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
319 $sSQL .= ' and rank_address > 28';
320 $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
321 $sSQL .= ' and (name is not null or housenumber is not null)';
322 $sSQL .= ' and class not in (\'boundary\')';
323 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
324 $sSQL .= ' ORDER BY distance ASC limit 1';
325 Debug::printSQL($sSQL);
327 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
328 Debug::printVar('Closest POI result', $aStreet);
331 $oResult = new Result($aStreet['place_id']);
335 // In the US we can check TIGER data for nearest housenumber
336 if (CONST_Use_US_Tiger_Data
337 && $iRankAddress <= 27
338 && $aPlace['country_code'] == 'us'
339 && $this->iMaxRank >= 28
341 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
342 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
343 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
344 $sSQL .= 'startnumber,endnumber,interpolationtype';
345 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
346 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
347 $sSQL .= ' ORDER BY distance ASC limit 1';
348 Debug::printSQL($sSQL);
350 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
351 Debug::printVar('Tiger house number result', $aPlaceTiger);
354 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
355 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
359 // if no POI or street is found ...
360 $oResult = $this->lookupLargeArea($sPointSQL, 25);
363 // lower than street level ($iMaxRank < 26 )
364 $oResult = $this->lookupLargeArea($sPointSQL, $iMaxRank);
367 Debug::printVar('Final result', $oResult);