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 between 5 and ' .min(25, $iMaxRank);
115 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
116 $sSQL .= ' AND name IS NOT NULL ';
117 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
118 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
119 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
120 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
122 Debug::printSQL($sSQL);
124 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
125 Debug::printVar('Country node', $aPlace);
128 return new Result($aPlace['place_id']);
132 // still nothing, then return the country object
133 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
134 $sSQL .= ' FROM placex';
135 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
136 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
137 $sSQL .= ' AND class in (\'boundary\', \'place\')';
138 $sSQL .= ' AND linked_place_id is null';
139 $sSQL .= ' ORDER BY distance ASC';
140 Debug::printSQL($sSQL);
142 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
143 Debug::printVar('Country place', $aPlace);
145 return new Result($aPlace['place_id']);
153 * Search for areas or nodes for areas or nodes between state and suburb level.
155 * @param string $sPointSQL Search point as SQL string.
156 * @param int $iMaxRank Maximum address rank of the feature.
158 * @return Record of the found feature or null.
160 * Searches first for polygon that contains the search point.
161 * If such a polygon is found, place nodes with a higher rank are
162 * searched inside the polygon.
164 protected function lookupPolygon($sPointSQL, $iMaxRank)
166 Debug::newFunction('lookupPolygon');
167 // polygon search begins at suburb-level
168 if ($iMaxRank > 25) {
171 // no polygon search over country-level
175 // search for polygon
176 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
177 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
178 $sSQL .= ' FROM placex';
179 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
180 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
181 $sSQL .= ' AND geometry && '.$sPointSQL;
182 $sSQL .= ' AND type != \'postcode\' ';
183 $sSQL .= ' AND name is not null';
184 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
185 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
186 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
187 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
188 Debug::printSQL($sSQL);
190 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
191 Debug::printVar('Polygon result', $aPoly);
194 // if a polygon is found, search for placenodes begins ...
195 $iRankAddress = $aPoly['rank_address'];
196 $iRankSearch = $aPoly['rank_search'];
197 $iPlaceID = $aPoly['place_id'];
199 if ($iRankAddress != $iMaxRank) {
200 $sSQL = 'SELECT place_id FROM ';
201 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
202 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
203 $sSQL .= ' FROM placex';
204 $sSQL .= ' WHERE osm_type = \'N\'';
205 // using rank_search because of a better differentiation
206 // for place nodes at rank_address 16
207 $sSQL .= ' AND rank_search > '.$iRankSearch;
208 $sSQL .= ' AND rank_search <= '.$iMaxRank;
209 $sSQL .= ' AND rank_address > 0';
210 $sSQL .= ' AND class = \'place\'';
211 $sSQL .= ' AND type != \'postcode\'';
212 $sSQL .= ' AND name IS NOT NULL ';
213 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
214 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
215 $sSQL .= ' ORDER BY distance ASC,';
216 $sSQL .= ' rank_address DESC';
217 $sSQL .= ' limit 500) as a';
218 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
219 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
220 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
222 Debug::printSQL($sSQL);
224 $aPlaceNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
225 Debug::printVar('Nearest place node', $aPlaceNode);
235 public function lookup($fLat, $fLon, $bDoInterpolation = true)
237 return $this->lookupPoint(
238 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
243 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
245 Debug::newFunction('lookupPoint');
246 // Find the nearest point
247 $fSearchDiam = 0.006;
251 // for POI or street level
252 if ($this->iMaxRank >= 26) {
253 // starts if the search is on POI or street level,
254 // searches for the nearest POI or street,
255 // if a street is found and a POI is searched for,
256 // the nearest POI which the found street is a parent of is choosen.
257 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
258 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
261 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
263 $sSQL .= ' rank_address between 26 and '.$this->iMaxRank;
264 $sSQL .= ' and (name is not null or housenumber is not null';
265 $sSQL .= ' or rank_address between 26 and 27)';
266 $sSQL .= ' and (rank_address between 26 and 27';
267 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
268 $sSQL .= ' and class not in (\'boundary\')';
269 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
270 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
271 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
272 $sSQL .= ' ORDER BY distance ASC limit 1';
273 Debug::printSQL($sSQL);
275 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
277 Debug::printVar('POI/street level result', $aPlace);
279 $iPlaceID = $aPlace['place_id'];
280 $oResult = new Result($iPlaceID);
281 $iRankAddress = $aPlace['rank_address'];
285 // if street and maxrank > streetlevel
286 if ($iRankAddress <= 27 && $this->iMaxRank > 27) {
287 // find the closest object (up to a certain radius) of which the street is a parent of
288 $sSQL = ' select place_id,';
289 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
293 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
294 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
295 $sSQL .= ' and rank_address > 28';
296 $sSQL .= ' and ST_GeometryType(geometry) != \'ST_LineString\'';
297 $sSQL .= ' and (name is not null or housenumber is not null)';
298 $sSQL .= ' and class not in (\'boundary\')';
299 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
300 $sSQL .= ' ORDER BY distance ASC limit 1';
301 Debug::printSQL($sSQL);
303 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
304 Debug::printVar('Closest POI result', $aStreet);
308 $oResult = new Result($aStreet['place_id']);
313 // In the US we can check TIGER data for nearest housenumber
314 if (CONST_Use_US_Tiger_Data
315 && $iRankAddress <= 27
316 && $aPlace['country_code'] == 'us'
317 && $this->iMaxRank >= 28
319 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
320 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
321 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
322 $sSQL .= 'startnumber,endnumber,interpolationtype';
323 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
324 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
325 $sSQL .= ' ORDER BY distance ASC limit 1';
326 Debug::printSQL($sSQL);
328 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
329 Debug::printVar('Tiger house number result', $aPlaceTiger);
332 $aPlace = $aPlaceTiger;
333 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
334 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
340 if ($bDoInterpolation && $this->iMaxRank >= 30) {
341 $fDistance = $fSearchDiam;
343 // We can't reliably go from the closest street to an
344 // interpolation line because the closest interpolation
345 // may have a different street segments as a parent.
346 // Therefore allow an interpolation line to take precendence
347 // even when the street is closer.
348 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
351 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
352 Debug::printVar('Interpolation result', $aPlace);
355 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
356 $oResult->iHouseNumber = closestHouseNumber($aHouse);
362 // if no POI or street is found ...
363 $oResult = $this->lookupLargeArea($sPointSQL, 25);
366 // lower than street level ($iMaxRank < 26 )
367 $oResult = $this->lookupLargeArea($sPointSQL, $this->iMaxRank);
370 Debug::printVar('Final result', $oResult);