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 * FROM';
75 $sSQL .= '(select place_id,parent_place_id,rank_address,country_code, geometry';
76 $sSQL .= ' FROM placex';
77 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
78 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
79 $sSQL .= ' AND geometry && '.$sPointSQL;
80 $sSQL .= ' AND type != \'postcode\' ';
81 $sSQL .= ' AND name is not null';
82 $sSQL .= ' AND class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
83 $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
84 $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
85 $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
86 $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
89 $this->oDB->getRow($sSQL),
90 'Could not determine polygon containing the point.'
93 $iParentPlaceID = $aPoly['parent_place_id'];
94 $iRankAddress = $aPoly['rank_address'];
95 $iPlaceID = $aPoly['place_id'];
97 if ($iRankAddress != $iMaxRank) {
100 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
101 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
102 $sSQL .= ' FROM placex';
103 $sSQL .= ' WHERE osm_type = \'N\'';
104 $sSQL .= ' AND rank_address > '.$iRankAddress;
105 $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
106 $sSQL .= ' AND type != \'postcode\'';
107 $sSQL .= ' AND name IS NOT NULL ';
108 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
109 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
110 // preselection through bbox
111 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
112 $sSQL .= ' ORDER BY distance ASC,';
113 $sSQL .= ' rank_address DESC';
114 $sSQL .= ' limit 500) as a';
115 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
116 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
119 if (CONST_Debug) var_dump($sSQL);
121 $this->oDB->getRow($sSQL),
122 'Could not determine place node.'
132 public function lookup($fLat, $fLon, $bDoInterpolation = true)
134 return $this->lookupPoint(
135 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
140 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
143 $iMaxRank = $this->iMaxRank;
145 // Find the nearest point
146 $fSearchDiam = 0.006;
149 $fMaxAreaDistance = 1;
150 $bIsTigerStreet = false;
152 // for POI or street level
153 if ($iMaxRank >= 26) {
154 $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
155 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
156 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
157 $sSQL .= ' END as distance';
160 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
163 if ($iMaxRank == 26) {
164 $sSQL .= ' rank_address != 28 and rank_address = 26';
166 $sSQL .= ' rank_address != 28 and rank_address >= 26';
168 $sSQL .= ' and (name is not null or housenumber is not null';
169 $sSQL .= ' or rank_address between 26 and 27)';
170 //$sSQL .= ' and type not in (\'proposed\')';
171 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
172 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
173 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
174 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
175 $sSQL .= ' ORDER BY distance ASC limit 1';
176 if (CONST_Debug) var_dump($sSQL);
178 $this->oDB->getRow($sSQL),
179 'Could not determine closest place.'
183 $iPlaceID = $aPlace['place_id'];
184 $oResult = new Result($iPlaceID);
185 $iParentPlaceID = $aPlace['parent_place_id'];
186 // if street and maxrank > streetlevel
187 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
188 // find the closest object (up to a certain radius) of which the street is a parent of
189 $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
190 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
194 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
195 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
196 $sSQL .= ' and rank_address != 28';
197 $sSQL .= ' and (name is not null or housenumber is not null';
198 $sSQL .= ' or rank_address between 26 and 27)';
199 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
200 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
201 $sSQL .= ' ORDER BY distance ASC limit 1';
202 if (CONST_Debug) var_dump($sSQL);
204 $this->oDB->getRow($sSQL),
205 'Could not determine closest place.'
208 $iPlaceID = $aStreet['place_id'];
209 $oResult = new Result($iPlaceID);
210 $iParentPlaceID = $aStreet['parent_place_id'];
214 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
216 $oResult = new Result($aPlace['place_id']);
219 // lower than street level ($iMaxRank < 26 )
221 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
223 $oResult = new Result($aPlace['place_id']);