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