5 require_once(CONST_LibDir.'/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)
78 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
80 return new Result($aPlace['place_id']);
84 // If no polygon which contains the searchpoint is found,
85 // searches in the country_osm_grid table for a polygon.
86 return $this->lookupInCountry($sPointSQL, $iMaxRank);
89 protected function lookupInCountry($sPointSQL, $iMaxRank)
91 Debug::newFunction('lookupInCountry');
92 // searches for polygon in table country_osm_grid which contains the searchpoint
93 // and searches for the nearest place node to the searchpoint in this polygon
94 $sSQL = 'SELECT country_code FROM country_osm_grid';
95 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
96 Debug::printSQL($sSQL);
98 $sCountryCode = $this->oDB->getOne(
101 'Could not determine country polygon containing the point.'
103 Debug::printVar('Country code', $sCountryCode);
107 // look for place nodes with the given country code
108 $sSQL = 'SELECT place_id FROM';
109 $sSQL .= ' (SELECT place_id, rank_search,';
110 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
111 $sSQL .= ' FROM placex';
112 $sSQL .= ' WHERE osm_type = \'N\'';
113 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
114 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
115 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
116 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
117 $sSQL .= ' AND name IS NOT NULL ';
118 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
119 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
120 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
121 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
123 Debug::printSQL($sSQL);
125 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
126 Debug::printVar('Country node', $aPlace);
129 return new Result($aPlace['place_id']);
133 // still nothing, then return the country object
134 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
135 $sSQL .= ' FROM placex';
136 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
137 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
138 $sSQL .= ' AND class in (\'boundary\', \'place\')';
139 $sSQL .= ' AND linked_place_id is null';
140 $sSQL .= ' ORDER BY distance ASC';
141 Debug::printSQL($sSQL);
143 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
144 Debug::printVar('Country place', $aPlace);
146 return new Result($aPlace['place_id']);
154 * Search for areas or nodes for areas or nodes between state and suburb level.
156 * @param string $sPointSQL Search point as SQL string.
157 * @param int $iMaxRank Maximum address rank of the feature.
159 * @return Record of the found feature or null.
161 * Searches first for polygon that contains the search point.
162 * If such a polygon is found, place nodes with a higher rank are
163 * searched inside the polygon.
165 protected function lookupPolygon($sPointSQL, $iMaxRank)
167 Debug::newFunction('lookupPolygon');
168 // polygon search begins at suburb-level
169 if ($iMaxRank > 25) {
172 // no polygon search over country-level
176 // search for polygon
177 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
178 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
179 $sSQL .= ' FROM placex';
180 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
181 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
182 $sSQL .= ' AND geometry && '.$sPointSQL;
183 $sSQL .= ' AND type != \'postcode\' ';
184 $sSQL .= ' AND name is not null';
185 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
186 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
187 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
188 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
189 Debug::printSQL($sSQL);
191 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
192 Debug::printVar('Polygon result', $aPoly);
195 // if a polygon is found, search for placenodes begins ...
196 $iRankAddress = $aPoly['rank_address'];
197 $iRankSearch = $aPoly['rank_search'];
198 $iPlaceID = $aPoly['place_id'];
200 if ($iRankAddress != $iMaxRank) {
201 $sSQL = 'SELECT place_id FROM ';
202 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
203 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
204 $sSQL .= ' FROM placex';
205 $sSQL .= ' WHERE osm_type = \'N\'';
206 // using rank_search because of a better differentiation
207 // for place nodes at rank_address 16
208 $sSQL .= ' AND rank_search > '.$iRankSearch;
209 $sSQL .= ' AND rank_search <= '.$iMaxRank;
210 $sSQL .= ' AND rank_search < 26 '; // needed to select right index
211 $sSQL .= ' AND rank_address > 0';
212 $sSQL .= ' AND class = \'place\'';
213 $sSQL .= ' AND type != \'postcode\'';
214 $sSQL .= ' AND name IS NOT NULL ';
215 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
216 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
217 $sSQL .= ' ORDER BY distance ASC,';
218 $sSQL .= ' rank_address DESC';
219 $sSQL .= ' limit 500) as a';
220 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
221 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
222 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
224 Debug::printSQL($sSQL);
226 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
227 Debug::printVar('Nearest place node', $aPlaceNode);
237 public function lookup($fLat, $fLon, $bDoInterpolation = true)
239 return $this->lookupPoint(
240 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
245 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
247 Debug::newFunction('lookupPoint');
248 // Find the nearest point
249 $fSearchDiam = 0.006;
253 // for POI or street level
254 if ($this->iMaxRank >= 26) {
255 // starts if the search is on POI or street level,
256 // searches for the nearest POI or street,
257 // if a street is found and a POI is searched for,
258 // the nearest POI which the found street is a parent of is choosen.
259 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
260 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
263 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
265 $sSQL .= ' rank_address between 26 and '.$this->iMaxRank;
266 $sSQL .= ' and (name is not null or housenumber is not null';
267 $sSQL .= ' or rank_address between 26 and 27)';
268 $sSQL .= ' and (rank_address between 26 and 27';
269 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
270 $sSQL .= ' and class not in (\'boundary\')';
271 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
272 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
273 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
274 $sSQL .= ' ORDER BY distance ASC limit 1';
275 Debug::printSQL($sSQL);
277 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
279 Debug::printVar('POI/street level result', $aPlace);
281 $iPlaceID = $aPlace['place_id'];
282 $oResult = new Result($iPlaceID);
283 $iRankAddress = $aPlace['rank_address'];
287 // if street and maxrank > streetlevel
288 if ($iRankAddress <= 27 && $this->iMaxRank > 27) {
289 // find the closest object (up to a certain radius) of which the street is a parent of
290 $sSQL = ' select place_id,';
291 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
295 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
296 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
297 $sSQL .= ' and rank_address > 28';
298 $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
299 $sSQL .= ' and (name is not null or housenumber is not null)';
300 $sSQL .= ' and class not in (\'boundary\')';
301 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
302 $sSQL .= ' ORDER BY distance ASC limit 1';
303 Debug::printSQL($sSQL);
305 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
306 Debug::printVar('Closest POI result', $aStreet);
310 $oResult = new Result($aStreet['place_id']);
315 // In the US we can check TIGER data for nearest housenumber
316 if (CONST_Use_US_Tiger_Data
317 && $iRankAddress <= 27
318 && $aPlace['country_code'] == 'us'
319 && $this->iMaxRank >= 28
321 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
322 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
323 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
324 $sSQL .= 'startnumber,endnumber,interpolationtype';
325 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
326 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
327 $sSQL .= ' ORDER BY distance ASC limit 1';
328 Debug::printSQL($sSQL);
330 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
331 Debug::printVar('Tiger house number result', $aPlaceTiger);
334 $aPlace = $aPlaceTiger;
335 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
336 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
342 if ($bDoInterpolation && $this->iMaxRank >= 30) {
343 $fDistance = $fSearchDiam;
345 // We can't reliably go from the closest street to an
346 // interpolation line because the closest interpolation
347 // may have a different street segments as a parent.
348 // Therefore allow an interpolation line to take precendence
349 // even when the street is closer.
350 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
353 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
354 Debug::printVar('Interpolation result', $aPlace);
357 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
358 $oResult->iHouseNumber = closestHouseNumber($aHouse);
364 // if no POI or street is found ...
365 $oResult = $this->lookupLargeArea($sPointSQL, 25);
368 // lower than street level ($iMaxRank < 26 )
369 $oResult = $this->lookupLargeArea($sPointSQL, $this->iMaxRank);
372 Debug::printVar('Final result', $oResult);