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 lookupPolygon($sPointSQL, $iMaxRank)
74 $sSQL = 'select place_id,parent_place_id,rank_address,country_code, geometry';
75 $sSQL .= ' FROM placex';
76 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\')';
77 $sSQL .= ' AND rank_address <= LEAST(25, '.$iMaxRank.')';
78 $sSQL .= ' AND ST_CONTAINS(geometry, '.$sPointSQL.' )';
79 $sSQL .= ' AND type != \'postcode\' ';
80 $sSQL .= ' and rank_address != 28';
81 $sSQL .= ' and (name is not null or housenumber is not null';
82 $sSQL .= ' or rank_address between 26 and 27)';
83 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
84 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
87 $this->oDB->getRow($sSQL),
88 'Could not determine polygon containing the point.'
91 $iParentPlaceID = $aPoly['parent_place_id'];
92 $iRankAddress = $aPoly['rank_address'];
93 $iPlaceID = $aPoly['place_id'];
97 $sSQL .= ' SELECT place_id, rank_address,country_code, linked_place_id, geometry,';
98 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
99 $sSQL .= ' FROM placex';
100 $sSQL .= ' WHERE osm_type = \'N\'';
101 $sSQL .= ' AND rank_address >= '.$iRankAddress;
102 $sSQL .= ' AND rank_address <= LEAST(25, '.$iMaxRank.')';
103 $sSQL .= ' AND type != \'postcode\'';
104 $sSQL .= ' AND name IS NOT NULL ';
105 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
106 $sSQL .= ' ORDER BY distance ASC,';
107 $sSQL .= ' rank_address DESC';
108 $sSQL .= ' limit 500) as a';
109 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
110 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
113 if (CONST_Debug) var_dump($sSQL);
115 $this->oDB->getRow($sSQL),
116 'Could not determine place node.'
125 public function lookup($fLat, $fLon, $bDoInterpolation = true)
127 return $this->lookupPoint(
128 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
133 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
136 $iMaxRank = $this->iMaxRank;
138 // Find the nearest point
139 $fSearchDiam = 0.006;
142 $fMaxAreaDistance = 1;
143 $bIsTigerStreet = false;
145 // for POI or street level
146 if ( $iMaxRank >= 26 ) {
148 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
149 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
150 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
151 $sSQL .= ' END as distance';
154 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
157 if ($iMaxRank == 26) {
158 $sSQL .= ' rank_address != 28 and rank_address = 26';
160 $sSQL .= ' rank_address != 28 and rank_address >= 26';
162 $sSQL .= ' and (name is not null or housenumber is not null';
163 $sSQL .= ' or rank_address between 26 and 27)';
164 //$sSQL .= ' and type not in (\'proposed\')';
165 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
166 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
167 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
168 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
169 $sSQL .= ' ORDER BY distance ASC limit 1';
170 if (CONST_Debug) var_dump($sSQL);
172 $this->oDB->getRow($sSQL),
173 'Could not determine closest place.'
177 $iPlaceID = $aPlace['place_id'];
178 $oResult = new Result($iPlaceID);
179 $iParentPlaceID = $aPlace['parent_place_id'];
180 // if street and maxrank > streetlevel
181 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27 ) {
182 // find the closest object (up to a certain radius) of which the street is a parent of
183 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
184 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
188 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
189 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
190 $sSQL .= ' and rank_address != 28';
191 $sSQL .= ' and (name is not null or housenumber is not null';
192 $sSQL .= ' or rank_address between 26 and 27)';
193 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
194 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
195 $sSQL .= ' ORDER BY distance ASC limit 1';
196 if (CONST_Debug) var_dump($sSQL);
198 $this->oDB->getRow($sSQL),
199 'Could not determine closest place.'
202 $iPlaceID = $aStreet['place_id'];
203 $oResult = new Result($iPlaceID);
204 $iParentPlaceID = $aStreet['parent_place_id'];
208 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
210 // if place node is found adress goes over linked_place_id
211 if (!empty($aPlace['linked_place_id'])) {
212 $oResult = new Result($aPlace['linked_place_id']);
214 $oResult = new Result($aPlace['place_id']);
218 // lower than street level ($iMaxRank < 26 )
220 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
222 // if place node is found adress goes over linked_place_id
223 if (!empty($aPlace['linked_place_id'])) {
224 $oResult = new Result($aPlace['linked_place_id']);
226 $oResult = new Result($aPlace['place_id']);