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 noPolygonFound($sPointSQL, $iMaxRank)
74 // searches for polygon in table country_osm_grid which contains the searchpoint
75 $sSQL = 'SELECT * FROM country_osm_grid';
76 $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
79 $this->oDB->getRow($sSQL),
80 'Could not determine polygon containing the point.'
83 $sCountryCode = $aPoly['country_code'];
85 $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
86 $sSQL .= ' FROM placex';
87 $sSQL .= ' WHERE osm_type = \'N\'';
88 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
89 $sSQL .= ' AND rank_address > 0';
90 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
91 $sSQL .= ' AND type != \'postcode\'';
92 $sSQL .= ' AND name IS NOT NULL ';
93 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
94 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
97 if (CONST_Debug) var_dump($sSQL);
99 $this->oDB->getRow($sSQL),
100 'Could not determine place node.'
108 protected function lookupPolygon($sPointSQL, $iMaxRank)
114 $sSQL = 'SELECT * FROM';
115 $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
116 $sSQL .= ' FROM placex';
117 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
118 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
119 $sSQL .= ' AND geometry && '.$sPointSQL;
120 $sSQL .= ' AND type != \'postcode\' ';
121 $sSQL .= ' AND name is not null';
122 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
123 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
124 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
125 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
128 $this->oDB->getRow($sSQL),
129 'Could not determine polygon containing the point.'
132 $iParentPlaceID = $aPoly['parent_place_id'];
133 $iRankAddress = $aPoly['rank_address'];
134 $iRankSearch = $aPoly['rank_search'];
135 $iPlaceID = $aPoly['place_id'];
137 if ($iRankAddress != $iMaxRank) {
140 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
141 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
142 $sSQL .= ' FROM placex';
143 $sSQL .= ' WHERE osm_type = \'N\'';
144 if ($iRankAddress = 16){
145 // using rank_search beacause of a better differentiation for place nodes at rank_address 16
146 $sSQL .= ' AND rank_search > '.$iRankSearch;
147 $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
148 $sSQL .= ' AND class = \'place\'';
150 $sSQL .= ' AND rank_address > '.$iRankAddress;
151 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
153 $sSQL .= ' AND type != \'postcode\'';
154 $sSQL .= ' AND name IS NOT NULL ';
155 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
156 // preselection through bbox
157 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
158 $sSQL .= ' ORDER BY distance ASC,';
159 $sSQL .= ' rank_address DESC';
160 $sSQL .= ' limit 500) as a';
161 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
162 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
165 if (CONST_Debug) var_dump($sSQL);
167 $this->oDB->getRow($sSQL),
168 'Could not determine place node.'
178 public function lookup($fLat, $fLon, $bDoInterpolation = true)
180 return $this->lookupPoint(
181 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
186 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
189 $iMaxRank = $this->iMaxRank;
191 // Find the nearest point
192 $fSearchDiam = 0.006;
195 $fMaxAreaDistance = 1;
196 $bIsTigerStreet = false;
198 // try with interpolations before continuing
199 if ($bDoInterpolation && $iMaxRank >= 30) {
200 $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/3);
203 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
204 $oResult->iHouseNumber = closestHouseNumber($aHouse);
207 $iParentPlaceID = $aHouse['parent_place_id']; // the street
212 }// no interpolation found, continue search
214 // for POI or street level
215 if ($iMaxRank >= 26) {
216 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
217 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
218 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
219 $sSQL .= ' END as distance';
222 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
225 if ($iMaxRank == 26) {
226 $sSQL .= ' rank_address = 26';
228 $sSQL .= ' rank_address >= 26';
230 $sSQL .= ' and (name is not null or housenumber is not null';
231 $sSQL .= ' or rank_address between 26 and 27)';
232 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
233 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
234 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
235 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
236 $sSQL .= ' ORDER BY distance ASC limit 1';
237 if (CONST_Debug) var_dump($sSQL);
239 $this->oDB->getRow($sSQL),
240 'Could not determine closest place.'
244 $iPlaceID = $aPlace['place_id'];
245 $oResult = new Result($iPlaceID);
246 $iParentPlaceID = $aPlace['parent_place_id'];
247 // if street and maxrank > streetlevel
248 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
249 // find the closest object (up to a certain radius) of which the street is a parent of
250 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
251 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
255 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
256 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
257 $sSQL .= ' and rank_address != 28';
258 $sSQL .= ' and (name is not null or housenumber is not null';
259 $sSQL .= ' or rank_address between 26 and 27)';
260 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
261 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
262 $sSQL .= ' ORDER BY distance ASC limit 1';
263 if (CONST_Debug) var_dump($sSQL);
265 $this->oDB->getRow($sSQL),
266 'Could not determine closest place.'
269 $iPlaceID = $aStreet['place_id'];
270 $oResult = new Result($iPlaceID);
271 $iParentPlaceID = $aStreet['parent_place_id'];
275 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
277 $oResult = new Result($aPlace['place_id']);
278 } elseif(!$aPlace && $iMaxRank > 4) {
279 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
281 $oResult = new Result($aPlace['place_id']);
285 // lower than street level ($iMaxRank < 26 )
287 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
289 $oResult = new Result($aPlace['place_id']);
290 } elseif(!$aPlace && $iMaxRank > 4) {
291 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
293 $oResult = new Result($aPlace['place_id']);