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 $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';
66 return $this->oDB->getRow(
69 'Could not determine closest housenumber on an osm interpolation line.'
73 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 // searches for polygon in table country_osm_grid which contains the searchpoint
92 // and searches for the nearest place node to the searchpoint in this polygon
93 $sSQL = 'SELECT country_code FROM country_osm_grid';
94 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.') LIMIT 1';
96 $sCountryCode = $this->oDB->getOne(
99 'Could not determine country polygon containing the point.'
103 // look for place nodes with the given country code
104 $sSQL = 'SELECT place_id FROM';
105 $sSQL .= ' (SELECT place_id, rank_search,';
106 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
107 $sSQL .= ' FROM placex';
108 $sSQL .= ' WHERE osm_type = \'N\'';
109 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
110 $sSQL .= ' AND rank_search between 5 and ' .min(25, $iMaxRank);
111 $sSQL .= ' AND class = \'place\' AND type != \'postcode\'';
112 $sSQL .= ' AND name IS NOT NULL ';
113 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
114 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.8)) p ';
115 $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
116 $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
119 if (CONST_Debug) var_dump($sSQL);
120 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
122 return new Result($aPlace['place_id']);
126 // still nothing, then return the country object
127 $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
128 $sSQL .= ' FROM placex';
129 $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
130 $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
131 $sSQL .= ' AND class in (\'boundary\', \'place\')';
132 $sSQL .= ' AND linked_place_id is null';
133 $sSQL .= ' ORDER BY distance ASC';
135 if (CONST_Debug) var_dump($sSQL);
136 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
138 return new Result($aPlace['place_id']);
146 * Search for areas or nodes for areas or nodes between state and suburb level.
148 * @param string $sPointSQL Search point as SQL string.
149 * @param int $iMaxRank Maximum address rank of the feature.
151 * @return Record of the found feature or null.
153 * Searches first for polygon that contains the search point.
154 * If such a polygon is found, place nodes with a higher rank are
155 * searched inside the polygon.
157 protected function lookupPolygon($sPointSQL, $iMaxRank)
159 // polygon search begins at suburb-level
160 if ($iMaxRank > 25) $iMaxRank = 25;
161 // no polygon search over country-level
162 if ($iMaxRank < 5) $iMaxRank = 5;
163 // search for polygon
164 $sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
165 $sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
166 $sSQL .= ' FROM placex';
167 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
168 $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
169 $sSQL .= ' AND geometry && '.$sPointSQL;
170 $sSQL .= ' AND type != \'postcode\' ';
171 $sSQL .= ' AND name is not null';
172 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
173 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
174 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
175 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
177 $aPoly = $this->oDB->getRow($sSQL, null, 'Could not determine polygon containing the point.');
180 // if a polygon is found, search for placenodes begins ...
181 $iParentPlaceID = $aPoly['parent_place_id'];
182 $iRankAddress = $aPoly['rank_address'];
183 $iRankSearch = $aPoly['rank_search'];
184 $iPlaceID = $aPoly['place_id'];
186 if ($iRankAddress != $iMaxRank) {
187 $sSQL = 'SELECT place_id FROM ';
188 $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
189 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
190 $sSQL .= ' FROM placex';
191 $sSQL .= ' WHERE osm_type = \'N\'';
192 // using rank_search because of a better differentiation
193 // for place nodes at rank_address 16
194 $sSQL .= ' AND rank_search > '.$iRankSearch;
195 $sSQL .= ' AND rank_search <= '.$iMaxRank;
196 $sSQL .= ' AND class = \'place\'';
197 $sSQL .= ' AND type != \'postcode\'';
198 $sSQL .= ' AND name IS NOT NULL ';
199 $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
200 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, reverse_place_diameter('.$iRankSearch.'::smallint))';
201 $sSQL .= ' ORDER BY distance ASC,';
202 $sSQL .= ' rank_address DESC';
203 $sSQL .= ' limit 500) as a';
204 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
205 $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
206 $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
209 if (CONST_Debug) var_dump($sSQL);
210 $aPlacNode = $this->oDB->getRow($sSQL, null, 'Could not determine place node.');
220 public function lookup($fLat, $fLon, $bDoInterpolation = true)
222 return $this->lookupPoint(
223 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
228 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
230 // starts if the search is on POI or street level,
231 // searches for the nearest POI or street,
232 // if a street is found and a POI is searched for,
233 // the nearest POI which the found street is a parent of is choosen.
234 $iMaxRank = $this->iMaxRank;
236 // Find the nearest point
237 $fSearchDiam = 0.006;
241 // for POI or street level
242 if ($iMaxRank >= 26) {
243 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
244 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
247 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
249 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
250 $sSQL .= ' and (name is not null or housenumber is not null';
251 $sSQL .= ' or rank_address between 26 and 27)';
252 $sSQL .= ' and (rank_address between 26 and 27';
253 $sSQL .= ' or ST_GeometryType(geometry) != \'ST_LineString\')';
254 $sSQL .= ' and class not in (\'boundary\')';
255 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
256 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
257 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
258 $sSQL .= ' ORDER BY distance ASC limit 1';
259 if (CONST_Debug) var_dump($sSQL);
260 $aPlace = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
262 if (CONST_Debug) var_dump($aPlace);
264 $iPlaceID = $aPlace['place_id'];
265 $oResult = new Result($iPlaceID);
266 $iRankAddress = $aPlace['rank_address'];
267 $iParentPlaceID = $aPlace['parent_place_id'];
270 if ($bDoInterpolation && $iMaxRank >= 30) {
271 $fDistance = $fSearchDiam;
273 // We can't reliably go from the closest street to an
274 // interpolation line because the closest interpolation
275 // may have a different street segments as a parent.
276 // Therefore allow an interpolation line to take precendence
277 // even when the street is closer.
278 $fDistance = $iRankAddress < 28 ? 0.001 : $aPlace['distance'];
281 $aHouse = $this->lookupInterpolation($sPointSQL, $fDistance);
284 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
285 $oResult->iHouseNumber = closestHouseNumber($aHouse);
292 // if street and maxrank > streetlevel
293 if ($iRankAddress <= 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,';
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 ST_GeometryType(geometry) != \'ST_LineString\'';
304 $sSQL .= ' and (name is not null or housenumber is not null)';
305 $sSQL .= ' and class not in (\'boundary\')';
306 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
307 $sSQL .= ' ORDER BY distance ASC limit 1';
308 if (CONST_Debug) var_dump($sSQL);
309 $aStreet = $this->oDB->getRow($sSQL, null, 'Could not determine closest place.');
311 if (CONST_Debug) var_dump($aStreet);
312 $oResult = new Result($aStreet['place_id']);
316 // In the US we can check TIGER data for nearest housenumber
317 if (CONST_Use_US_Tiger_Data
318 && $iRankAddress <= 27
319 && $aPlace['country_code'] == 'us'
320 && $this->iMaxRank >= 28
322 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
323 $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
324 $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
325 $sSQL .= 'startnumber,endnumber,interpolationtype';
326 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
327 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, 0.001)';
328 $sSQL .= ' ORDER BY distance ASC limit 1';
329 if (CONST_Debug) var_dump($sSQL);
330 $aPlaceTiger = $this->oDB->getRow($sSQL, null, 'Could not determine closest Tiger place.');
332 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
333 $oResult = new Result($aPlaceTiger['place_id'], Result::TABLE_TIGER);
334 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
338 // if no POI or street is found ...
339 $oResult = $this->lookupLargeArea($sPointSQL, 25);
342 // lower than street level ($iMaxRank < 26 )
343 $oResult = $this->lookupLargeArea($sPointSQL, $iMaxRank);