]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
second argument of array_merge can be empty
[nominatim.git] / lib / ReverseGeocode.php
1 <?php
2         class ReverseGeocode
3         {
4                 protected $oDB;
5
6                 protected $fLat;
7                 protected $fLon;
8                 protected $iMaxRank = 28;
9
10                 protected $aLangPrefOrder = array();
11
12                 protected $bIncludePolygonAsPoints = false;
13                 protected $bIncludePolygonAsText = false;
14                 protected $bIncludePolygonAsGeoJSON = false;
15                 protected $bIncludePolygonAsKML = false;
16                 protected $bIncludePolygonAsSVG = false;
17                 protected $fPolygonSimplificationThreshold = 0.0;
18
19
20                 function ReverseGeocode(&$oDB)
21                 {
22                         $this->oDB =& $oDB;
23                 }
24
25                 function setLanguagePreference($aLangPref)
26                 {
27                         $this->aLangPrefOrder = $aLangPref;
28                 }
29
30                 function setLatLon($fLat, $fLon)
31                 {
32                         $this->fLat = (float)$fLat;
33                         $this->fLon = (float)$fLon;
34                 }
35
36                 function setRank($iRank)
37                 {
38                         $this->iMaxRank = $iRank;
39                 }
40
41                 function setZoom($iZoom)
42                 {
43                         // Zoom to rank, this could probably be calculated but a lookup gives fine control
44                         $aZoomRank = array(
45                                 0 => 2, // Continent / Sea
46                                 1 => 2,
47                                 2 => 2,
48                                 3 => 4, // Country
49                                 4 => 4,
50                                 5 => 8, // State
51                                 6 => 10, // Region
52                                 7 => 10,
53                                 8 => 12, // County
54                                 9 => 12,
55                                 10 => 17, // City
56                                 11 => 17,
57                                 12 => 18, // Town / Village
58                                 13 => 18,
59                                 14 => 22, // Suburb
60                                 15 => 22,
61                                 16 => 26, // Street, TODO: major street?
62                                 17 => 26,
63                                 18 => 30, // or >, Building
64                                 19 => 30, // or >, Building
65                                 );
66                         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
67                 }
68
69                                 function setIncludePolygonAsPoints($b = true)
70                 {
71                         $this->bIncludePolygonAsPoints = $b;
72                 }
73
74                 function getIncludePolygonAsPoints()
75                 {
76                         return $this->bIncludePolygonAsPoints;
77                 }
78
79                 function setIncludePolygonAsText($b = true)
80                 {
81                         $this->bIncludePolygonAsText = $b;
82                 }
83
84                 function getIncludePolygonAsText()
85                 {
86                         return $this->bIncludePolygonAsText;
87                 }
88
89                 function setIncludePolygonAsGeoJSON($b = true)
90                 {
91                         $this->bIncludePolygonAsGeoJSON = $b;
92                 }
93
94                 function setIncludePolygonAsKML($b = true)
95                 {
96                         $this->bIncludePolygonAsKML = $b;
97                 }
98
99                 function setIncludePolygonAsSVG($b = true)
100                 {
101                         $this->bIncludePolygonAsSVG = $b;
102                 }
103
104                 function setPolygonSimplificationThreshold($f)
105                 {
106                         $this->fPolygonSimplificationThreshold = $f;
107                 }
108
109                 // returns { place_id =>, type => '(osm|tiger)' }
110                 // fails if no place was found
111                 function lookup()
112                 {
113                         $sPointSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
114                         $iMaxRank = $this->iMaxRank;
115                         $iMaxRank_orig = $this->iMaxRank;
116
117                         // Find the nearest point
118                         $fSearchDiam = 0.0004;
119                         $iPlaceID = null;
120                         $aArea = false;
121                         $fMaxAreaDistance = 1;
122                         $bIsInUnitedStates = false;
123                         $bPlaceIsTiger = false;
124                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
125                         {
126                                 $fSearchDiam = $fSearchDiam * 2;
127
128                                 // If we have to expand the search area by a large amount then we need a larger feature
129                                 // then there is a limit to how small the feature should be
130                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
131                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
132                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
133                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
134                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
135                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
136                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
137                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
138
139                                 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code';
140                                 $sSQL .= ' FROM placex';
141                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
142                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
143                                 $sSQL .= ' and (name is not null or housenumber is not null)';
144                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
145                                 $sSQL .= ' and indexed_status = 0 ';
146                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
147                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
148                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
149                                 if (CONST_Debug) var_dump($sSQL);
150                                 $aPlace = $this->oDB->getRow($sSQL);
151                                 if (PEAR::IsError($aPlace))
152                                 {
153                                         failInternalError("Could not determine closest place.", $sSQL, $aPlace);
154                                 }
155                                 $iPlaceID = $aPlace['place_id'];
156                                 $iParentPlaceID = $aPlace['parent_place_id'];
157                                 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
158                         }
159
160                         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
161                         if ($bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 )) 
162                         {
163                                 $fSearchDiam = 0.001;
164                                 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search ';
165                                 if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
166                                 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
167                                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.')';
168                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', centroid) ASC limit 1';
169
170
171                                 // print all house numbers in the parent (street)
172                                 if (CONST_Debug)
173                                 {
174                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
175                                         var_dump($sSQL);
176
177                                         $aAllHouses = $this->oDB->getAll($sSQL);
178                                         foreach($aAllHouses as $i)
179                                         {
180                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
181                                         }
182                                 }
183
184                                 $aPlaceTiger = $this->oDB->getRow($sSQL);
185                                 if (PEAR::IsError($aPlace))
186                                 {
187                                         failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
188                                 }
189                                 if ($aPlaceTiger)
190                                 {
191                                         if (CONST_Debug) var_dump('found Tiger place', $aPlaceTiger);
192                                         $bPlaceIsTiger = true;
193                                         $aPlace = $aPlaceTiger;
194                                         $iPlaceID = $aPlaceTiger['place_id'];
195                                         $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
196                                 }
197                         }
198
199                         // The point we found might be too small - use the address to find what it is a child of
200                         if ($iPlaceID && $iMaxRank < 28)
201                         {
202                                 if ($aPlace['rank_search'] > 28 && $iParentPlaceID && !$bPlaceIsTiger)
203                                 {
204                                         $iPlaceID = $iParentPlaceID;
205                                 }
206                                 $sSQL  = 'select address_place_id';
207                                 $sSQL .= ' FROM place_addressline';
208                                 $sSQL .= " WHERE place_id = $iPlaceID";
209                                 $sSQL .= " ORDER BY abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc";
210                                 $sSQL .= ' LIMIT 1';
211                                 $iPlaceID = $this->oDB->getOne($sSQL);
212                                 if (PEAR::IsError($iPlaceID))
213                                 {
214                                         failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
215                                 }
216                                 if (!$iPlaceID)
217                                 {
218                                         $iPlaceID = $aPlace['place_id'];
219                                 }
220                         }
221
222                         return array('place_id' => $iPlaceID,
223                                      'type' => $bPlaceIsTiger ? 'tiger' : 'osm');
224                 }
225                 
226         }
227 ?>