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, $iParentPlaceID = null)
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 if (isset($iParentPlaceID)) {
65 $sSQL .= ' and parent_place_id = '.$iParentPlaceID;
67 $sSQL .= ' ORDER BY distance ASC limit 1';
70 $this->oDB->getRow($sSQL),
71 'Could not determine closest housenumber on an osm interpolation line.'
75 protected function noPolygonFound($sPointSQL, $iMaxRank)
77 // searches for polygon in table country_osm_grid which contains the searchpoint
78 $sSQL = 'SELECT * FROM country_osm_grid';
79 $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
82 $this->oDB->getRow($sSQL),
83 'Could not determine polygon containing the point.'
86 $sCountryCode = $aPoly['country_code'];
88 $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
89 $sSQL .= ' FROM placex';
90 $sSQL .= ' WHERE osm_type = \'N\'';
91 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
92 $sSQL .= ' AND rank_address > 0';
93 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
94 $sSQL .= ' AND type != \'postcode\'';
95 $sSQL .= ' AND name IS NOT NULL ';
96 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
97 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
100 if (CONST_Debug) var_dump($sSQL);
102 $this->oDB->getRow($sSQL),
103 'Could not determine place node.'
111 protected function lookupPolygon($sPointSQL, $iMaxRank)
117 $sSQL = 'SELECT * FROM';
118 $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
119 $sSQL .= ' FROM placex';
120 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
121 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
122 $sSQL .= ' AND geometry && '.$sPointSQL;
123 $sSQL .= ' AND type != \'postcode\' ';
124 $sSQL .= ' AND name is not null';
125 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
126 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
127 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
128 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
131 $this->oDB->getRow($sSQL),
132 'Could not determine polygon containing the point.'
135 $iParentPlaceID = $aPoly['parent_place_id'];
136 $iRankAddress = $aPoly['rank_address'];
137 $iRankSearch = $aPoly['rank_search'];
138 $iPlaceID = $aPoly['place_id'];
140 if ($iRankAddress != $iMaxRank) {
143 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
144 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
145 $sSQL .= ' FROM placex';
146 $sSQL .= ' WHERE osm_type = \'N\'';
147 if ($iRankAddress = 16) {
148 // using rank_search because of a better differentiation for place nodes at rank_address 16
149 $sSQL .= ' AND rank_search > '.$iRankSearch;
150 $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
151 $sSQL .= ' AND class = \'place\'';
153 $sSQL .= ' AND rank_address > '.$iRankAddress;
154 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
156 $sSQL .= ' AND type != \'postcode\'';
157 $sSQL .= ' AND name IS NOT NULL ';
158 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
159 // preselection through bbox
160 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
161 $sSQL .= ' ORDER BY distance ASC,';
162 $sSQL .= ' rank_address DESC';
163 $sSQL .= ' limit 500) as a';
164 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
165 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
168 if (CONST_Debug) var_dump($sSQL);
170 $this->oDB->getRow($sSQL),
171 'Could not determine place node.'
181 public function lookup($fLat, $fLon, $bDoInterpolation = true)
183 return $this->lookupPoint(
184 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
189 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
192 $iMaxRank = $this->iMaxRank;
194 // Find the nearest point
195 $fSearchDiam = 0.006;
198 $fMaxAreaDistance = 1;
199 $bIsTigerStreet = false;
201 // for POI or street level
202 if ($iMaxRank >= 26) {
203 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
204 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
205 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
206 $sSQL .= ' END as distance';
209 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
212 if ($iMaxRank == 26) {
213 $sSQL .= ' rank_address = 26';
215 $sSQL .= ' rank_address >= 26';
217 $sSQL .= ' and (name is not null or housenumber is not null';
218 $sSQL .= ' or rank_address between 26 and 27)';
219 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
220 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
221 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
222 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
223 $sSQL .= ' ORDER BY distance ASC limit 1';
224 if (CONST_Debug) var_dump($sSQL);
226 $this->oDB->getRow($sSQL),
227 'Could not determine closest place.'
232 $iDistance = $aPlace['distance'];
233 $iPlaceID = $aPlace['place_id'];
234 $oResult = new Result($iPlaceID);
235 $iParentPlaceID = $aPlace['parent_place_id'];
237 if ($bDoInterpolation && $iMaxRank >= 30) {
238 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
241 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
242 $oResult->iHouseNumber = closestHouseNumber($aHouse);
245 // if street and maxrank > streetlevel
246 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
247 // find the closest object (up to a certain radius) of which the street is a parent of
248 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
249 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
253 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
254 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
255 $sSQL .= ' and rank_address != 28';
256 $sSQL .= ' and (name is not null or housenumber is not null';
257 $sSQL .= ' or rank_address between 26 and 27)';
258 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
259 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
260 $sSQL .= ' ORDER BY distance ASC limit 1';
261 if (CONST_Debug) var_dump($sSQL);
263 $this->oDB->getRow($sSQL),
264 'Could not determine closest place.'
267 $iDistance = $aPlace['distance'];
268 $iPlaceID = $aStreet['place_id'];
269 $oResult = new Result($iPlaceID);
270 $iParentPlaceID = $aStreet['parent_place_id'];
272 if ($bDoInterpolation && $iMaxRank >= 30) {
273 $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
276 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
277 $oResult->iHouseNumber = closestHouseNumber($aHouse);
282 // if no POI or street is found ...
284 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
286 $oResult = new Result($aPlace['place_id']);
287 } elseif (!$aPlace && $iMaxRank > 4) {
288 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
290 $oResult = new Result($aPlace['place_id']);
294 // lower than street level ($iMaxRank < 26 )
296 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
298 $oResult = new Result($aPlace['place_id']);
299 } elseif (!$aPlace && $iMaxRank > 4) {
300 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
302 $oResult = new Result($aPlace['place_id']);