]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / website / reverse.php
1 <?php
2         @define('CONST_ConnectionBucket_PageType', 'Reverse');
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5         require_once(CONST_BasePath.'/lib/log.php');
6
7     if (preg_match(CONST_BlockedUserAgents, $_SERVER["HTTP_USER_AGENT"]) > 0)
8     {
9         $fLoadAvg = getLoadAverage();
10         if ($fLoadAvg >= CONST_BlockReverseMaxLoad) {
11             header('HTTP/1.0 403 Forbidden');
12             header('Content-type: text/html; charset=utf-8');
13                 echo "<html><body><h1>App temporarily blocked</h1>";
14             echo "Your application has been temporarily blocked from the OpenStreetMap Nominatim ";
15             echo "geolocation service due to high server load.";
16             echo "\n</body></html>\n";
17             exit;
18         }
19
20     }
21
22
23         if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
24         {
25                 $fLoadAvg = getLoadAverage();
26                 if ($fLoadAvg > 2) sleep(60);
27                 if ($fLoadAvg > 4) sleep(120);
28                 if ($fLoadAvg > 6)
29                 {
30                         echo "Bulk User: Temporary block due to high server load\n";
31                         exit;
32                 }
33         }
34
35         $oDB =& getDB();
36         ini_set('memory_limit', '200M');
37
38         // Format for output
39         $sOutputFormat = 'xml';
40         if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
41         {
42                 $sOutputFormat = $_GET['format'];
43         }
44
45         // Show address breakdown
46         $bShowAddressDetails = true;
47         if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
48
49         // Preferred language
50         $aLangPrefOrder = getPreferredLanguages();
51         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
52
53         $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
54
55         if (isset($_GET['osm_type']) && isset($_GET['osm_id']) && (int)$_GET['osm_id'] && ($_GET['osm_type'] == 'N' || $_GET['osm_type'] == 'W' || $_GET['osm_type'] == 'R'))
56         {
57                 $iPlaceID = $oDB->getOne($sSQL = ("select place_id from placex where osm_type = '".$_GET['osm_type']."' and osm_id = ".(int)$_GET['osm_id']." order by type = 'postcode' asc"));
58                 if (CONST_Debug) var_dump($sSQL);
59                 if (!$iPlaceID) $sError = 'OSM ID Not Found';
60         }
61         else
62         {
63                 // Location to look up
64                 $fLat = (float)$_GET['lat'];
65                 $fLon = (float)$_GET['lon'];
66                 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
67
68                 // Zoom to rank, this could probably be calculated but a lookup gives fine control
69                 $aZoomRank = array(
70                         0 => 2, // Continent / Sea
71                         1 => 2,
72                         2 => 2,
73                         3 => 4, // Country
74                         4 => 4,
75                         5 => 8, // State
76                         6 => 10, // Region
77                         7 => 10,
78                         8 => 12, // County
79                         9 => 12,
80                         10 => 17, // City
81                         11 => 17,
82                         12 => 18, // Town / Village
83                         13 => 18,
84                         14 => 22, // Suburb
85                         15 => 22,
86                         16 => 26, // Street, TODO: major street?
87                         17 => 26,
88                         18 => 30, // or >, Building
89                         19 => 30, // or >, Building
90                         );
91                 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
92
93                 // Find the nearest point
94                 $fSearchDiam = 0.0004;
95                 $iPlaceID = null;
96                 $aArea = false;
97                 $fMaxAreaDistance = 1;
98                 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
99                 {
100                         $fSearchDiam = $fSearchDiam * 2;
101
102                         // If we have to expand the search area by a large amount then we need a larger feature
103                         // then there is a limit to how small the feature should be
104                         if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
105                         if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
106                         if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
107                         if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
108                         if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
109                         if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
110                         if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
111                         if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
112
113                         $sSQL = 'select place_id,parent_place_id,rank_search from placex';
114                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
115                         $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
116                         $sSQL .= ' and (name is not null or housenumber is not null)';
117                         $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
118                         $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
119                         $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
120                         $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
121                         if (CONST_Debug) var_dump($sSQL);
122                         $aPlace = $oDB->getRow($sSQL);
123                         if (PEAR::IsError($aPlace))
124                         {
125                                 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID); 
126                         }
127                         $iPlaceID = $aPlace['place_id'];
128                         $iParentPlaceID = $aPlace['parent_place_id'];
129                 }
130
131                 // The point we found might be too small - use the address to find what it is a child of
132                 if ($iPlaceID && $iMaxRank < 28)
133                 {
134                         if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
135                                 $iPlaceID = $iParentPlaceID;
136                         }
137                         $sSQL = "select address_place_id from place_addressline where place_id = $iPlaceID order by abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc limit 1";
138                         $iPlaceID = $oDB->getOne($sSQL);
139                         if (PEAR::IsError($iPlaceID))
140                         {
141                                 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID); 
142                         }
143                         if (!$iPlaceID)
144                         {
145                                 $iPlaceID = $aPlace['place_id'];
146                         }
147                 }
148         }
149
150         if ($iPlaceID)
151         {
152                 $sSQL = "select placex.*,";
153                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
154                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
155                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
156                 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
157                 $sSQL .= " from placex where place_id = $iPlaceID ";
158
159                 $aPlace = $oDB->getRow($sSQL);
160 //var_dump($sSQL, $aPlace); exit;
161
162                 if ($bShowAddressDetails)
163                 {
164                         $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
165                 }
166                 $aClassType = getClassTypes();
167                 $sAddressType = '';
168                 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
169                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
170                 {
171                         $sAddressType = $aClassType[$aClassType]['simplelabel'];
172                 }
173                 else
174                 {
175                         $sClassType = $aPlace['class'].':'.$aPlace['type'];
176                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
177                                 $sAddressType = $aClassType[$sClassType]['simplelabel'];
178                         else $sAddressType = $aPlace['class'];
179                 }
180                 $aPlace['addresstype'] = $sAddressType;
181
182         }
183
184         if (CONST_Debug) exit;
185
186         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');