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