]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
Merge remote-tracking branch 'upstream/master'
[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     public function lookup($fLat, $fLon, $bDoInterpolation = true)
73     {
74         return $this->lookupPoint(
75             'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
76             $bDoInterpolation
77         );
78     }
79
80     public function lookupPoint($sPointSQL, $bDoInterpolation = true)
81     {
82         $iMaxRank = $this->iMaxRank;
83
84         // Find the nearest point
85         $fSearchDiam = 0.0004;
86         $oResult = null;
87         $aPlace = null;
88         $fMaxAreaDistance = 1;
89         $bIsTigerStreet = false;
90         while ($oResult === null && $fSearchDiam < $fMaxAreaDistance) {
91             $fSearchDiam = $fSearchDiam * 2;
92
93             // If we have to expand the search area by a large amount then we need a larger feature
94             // then there is a limit to how small the feature should be
95             if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
96             if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
97             if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
98             if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
99             if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
100             if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
101             if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
102             if ($fSearchDiam > 0.001 && $iMaxRank > 26) {
103                 // try with interpolations before continuing
104                 if ($bDoInterpolation) {
105                     $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/2);
106
107                     if ($aHouse) {
108                         $oResult = new Result($aHouse['place_id'], Result::TABLE_OSMLINE);
109                         $oResult->iHouseNumber = closestHouseNumber($aHouse);
110
111                         $aPlace = $aHouse;
112                         $iParentPlaceID = $aHouse['parent_place_id']; // the street
113                         $iMaxRank = 30;
114
115                         break;
116                     }
117                 }
118                 // no interpolation found, continue search
119                 $iMaxRank = 26;
120             }
121
122             $sSQL = 'select place_id,parent_place_id,rank_search,country_code,';
123             $sSQL .= '  ST_distance('.$sPointSQL.', geometry) as distance';
124             $sSQL .= ' FROM ';
125             if ($fSearchDiam < 0.01) {
126                 $sSQL .= ' placex';
127                 $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
128                 $sSQL .= '   AND';
129             } else {
130                 $sSQL .= ' (SELECT * FROM placex ';
131                 $sSQL .= '   WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
132                 $sSQL .= '   LIMIT 1000) as p WHERE';
133             }
134             $sSQL .= ' rank_search != 28 and rank_search >= '.$iMaxRank;
135             $sSQL .= ' and (name is not null or housenumber is not null)';
136             $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
137             $sSQL .= ' and indexed_status = 0 ';
138             $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
139             $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
140             $sSQL .= ' ORDER BY distance ASC limit 1';
141             if (CONST_Debug) var_dump($sSQL);
142             $aPlace = chksql(
143                 $this->oDB->getRow($sSQL),
144                 "Could not determine closest place."
145             );
146             if ($aPlace) {
147                 $oResult = new Result($aPlace['place_id']);
148                 $iParentPlaceID = $aPlace['parent_place_id'];
149                 if ($bDoInterpolation) {
150                     if ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27) {
151                         $bIsTigerStreet = ($aPlace['country_code'] == 'us');
152                     } elseif ($aPlace['rank_search'] == 30) {
153                         // If a house was found, make sure there isn't an
154                         // interpolation line that is closer.
155                         $aHouse = $this->lookupInterpolation(
156                             $sPointSQL,
157                             $aPlace['distance']
158                         );
159                         if ($aHouse && $aPlace['distance'] < $aHouse['distance']) {
160                             $oResult = new Result(
161                                 $aHouse['place_id'],
162                                 Result::TABLE_OSMLINE
163                             );
164                             $oResult->iHouseNumber = closestHouseNumber($aHouse);
165
166                             $aPlace = $aHouse;
167                             $iParentPlaceID = $aHouse['parent_place_id'];
168                         }
169                     }
170                 }
171             }
172         }
173
174         // Only street found? In the US we can check TIGER data for nearest housenumber
175         if (CONST_Use_US_Tiger_Data && $bIsTigerStreet && $this->iMaxRank >= 28) {
176             $fSearchDiam = $aPlace['rank_search'] > 28 ? $aPlace['distance'] : 0.001;
177             $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search,';
178             $sSQL .= 'ST_LineLocatePoint(linegeo,'.$sPointSQL.') as fraction,';
179             $sSQL .= 'ST_distance('.$sPointSQL.', linegeo) as distance,';
180             $sSQL .= 'startnumber,endnumber,interpolationtype';
181             $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$oResult->iId;
182             $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
183             $sSQL .= ' ORDER BY distance ASC limit 1';
184
185             if (CONST_Debug) var_dump($sSQL);
186
187             $aPlaceTiger = chksql(
188                 $this->oDB->getRow($sSQL),
189                 "Could not determine closest Tiger place."
190             );
191             if ($aPlaceTiger) {
192                 if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
193                 $aPlace = $aPlaceTiger;
194                 $oResult = new Result($aPlace['place_id'], Result::TABLE_TIGER);
195                 $oResult->iHouseNumber = closestHouseNumber($aPlaceTiger);
196                 $iParentPlaceID = $aPlace['parent_place_id'];
197                 $iMaxRank = 30;
198             }
199         }
200
201         // The point we found might be too small - use the address to find what it is a child of
202         if ($oResult !== null && $iMaxRank < 28) {
203             if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
204                 $iPlaceID = $iParentPlaceID;
205             } else {
206                 $iPlaceID = $oResult->iId;
207             }
208             $sSQL  = 'select address_place_id';
209             $sSQL .= ' FROM place_addressline';
210             $sSQL .= " WHERE place_id = $iPlaceID";
211             $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
212             $sSQL .= ' LIMIT 1';
213             $iPlaceID = chksql($this->oDB->getOne($sSQL), "Could not get parent for place.");
214             if ($iPlaceID) {
215                 $oResult = new Result($iPlaceID);
216             }
217         }
218
219         return $oResult;
220     }
221 }