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