]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
remove debuing information
[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                                 var_Dump($sSQL, $iPlaceID); 
109                                 exit;
110                         }
111                 }
112
113                 // The point we found might be too small - use the address to find what it is a child of
114                 if ($iPlaceID)
115                 {
116                         $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";
117 //var_dump($sSQL);
118                         $iPlaceID = $oDB->getOne($sSQL);
119                         if (PEAR::IsError($iPlaceID))
120                         {
121                                 var_Dump($sSQL, $iPlaceID); 
122                                 exit;
123                         }
124
125                         if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
126                         {
127                                 $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";
128 //var_dump($sSQL);
129                                 $iPlaceID = $oDB->getOne($sSQL);
130                                 if (PEAR::IsError($iPlaceID))
131                                 {
132                                         var_Dump($sSQL, $iPlaceID); 
133                                         exit;
134                                 }
135                         }
136                         if (!$iPlaceID)
137                         {
138                                 $iPlaceID = $aPlace['place_id'];
139                         }
140                 }
141         }
142
143         if ($iPlaceID)
144         {
145                 $sSQL = "select placex.*,";
146                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
147                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
148                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
149                 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
150                 $sSQL .= " from placex where place_id = $iPlaceID ";
151 //var_dump($sSQL);
152                 $aPlace = $oDB->getRow($sSQL);
153
154                 if ($bShowAddressDetails)
155                 {
156                         $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
157                 }
158                 $aClassType = getClassTypes();
159                 $sAddressType = '';
160                 if (isset($aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]))
161                         $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]['simplelabel'];
162                 elseif (isset($aClassType[$aPlace['class'].':'.$aPlace['type']]))
163                         $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type']]['simplelabel'];
164                 else $sAddressType = $aPlace['class'];
165                 $aPlace['addresstype'] = $sAddressType;
166
167         }
168         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');