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 $sSQL = 'SELECT * FROM country_osm_grid';
75 $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
78 $this->oDB->getRow($sSQL),
79 'Could not determine polygon containing the point.'
82 $sCountryCode = $aPoly['country_code'];
84 $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
85 $sSQL .= ' FROM placex';
86 $sSQL .= ' WHERE osm_type = \'N\'';
87 $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
88 $sSQL .= ' AND rank_address > 0';
89 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
90 $sSQL .= ' AND type != \'postcode\'';
91 $sSQL .= ' AND name IS NOT NULL ';
92 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
93 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
96 if (CONST_Debug) var_dump($sSQL);
98 $this->oDB->getRow($sSQL),
99 'Could not determine place node.'
107 protected function lookupPolygon($sPointSQL, $iMaxRank)
113 $sSQL = 'SELECT * FROM';
114 $sSQL .= '(select place_id,parent_place_id,rank_address,country_code, geometry';
115 $sSQL .= ' FROM placex';
116 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
117 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
118 $sSQL .= ' AND geometry && '.$sPointSQL;
119 $sSQL .= ' AND type != \'postcode\' ';
120 $sSQL .= ' AND name is not null';
121 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
122 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
123 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
124 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
127 $this->oDB->getRow($sSQL),
128 'Could not determine polygon containing the point.'
131 $iParentPlaceID = $aPoly['parent_place_id'];
132 $iRankAddress = $aPoly['rank_address'];
133 $iPlaceID = $aPoly['place_id'];
135 if ($iRankAddress != $iMaxRank) {
138 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
139 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
140 $sSQL .= ' FROM placex';
141 $sSQL .= ' WHERE osm_type = \'N\'';
142 $sSQL .= ' AND rank_address > '.$iRankAddress;
143 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
144 $sSQL .= ' AND type != \'postcode\'';
145 $sSQL .= ' AND name IS NOT NULL ';
146 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
147 // preselection through bbox
148 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
149 $sSQL .= ' ORDER BY distance ASC,';
150 $sSQL .= ' rank_address DESC';
151 $sSQL .= ' limit 500) as a';
152 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
153 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
156 if (CONST_Debug) var_dump($sSQL);
158 $this->oDB->getRow($sSQL),
159 'Could not determine place node.'
169 public function lookup($fLat, $fLon, $bDoInterpolation = true)
171 return $this->lookupPoint(
172 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
177 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
180 $iMaxRank = $this->iMaxRank;
182 // Find the nearest point
183 $fSearchDiam = 0.006;
186 $fMaxAreaDistance = 1;
187 $bIsTigerStreet = false;
189 // try with interpolations before continuing
190 if ($bDoInterpolation && $iMaxRank >= 30) {
191 $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/3);
194 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
195 $oResult->iHouseNumber = closestHouseNumber($aHouse);
198 $iParentPlaceID = $aHouse['parent_place_id']; // the street
203 }// no interpolation found, continue search
205 // for POI or street level
206 if ($iMaxRank >= 26) {
207 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
208 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
209 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
210 $sSQL .= ' END as distance';
213 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
216 if ($iMaxRank == 26) {
217 $sSQL .= ' rank_address = 26';
219 $sSQL .= ' rank_address >= 26';
221 $sSQL .= ' and (name is not null or housenumber is not null';
222 $sSQL .= ' or rank_address between 26 and 27)';
223 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
224 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
225 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
226 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
227 $sSQL .= ' ORDER BY distance ASC limit 1';
228 if (CONST_Debug) var_dump($sSQL);
230 $this->oDB->getRow($sSQL),
231 'Could not determine closest place.'
235 $iPlaceID = $aPlace['place_id'];
236 $oResult = new Result($iPlaceID);
237 $iParentPlaceID = $aPlace['parent_place_id'];
238 // if street and maxrank > streetlevel
239 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
240 // find the closest object (up to a certain radius) of which the street is a parent of
241 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
242 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
246 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
247 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
248 $sSQL .= ' and rank_address != 28';
249 $sSQL .= ' and (name is not null or housenumber is not null';
250 $sSQL .= ' or rank_address between 26 and 27)';
251 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
252 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
253 $sSQL .= ' ORDER BY distance ASC limit 1';
254 if (CONST_Debug) var_dump($sSQL);
256 $this->oDB->getRow($sSQL),
257 'Could not determine closest place.'
260 $iPlaceID = $aStreet['place_id'];
261 $oResult = new Result($iPlaceID);
262 $iParentPlaceID = $aStreet['parent_place_id'];
266 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
268 $oResult = new Result($aPlace['place_id']);
269 } elseif(!$aPlace && $iMaxRank > 4) {
270 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
272 $oResult = new Result($aPlace['place_id']);
276 // lower than street level ($iMaxRank < 26 )
278 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
280 $oResult = new Result($aPlace['place_id']);
281 } elseif(!$aPlace && $iMaxRank > 4) {
282 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
284 $oResult = new Result($aPlace['place_id']);