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