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