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