]> git.openstreetmap.org Git - nominatim.git/blob - website/reverse.php
filter by feature rank, current options: country, state, city
[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 = 10;
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                 $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";
111 //var_dump($sSQL);
112                 $iPlaceID = $oDB->getOne($sSQL);
113                 if (PEAR::IsError($iPlaceID))
114                 {
115                         var_Dump($sSQL, $iPlaceID); 
116                         exit;
117                 }
118                 if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
119                 {
120                         $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";
121 //var_dump($sSQL);
122                         $iPlaceID = $oDB->getOne($sSQL);
123                         if (PEAR::IsError($iPlaceID))
124                         {
125                                 var_Dump($sSQL, $iPlaceID); 
126                                 exit;
127                         }
128                 }
129                 if (!$iPlaceID)
130                 {
131                         $iPlaceID = $aPlace['place_id'];
132                 }
133         }
134
135         if ($iPlaceID)
136         {
137                 $sSQL = "select placex.*,";
138                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
139                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
140                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref";
141                 $sSQL .= " from placex where place_id = $iPlaceID ";
142                 $aPlace = $oDB->getRow($sSQL);
143
144                 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
145
146                 $aClassType = getClassTypes();
147                 $sAddressType = '';
148                 if (isset($aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]))
149                         $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]['simplelabel'];
150                 elseif (isset($aClassType[$aPlace['class'].':'.$aPlace['type']]))
151                         $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type']]['simplelabel'];
152                 else $sAddressType = $aPlace['class'];
153                 $aPlace['addresstype'] = $sAddressType;
154
155         }
156         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');