]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
fixed syntax error
[nominatim.git] / lib / ReverseGeocode.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/Result.php');
6
7 class ReverseGeocode
8 {
9     protected $oDB;
10     protected $iMaxRank = 28;
11
12
13     public function __construct(&$oDB)
14     {
15         $this->oDB =& $oDB;
16     }
17
18
19     public function setZoom($iZoom)
20     {
21         // Zoom to rank, this could probably be calculated but a lookup gives fine control
22         $aZoomRank = array(
23                       0 => 2, // Continent / Sea
24                       1 => 2,
25                       2 => 2,
26                       3 => 4, // Country
27                       4 => 4,
28                       5 => 8, // State
29                       6 => 10, // Region
30                       7 => 10,
31                       8 => 12, // County
32                       9 => 12,
33                       10 => 17, // City
34                       11 => 17,
35                       12 => 18, // Town / Village
36                       13 => 18,
37                       14 => 22, // Suburb
38                       15 => 22,
39                       16 => 26, // Street, TODO: major street?
40                       17 => 26,
41                       18 => 30, // or >, Building
42                       19 => 30, // or >, Building
43                      );
44         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
45     }
46
47     /**
48      * Find the closest interpolation with the given search diameter.
49      *
50      * @param string  $sPointSQL      Reverse geocoding point as SQL
51      * @param float   $fSearchDiam    Search diameter
52      * @param integer $iParentPlaceID Id of parent object
53      *
54      * @return Record of the interpolation or null.
55      */
56     protected function lookupInterpolation($sPointSQL, $fSearchDiam, $iParentPlaceID = null)
57     {
58         $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search,';
59         $sSQL .= '  ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
60         $sSQL .= '  startnumber, endnumber, interpolationtype,';
61         $sSQL .= '  ST_Distance(linegeo,'.$sPointSQL.') as distance';
62         $sSQL .= ' FROM location_property_osmline';
63         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
64         $sSQL .= ' and indexed_status = 0 and startnumber is not NULL ';
65         if (isset($iParentPlaceID)) {
66             $sSQL .= ' and parent_place_id = '.$iParentPlaceID;
67         }
68         $sSQL .= ' ORDER BY distance ASC limit 1';
69
70         return chksql(
71             $this->oDB->getRow($sSQL),
72             'Could not determine closest housenumber on an osm interpolation line.'
73         );
74     }
75     
76     protected function polygonFunctions($sPointSQL, $iMaxRank)
77     {
78         $oResult = null;
79         
80         $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
81         if ($aPlace) {
82             $oResult = new Result($aPlace['place_id']);
83         } elseif (!$aPlace && $iMaxRank > 4) {
84             $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
85             if ($aPlace) {
86                 $oResult = new Result($aPlace['place_id']);
87             }
88         }
89         return $oResult;
90     }
91     
92     protected function noPolygonFound($sPointSQL, $iMaxRank)
93     {
94         // searches for polygon in table country_osm_grid which contains the searchpoint
95         $sSQL = 'SELECT * FROM country_osm_grid';
96         $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
97         
98         $aPoly = chksql(
99             $this->oDB->getRow($sSQL),
100             'Could not determine polygon containing the point.'
101         );
102         if ($aPoly) {
103             $sCountryCode = $aPoly['country_code'];
104             
105             $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
106             $sSQL .= ' FROM placex';
107             $sSQL .= ' WHERE osm_type = \'N\'';
108             $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
109             $sSQL .= ' AND rank_address > 0';
110             $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
111             $sSQL .= ' AND type != \'postcode\'';
112             $sSQL .= ' AND name IS NOT NULL ';
113             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
114             $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
115             $sSQL .= ' LIMIT 1';
116             
117             if (CONST_Debug) var_dump($sSQL);
118             $aPlacNode = chksql(
119                 $this->oDB->getRow($sSQL),
120                 'Could not determine place node.'
121             );
122             if ($aPlacNode) {
123                 return $aPlacNode;
124             }
125         }
126     }
127     
128     protected function lookupPolygon($sPointSQL, $iMaxRank)
129     {
130         $sSQL = 'SELECT * FROM';
131         $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
132         $sSQL .= ' FROM placex';
133         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
134         $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
135         $sSQL .= ' AND geometry && '.$sPointSQL;
136         $sSQL .= ' AND type != \'postcode\' ';
137         $sSQL .= ' AND name is not null';
138         $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
139         $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
140         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
141         $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
142
143         $aPoly = chksql(
144             $this->oDB->getRow($sSQL),
145             'Could not determine polygon containing the point.'
146         );
147         if ($aPoly) {
148             $iParentPlaceID = $aPoly['parent_place_id'];
149             $iRankAddress = $aPoly['rank_address'];
150             $iRankSearch = $aPoly['rank_search'];
151             $iPlaceID = $aPoly['place_id'];
152             
153             //search diameter for the place node search
154             if ($iMaxRank <= 30) $fSearchDiam = 0.1;
155             if ($iMaxRank <= 18) $fSearchDiam = 0.2;
156             if ($iMaxRank <= 17) $fSearchDiam = 0.6;
157             if ($iMaxRank <= 12) $fSearchDiam = 0.8;
158             if ($iMaxRank <= 10) $fSearchDiam = 1;
159             if ($iMaxRank <= 8) $fSearchDiam = 2;
160             if ($iMaxRank <= 4) $fSearchDiam = 4;
161             
162             if ($iRankAddress != $iMaxRank) {
163                 $sSQL = 'SELECT *';
164                 $sSQL .= ' FROM (';
165                 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
166                 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
167                 $sSQL .= ' FROM placex';
168                 $sSQL .= ' WHERE osm_type = \'N\'';
169                 if ($iRankAddress = 16) {
170                 // using rank_search because of a better differentiation for place nodes at rank_address 16
171                     $sSQL .= ' AND rank_search > '.$iRankSearch;
172                     $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
173                     $sSQL .= ' AND class = \'place\'';
174                 } else {
175                     $sSQL .= ' AND rank_address > '.$iRankAddress;
176                     $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
177                 }
178                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
179                 $sSQL .= ' AND type != \'postcode\'';
180                 $sSQL .= ' AND name IS NOT NULL ';
181                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
182                 // preselection through bbox
183                 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
184                 $sSQL .= ' ORDER BY distance ASC,';
185                 $sSQL .= ' rank_address DESC';
186                 $sSQL .= ' limit 500) as a';
187                 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
188                 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
189                 $sSQL .= ' LIMIT 1';
190                 
191                 if (CONST_Debug) var_dump($sSQL);
192                 $aPlacNode = chksql(
193                     $this->oDB->getRow($sSQL),
194                     'Could not determine place node.'
195                 );
196                 if ($aPlacNode) {
197                     return $aPlacNode;
198                 }
199             }
200         }
201         return $aPoly;
202     }
203     
204
205     public function lookup($fLat, $fLon, $bDoInterpolation = true)
206     {
207         return $this->lookupPoint(
208             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
209             $bDoInterpolation
210         );
211     }
212
213     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
214     {
215         
216         $iMaxRank = $this->iMaxRank;
217
218         // Find the nearest point
219         $fSearchDiam = 0.006;
220         $oResult = null;
221         $aPlace = null;
222         $fMaxAreaDistance = 1;
223         $bIsTigerStreet = false;
224        
225         // for POI or street level
226         if ($iMaxRank >= 26) {
227             $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
228             $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
229             $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
230             $sSQL .= ' END as distance';
231             $sSQL .= ' FROM ';
232             $sSQL .= ' placex';
233             $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
234             $sSQL .= '   AND';
235             // only streets
236             if ($iMaxRank == 26) {
237                 $sSQL .= ' rank_address = 26';
238             } else {
239                 $sSQL .= ' rank_address between 26 and '.$iMaxRank;
240             }
241             $sSQL .= ' and (name is not null or housenumber is not null';
242             $sSQL .= ' or rank_address between 26 and 27)';
243             $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
244             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
245             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
246             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
247             $sSQL .= ' ORDER BY distance ASC limit 1';
248             if (CONST_Debug) var_dump($sSQL);
249             $aPlace = chksql(
250                 $this->oDB->getRow($sSQL),
251                 'Could not determine closest place.'
252             );
253             
254             if ($aPlace) {
255                     
256                 $iDistance = $aPlace['distance'];
257                 $iPlaceID = $aPlace['place_id'];
258                 $oResult = new Result($iPlaceID);
259                 $iParentPlaceID = $aPlace['parent_place_id'];
260                 
261                 if ($bDoInterpolation && $iMaxRank >= 30) {
262                     if ($aPlace['rank_address'] <=27) {
263                         $iDistance = 0.001;
264                     }
265                     $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
266
267                     if ($aHouse) {
268                         $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
269                         $oResult->iHouseNumber = closestHouseNumber($aHouse);
270                     }
271                 }
272                 
273                 // if street and maxrank > streetlevel
274                 if (($aPlace['rank_address'] <=27)&& $iMaxRank > 27) {
275                     // find the closest object (up to a certain radius) of which the street is a parent of
276                     $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
277                     $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
278                     $sSQL .= ' FROM ';
279                     $sSQL .= ' placex';
280                     // radius ?
281                     $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
282                     $sSQL .= ' AND parent_place_id = '.$iPlaceID;
283                     $sSQL .= ' and rank_address != 28';
284                     $sSQL .= ' and (name is not null or housenumber is not null)';
285                     $sSQL .= ' and class not in (\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
286                     $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
287                     $sSQL .= ' ORDER BY distance ASC limit 1';
288                     if (CONST_Debug) var_dump($sSQL);
289                     $aStreet = chksql(
290                         $this->oDB->getRow($sSQL),
291                         'Could not determine closest place.'
292                     );
293                     if ($aStreet) {
294                         $iDistance = $aStreet['distance'];
295                         $iPlaceID = $aStreet['place_id'];
296                         $oResult = new Result($iPlaceID);
297                         $iParentPlaceID = $aStreet['parent_place_id'];
298                             
299                         if ($bDoInterpolation && $iMaxRank >= 30) {
300                             $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
301
302                             if ($aHouse) {
303                                 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
304                                 $oResult->iHouseNumber = closestHouseNumber($aHouse);
305                             }
306                         }
307                     }
308                 }
309                 
310                   // In the US we can check TIGER data for nearest housenumber
311                 if (CONST_Use_US_Tiger_Data && $aPlace['country_code'] == 'us' && $this->iMaxRank >= 28) {
312                     $fSearchDiam = $aPlace['rank_search'] > 28 ? $aPlace['distance'] : 0.001;
313                     $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
314                     $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
315                     $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
316                     $sSQL .= 'startnumber,endnumber,interpolationtype';
317                     $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
318                     $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
319                     $sSQL .= ' ORDER BY distance ASC limit 1';
320                     if (CONST_Debug) var_dump($sSQL);
321                     $aPlaceTiger = chksql(
322                         $this->oDB->getRow($sSQL),
323                         'Could not determine closest Tiger place.'
324                     );
325                     if ($aPlaceTiger) {
326                         if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
327                         $aPlace = $aPlaceTiger;
328                         $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
329                         $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
330                     }
331                 }
332             // if no POI or street is found ...
333             } else {
334                 $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
335             }
336             // lower than street level ($iMaxRank < 26 )
337         } else {
338             $oResult = $this->PolygonFunctions($sPointSQL, $iMaxRank);
339         }
340         return $oResult;
341     }
342 }