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