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_search,country_code, geometry';
75 $sSQL .= ' FROM placex';
76 $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\')';
77 $sSQL .= ' AND rank_search <= LEAST(25, '.$iMaxRank.')';
78 $sSQL .= ' AND ST_CONTAINS(geometry, '.$sPointSQL.' )';
79 $sSQL .= ' AND type != \'postcode\' ';
80 $sSQL .= ' AND name IS NOT NULL ';
81 $sSQL .= ' ORDER BY rank_search DESC LIMIT 1';
84 $this->oDB->getRow($sSQL),
85 'Could not determine polygon containing the point.'
88 $iParentPlaceID = $aPoly['parent_place_id'];
89 $iRankSearch = $aPoly['rank_search'];
90 $iGeometry = $aPoly['geometry'];
92 $sSQL = 'select place_id,parent_place_id,rank_search,country_code,';
93 $sSQL .=' ST_distance('.$sPointSQL.', geometry) as distance';
94 $sSQL .= ' FROM placex';
95 $sSQL .= ' WHERE osm_type = \'N\'';
96 $sSQL .= ' AND rank_search >= '.$iRankSearch;
97 $sSQL .= ' AND rank_search <= LEAST(25, '.$iMaxRank.')';
98 $sSQL .= ' AND ST_CONTAINS(\''.$iGeometry.'\'::geometry, geometry )';
99 $sSQL .= ' AND type != \'postcode\'';
100 $sSQL .= ' AND name IS NOT NULL ';
101 $sSQL .= ' ORDER BY distance ASC,';
102 $sSQL .= ' rank_search DESC';
104 if (CONST_Debug) var_dump($sSQL);
106 $this->oDB->getRow($sSQL),
107 'Could not determine place node.'
116 public function lookup($fLat, $fLon, $bDoInterpolation = true)
118 return $this->lookupPoint(
119 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
124 public function lookupPoint($sPointSQL, $bDoInterpolation = true)
127 $iMaxRank = $this->iMaxRank;
129 // Find the nearest point
130 $fSearchDiam = 0.006;
133 $fMaxAreaDistance = 1;
134 $bIsTigerStreet = false;
136 // for POI or street level
137 if ( $iMaxRank >= 26 ) {
139 $sSQL = 'select place_id,parent_place_id,rank_search,country_code,';
140 $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
141 $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
142 $sSQL .= ' END as distance';
145 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
148 if ($iMaxRank == 26) {
149 $sSQL .= ' rank_search != 28 and rank_search = 26';
151 $sSQL .= ' rank_search != 28 and rank_search >= 26';
153 $sSQL .= ' and (name is not null or housenumber is not null';
154 $sSQL .= ' or rank_search between 26 and 27)';
155 $sSQL .= ' and type not in (\'proposed\')';
156 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
157 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
158 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
159 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
160 $sSQL .= ' ORDER BY distance ASC limit 1';
161 if (CONST_Debug) var_dump($sSQL);
163 $this->oDB->getRow($sSQL),
164 'Could not determine closest place.'
168 $iPlaceID = $aPlace['place_id'];
169 $oResult = new Result($iPlaceID);
170 $iParentPlaceID = $aPlace['parent_place_id'];
171 // if street and maxrank > streetlevel
172 if (($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27)&& $iMaxRank > 27 ) {
173 // find the closest object (up to a certain radius) of which the street is a parent of
174 $sSQL = ' select place_id,parent_place_id,rank_search,country_code,';
175 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
179 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.003)';
180 $sSQL .= ' AND parent_place_id = '.$iPlaceID;
181 $sSQL .= ' and (name is not null or housenumber is not null)';
182 $sSQL .= ' ORDER BY distance ASC limit 1';
183 if (CONST_Debug) var_dump($sSQL);
185 $this->oDB->getRow($sSQL),
186 'Could not determine closest place.'
189 $iPlaceID = $aStreet['place_id'];
190 $oResult = new Result($iPlaceID);
191 $iParentPlaceID = $aStreet['parent_place_id'];
195 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
197 $oResult = new Result($aPlace['place_id']);
200 // lower than street level ($iMaxRank < 26 )
202 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
204 $oResult = new Result($aPlace['place_id']);