]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
remove hard-coded DB name in munin plugins
[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 .= ' and indexed_status = 0 ';
104                         $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
105                         $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
106                         if (CONST_Debug) var_dump($sSQL);
107                         $aPlace = $oDB->getRow($sSQL);
108                         if (PEAR::IsError($aPlace))
109                         {
110                                 failInternalError("Could not determine closest place.", $sSQL, $aPlace);
111                         }
112                         $iPlaceID = $aPlace['place_id'];
113                         $iParentPlaceID = $aPlace['parent_place_id'];
114                 }
115
116                 // The point we found might be too small - use the address to find what it is a child of
117                 if ($iPlaceID && $iMaxRank < 28)
118                 {
119                         if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
120                         {
121                                 $iPlaceID = $iParentPlaceID;
122                         }
123                         $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";
124                         $iPlaceID = $oDB->getOne($sSQL);
125                         if (PEAR::IsError($iPlaceID))
126                         {
127                                 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
128                         }
129                         if (!$iPlaceID)
130                         {
131                                 $iPlaceID = $aPlace['place_id'];
132                         }
133                 }
134         }
135
136         if ($iPlaceID)
137         {
138                 $sSQL = "select placex.*,";
139                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
140                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
141                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
142                 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
143                 $sSQL .= " from placex where place_id = $iPlaceID ";
144
145                 $aPlace = $oDB->getRow($sSQL);
146                 //var_dump($sSQL, $aPlace); exit;
147
148                 if ($bShowAddressDetails)
149                 {
150                         $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
151                 }
152                 $aClassType = getClassTypes();
153                 $sAddressType = '';
154                 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
155                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
156                 {
157                         $sAddressType = $aClassType[$aClassType]['simplelabel'];
158                 }
159                 else
160                 {
161                         $sClassType = $aPlace['class'].':'.$aPlace['type'];
162                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
163                                 $sAddressType = $aClassType[$sClassType]['simplelabel'];
164                         else $sAddressType = $aPlace['class'];
165                 }
166                 $aPlace['addresstype'] = $sAddressType;
167
168         }
169
170         if (CONST_Debug) exit;
171
172         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');