]> 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("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 (!$iPlaceID) $sError = 'OSM ID Not Found';
59         }
60         else
61         {
62                 // Location to look up
63                 $fLat = (float)$_GET['lat'];
64                 $fLon = (float)$_GET['lon'];
65                 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
66
67                 // Zoom to rank, this could probably be calculated but a lookup gives fine control
68                 $aZoomRank = array(
69                         0 => 2, // Continent / Sea
70                         1 => 2,
71                         2 => 2,
72                         3 => 4, // Country
73                         4 => 4,
74                         5 => 8, // State
75                         6 => 10, // Region
76                         7 => 10,
77                         8 => 12, // County
78                         9 => 12,
79                         10 => 17, // City
80                         11 => 17,
81                         12 => 18, // Town / Village
82                         13 => 18,
83                         14 => 22, // Suburb
84                         15 => 22,
85                         16 => 26, // Street, TODO: major street?
86                         17 => 26,
87                         18 => 30, // or >, Building
88                         19 => 30, // or >, Building
89                         );
90                 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
91
92                 // Find the nearest point
93                 $fSearchDiam = 0.0001;
94                 $iPlaceID = null;
95                 $aArea = false;
96                 $fMaxAreaDistance = 1;
97                 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
98                 {
99                         $fSearchDiam = $fSearchDiam * 2;
100
101                         // If we have to expand the search area by a large amount then we need a larger feature
102                         // then there is a limit to how small the feature should be
103                         if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
104                         if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
105                         if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
106                         if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
107                         if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
108                         if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
109                         if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
110                         if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
111
112                         $sSQL = 'select place_id,parent_place_id,rank_search from placex';
113                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
114                         $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
115                         $sSQL .= ' and (name is not null or housenumber is not null)';
116                         $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
117                         $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
118                         $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
119                         $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
120 //var_dump($sSQL);
121                         $aPlace = $oDB->getRow($sSQL);
122                         if (PEAR::IsError($aPlace))
123                         {
124                                 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID); 
125                         }
126                         $iPlaceID = $aPlace['place_id'];
127                         $iParentPlaceID = $aPlace['parent_place_id'];
128                 }
129
130                 // The point we found might be too small - use the address to find what it is a child of
131                 if ($iPlaceID && $iMaxRank < 28)
132                 {
133                         if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
134                                 $iPlaceID = $iParentPlaceID;
135                         }
136                         $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";
137                         $iPlaceID = $oDB->getOne($sSQL);
138                         if (PEAR::IsError($iPlaceID))
139                         {
140                                 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID); 
141                         }
142                         if (!$iPlaceID)
143                         {
144                                 $iPlaceID = $aPlace['place_id'];
145                         }
146                 }
147         }
148
149         if ($iPlaceID)
150         {
151                 $sSQL = "select placex.*,";
152                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
153                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
154                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
155                 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
156                 $sSQL .= " from placex where place_id = $iPlaceID ";
157
158                 $aPlace = $oDB->getRow($sSQL);
159 //var_dump($sSQL, $aPlace); exit;
160
161                 if ($bShowAddressDetails)
162                 {
163                         $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
164                 }
165                 $aClassType = getClassTypes();
166                 $sAddressType = '';
167                 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
168                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
169                 {
170                         $sAddressType = $aClassType[$aClassType]['simplelabel'];
171                 }
172                 else
173                 {
174                         $sClassType = $aPlace['class'].':'.$aPlace['type'];
175                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
176                                 $sAddressType = $aClassType[$sClassType]['simplelabel'];
177                         else $sAddressType = $aPlace['class'];
178                 }
179                 $aPlace['addresstype'] = $sAddressType;
180
181         }
182         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');