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