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