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
53 * @return Record of the interpolation or null.
55 protected function lookupInterpolation($sPointSQL, $fSearchDiam)
57 $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
58 $sSQL .= ' ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
59 $sSQL .= ' startnumber, endnumber, interpolationtype,';
60 $sSQL .= ' ST_Distance(linegeo,'.$sPointSQL.') as distance';
61 $sSQL .= ' FROM location_property_osmline';
62 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
63 $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
64 $sSQL .= ' ORDER BY distance ASC limit 1';
67 $this->oDB->getRow($sSQL),
68 'Could not determine closest housenumber on an osm interpolation line.'
72 protected function lookupLargeArea($sPointSQL, $iMaxRank)
77 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
79 return new Result($aPlace['place_id']);
83 // If no polygon which contains the searchpoint is found,
84 // searches in the country_osm_grid table for a polygon.
85 return $this->lookupInCountry($sPointSQL, $iMaxRank);
88 protected function lookupInCountry($sPointSQL, $iMaxRank)
90 // searches for polygon in table country_osm_grid which contains the searchpoint
91 // and searches for the nearest place node to the searchpoint in this polygon
92 $sSQL = 'SELECT country_code FROM country_osm_grid';
93 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
95 $sCountryCode = chksql(
96 $this->oDB->getOne($sSQL),
97 'Could not determine country polygon containing the point.'
101 // look for place nodes with the given country code
102 $sSQL = 'SELECT place_id FROM';
103 $sSQL .= ' (SELECT place_id, rank_search,';
104 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
105 $sSQL .= ' FROM placex';
106 $sSQL .= ' WHERE osm_type = \'N\'';
107 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
108 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
109 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
110 $sSQL .= ' AND name IS NOT NULL ';
111 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
112 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
113 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
114 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
117 if (CONST_Debug) var_dump($sSQL);
119 $this->oDB->getRow($sSQL),
120 'Could not determine place node.'
123 return new Result($aPlace['place_id']);
127 // still nothing, then return the country object
128 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
129 $sSQL .= ' FROM placex';
130 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
131 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
132 $sSQL .= ' AND class in (\'boundary\', \'place\')';
133 $sSQL .= ' AND linked_place_id is null';
134 $sSQL .= ' ORDER BY distance ASC';
136 if (CONST_Debug) var_dump($sSQL);
138 $this->oDB->getRow($sSQL),
139 'Could not determine place node.'
142 return new Result($aPlace['place_id']);
150 * Search for areas or nodes for areas or nodes between state and suburb level.
152 * @param string $sPointSQL Search point as SQL string.
153 * @param int $iMaxRank Maximum address rank of the feature.
155 * @return Record of the found feature or null.
157 * Searches first for polygon that contains the search point.
158 * If such a polygon is found, place nodes with a higher rank are
159 * searched inside the polygon.
161 protected function lookupPolygon($sPointSQL, $iMaxRank)
163 // polygon search begins at suburb-level
164 if ($iMaxRank > 25) $iMaxRank = 25;
165 // no polygon search over country-level
166 if ($iMaxRank < 5) $iMaxRank = 5;
167 // search for polygon
168 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
169 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
170 $sSQL .= ' FROM placex';
171 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
172 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
173 $sSQL .= ' AND geometry && '.$sPointSQL;
174 $sSQL .= ' AND type != \'postcode\' ';
175 $sSQL .= ' AND name is not null';
176 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
177 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
178 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
179 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
182 $this->oDB->getRow($sSQL),
183 'Could not determine polygon containing the point.'
186 // if a polygon is found, search for placenodes begins ...
187 $iParentPlaceID = $aPoly['parent_place_id'];
188 $iRankAddress = $aPoly['rank_address'];
189 $iRankSearch = $aPoly['rank_search'];
190 $iPlaceID = $aPoly['place_id'];
192 if ($iRankAddress != $iMaxRank) {
193 $sSQL = 'SELECT place_id FROM ';
194 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
195 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
196 $sSQL .= ' FROM placex';
197 $sSQL .= ' WHERE osm_type = \'N\'';
198 // using rank_search because of a better differentiation
199 // for place nodes at rank_address 16
200 $sSQL .= ' AND rank_search > '.$iRankSearch;
201 $sSQL .= ' AND rank_search <= '.$iMaxRank;
202 $sSQL .= ' AND class = \'place\'';
203 $sSQL .= ' AND type != \'postcode\'';
204 $sSQL .= ' AND name IS NOT NULL ';
205 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
206 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
207 $sSQL .= ' ORDER BY distance ASC,';
208 $sSQL .= ' rank_address DESC';
209 $sSQL .= ' limit 500) as a';
210 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
211 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
212 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
215 if (CONST_Debug) var_dump($sSQL);
217 $this->oDB->getRow($sSQL),
218 'Could not determine place node.'
229 public function lookup($fLat, $fLon, $bDoInterpolation = true)
231 return $this->lookupPoint(
232 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
237 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
239 // starts if the search is on POI or street level,
240 // searches for the nearest POI or street,
241 // if a street is found and a POI is searched for,
242 // the nearest POI which the found street is a parent of is choosen.
243 $iMaxRank = $this->iMaxRank;
245 // Find the nearest point
246 $fSearchDiam = 0.006;
250 // for POI or street level
251 if ($iMaxRank >= 26) {
252 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
253 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
256 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
259 if ($iMaxRank == 26) {
260 $sSQL .= ' rank_address = 26';
262 $sSQL .= ' rank_address between 26 and '.$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 if (CONST_Debug) var_dump($sSQL);
275 $this->oDB->getRow($sSQL),
276 'Could not determine closest place.'
279 if (CONST_Debug) var_dump($aPlace);
281 $iPlaceID = $aPlace['place_id'];
282 $oResult = new Result($iPlaceID);
283 $iRankAddress = $aPlace['rank_address'];
284 $iParentPlaceID = $aPlace['parent_place_id'];
287 if ($bDoInterpolation && $iMaxRank >= 30) {
288 $fDistance = $fSearchDiam;
290 // We can't reliably go from the closest street to an
291 // interpolation line because the closest interpolation
292 // may have a different street segments as a parent.
293 // Therefore allow an interpolation line to take precendence
294 // even when the street is closer.
295 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
298 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
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 if (CONST_Debug) var_dump($sSQL);
327 $this->oDB->getRow($sSQL),
328 'Could not determine closest place.'
331 if (CONST_Debug) var_dump($aStreet);
332 $oResult = new Result($aStreet['place_id']);
336 // In the US we can check TIGER data for nearest housenumber
337 if (CONST_Use_US_Tiger_Data
338 && $iRankAddress <= 27
339 && $aPlace['country_code'] == 'us'
340 && $this->iMaxRank >= 28
342 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
343 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
344 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
345 $sSQL .= 'startnumber,endnumber,interpolationtype';
346 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
347 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
348 $sSQL .= ' ORDER BY distance ASC limit 1';
349 if (CONST_Debug) var_dump($sSQL);
350 $aPlaceTiger = chksql(
351 $this->oDB->getRow($sSQL),
352 'Could not determine closest Tiger place.'
355 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
356 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
357 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
361 // if no POI or street is found ...
362 $oResult = $this->lookupLargeArea($sPointSQL, 25);
365 // lower than street level ($iMaxRank < 26 )
366 $oResult = $this->lookupLargeArea($sPointSQL, $iMaxRank);