]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
fixed getoutlines function if no coordinates are passed
[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      *
53      * @return Record of the interpolation or null.
54      */
55     protected function lookupInterpolation($sPointSQL, $fSearchDiam, $iParentPlaceID = null)
56     {
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         if (isset($iParentPlaceID)) {
65             $sSQL .= ' and parent_place_id = '.$iParentPlaceID;
66         }
67         $sSQL .= ' ORDER BY distance ASC limit 1';
68
69         return chksql(
70             $this->oDB->getRow($sSQL),
71             'Could not determine closest housenumber on an osm interpolation line.'
72         );
73     }
74     
75     protected function noPolygonFound($sPointSQL, $iMaxRank)
76     {
77         // searches for polygon in table country_osm_grid which contains the searchpoint
78         $sSQL = 'SELECT * FROM country_osm_grid';
79         $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
80         
81         $aPoly = chksql(
82             $this->oDB->getRow($sSQL),
83             'Could not determine polygon containing the point.'
84         );
85         if ($aPoly) {
86             $sCountryCode = $aPoly['country_code'];
87             
88             $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
89             $sSQL .= ' FROM placex';
90             $sSQL .= ' WHERE osm_type = \'N\'';
91             $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
92             $sSQL .= ' AND rank_address > 0';
93             $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
94             $sSQL .= ' AND type != \'postcode\'';
95             $sSQL .= ' AND name IS NOT NULL ';
96             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
97             $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
98             $sSQL .= ' LIMIT 1';
99             
100             if (CONST_Debug) var_dump($sSQL);
101             $aPlacNode = chksql(
102                 $this->oDB->getRow($sSQL),
103                 'Could not determine place node.'
104             );
105             if ($aPlacNode) {
106                 return $aPlacNode;
107             }
108         }
109     }
110     
111     protected function lookupPolygon($sPointSQL, $iMaxRank)
112     {
113     
114         $oResult = null;
115         $aPlace = null;
116         
117         $sSQL = 'SELECT * FROM';
118         $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
119         $sSQL .= ' FROM placex';
120         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
121         $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
122         $sSQL .= ' AND geometry && '.$sPointSQL;
123         $sSQL .= ' AND type != \'postcode\' ';
124         $sSQL .= ' AND name is not null';
125         $sSQL .= ' AND indexed_status = 0 and linked_place_id is null';
126         $sSQL .= ' ORDER BY rank_address DESC LIMIT 50 ) as a';
127         $sSQL .= ' WHERE ST_CONTAINS(geometry, '.$sPointSQL.' )';
128         $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
129
130         $aPoly = chksql(
131             $this->oDB->getRow($sSQL),
132             'Could not determine polygon containing the point.'
133         );
134         if ($aPoly) {
135             $iParentPlaceID = $aPoly['parent_place_id'];
136             $iRankAddress = $aPoly['rank_address'];
137             $iRankSearch = $aPoly['rank_search'];
138             $iPlaceID = $aPoly['place_id'];
139             
140             if ($iRankAddress != $iMaxRank) {
141                 $sSQL = 'SELECT *';
142                 $sSQL .= ' FROM (';
143                 $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
144                 $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
145                 $sSQL .= ' FROM placex';
146                 $sSQL .= ' WHERE osm_type = \'N\'';
147                 if ($iRankAddress = 16) {
148                 // using rank_search because of a better differentiation for place nodes at rank_address 16
149                     $sSQL .= ' AND rank_search > '.$iRankSearch;
150                     $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
151                     $sSQL .= ' AND class = \'place\'';
152                 } else {
153                     $sSQL .= ' AND rank_address > '.$iRankAddress;
154                     $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
155                 }
156                 $sSQL .= ' AND type != \'postcode\'';
157                 $sSQL .= ' AND name IS NOT NULL ';
158                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
159                 // preselection through bbox
160                 $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
161                 $sSQL .= ' ORDER BY distance ASC,';
162                 $sSQL .= ' rank_address DESC';
163                 $sSQL .= ' limit 500) as a';
164                 $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
165                 $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
166                 $sSQL .= ' LIMIT 1';
167                 
168                 if (CONST_Debug) var_dump($sSQL);
169                 $aPlacNode = chksql(
170                     $this->oDB->getRow($sSQL),
171                     'Could not determine place node.'
172                 );
173                 if ($aPlacNode) {
174                     return $aPlacNode;
175                 }
176             }
177         }
178         return $aPoly;
179     }
180
181     public function lookup($fLat, $fLon, $bDoInterpolation = true)
182     {
183         return $this->lookupPoint(
184             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
185             $bDoInterpolation
186         );
187     }
188
189     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
190     {
191         
192         $iMaxRank = $this->iMaxRank;
193
194         // Find the nearest point
195         $fSearchDiam = 0.006;
196         $oResult = null;
197         $aPlace = null;
198         $fMaxAreaDistance = 1;
199         $bIsTigerStreet = false;
200        
201         // for POI or street level
202         if ($iMaxRank >= 26) {
203             $sSQL = 'select place_id,parent_place_id,rank_address,country_code,';
204             $sSQL .= 'CASE WHEN ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\') THEN ST_distance('.$sPointSQL.', centroid)';
205             $sSQL .= ' ELSE ST_distance('.$sPointSQL.', geometry) ';
206             $sSQL .= ' END as distance';
207             $sSQL .= ' FROM ';
208             $sSQL .= ' placex';
209             $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
210             $sSQL .= '   AND';
211             // only streets
212             if ($iMaxRank == 26) {
213                 $sSQL .= ' rank_address = 26';
214             } else {
215                 $sSQL .= ' rank_address >= 26';
216             }
217             $sSQL .= ' and (name is not null or housenumber is not null';
218             $sSQL .= ' or rank_address between 26 and 27)';
219             $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
220             $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
221             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
222             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
223             $sSQL .= ' ORDER BY distance ASC limit 1';
224             if (CONST_Debug) var_dump($sSQL);
225             $aPlace = chksql(
226                 $this->oDB->getRow($sSQL),
227                 'Could not determine closest place.'
228             );
229             
230             if ($aPlace) {
231                     
232                 $iDistance = $aPlace['distance'];
233                 $iPlaceID = $aPlace['place_id'];
234                 $oResult = new Result($iPlaceID);
235                 $iParentPlaceID = $aPlace['parent_place_id'];
236                 
237                 if ($bDoInterpolation && $iMaxRank >= 30) {
238                     $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance);
239
240                     if ($aHouse) {
241                         $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
242                         $oResult->iHouseNumber = closestHouseNumber($aHouse);
243                     }
244                 }
245                 // if street and maxrank > streetlevel
246                 if (($aPlace['rank_address'] == 26 || $aPlace['rank_address'] == 27)&& $iMaxRank > 27) {
247                     // find the closest object (up to a certain radius) of which the street is a parent of
248                     $sSQL = ' select place_id,parent_place_id,rank_address,country_code,';
249                     $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
250                     $sSQL .= ' FROM ';
251                     $sSQL .= ' placex';
252                     // radius ?
253                     $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, 0.001)';
254                     $sSQL .= ' AND parent_place_id = '.$iPlaceID;
255                     $sSQL .= ' and rank_address != 28';
256                     $sSQL .= ' and (name is not null or housenumber is not null';
257                     $sSQL .= ' or rank_address between 26 and 27)';
258                     $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
259                     $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
260                     $sSQL .= ' ORDER BY distance ASC limit 1';
261                     if (CONST_Debug) var_dump($sSQL);
262                     $aStreet = chksql(
263                         $this->oDB->getRow($sSQL),
264                         'Could not determine closest place.'
265                     );
266                     if ($aStreet) {
267                         $iDistance = $aPlace['distance'];
268                         $iPlaceID = $aStreet['place_id'];
269                         $oResult = new Result($iPlaceID);
270                         $iParentPlaceID = $aStreet['parent_place_id'];
271                             
272                         if ($bDoInterpolation && $iMaxRank >= 30) {
273                             $aHouse = $this->lookupInterpolation($sPointSQL, $iDistance, $iParentPlaceID);
274
275                             if ($aHouse) {
276                                 $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
277                                 $oResult->iHouseNumber = closestHouseNumber($aHouse);
278                             }
279                         }
280                     }
281                 }
282             // if no POI or street is found ...
283             } else {
284                 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
285                 if ($aPlace) {
286                     $oResult = new Result($aPlace['place_id']);
287                 } elseif (!$aPlace && $iMaxRank > 4) {
288                     $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
289                     if ($aPlace) {
290                         $oResult = new Result($aPlace['place_id']);
291                     }
292                 }
293             }
294             // lower than street level ($iMaxRank < 26 )
295         } else {
296             $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
297             if ($aPlace) {
298                 $oResult = new Result($aPlace['place_id']);
299             } elseif (!$aPlace && $iMaxRank > 4) {
300                 $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
301                 if ($aPlace) {
302                     $oResult = new Result($aPlace['place_id']);
303                 }
304             }
305         }
306         return $oResult;
307     }
308 }