]> git.openstreetmap.org Git - nominatim.git/blob - reverse.php
images and javascript
[nominatim.git] / reverse.php
1 <?php
2
3         require_once('.htlib/init.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         ini_set('memory_limit', '200M');
18
19         // Format for output
20         $sOutputFormat = 'xml';
21         if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json'))
22         {
23                 $sOutputFormat = $_GET['format'];
24         }
25
26         // Prefered language
27         $aLangPrefOrder = getPrefferedLangauges();
28         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
29
30         $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
31
32         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'))
33         {
34                 $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");
35                 if (!$iPlaceID) $sError = 'OSM ID Not Found';
36         }
37         else
38         {
39                 // Location to look up
40                 $fLat = (float)$_GET['lat'];
41                 $fLon = (float)$_GET['lon'];
42                 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
43
44                 // Zoom to rank, this could probably be calculated but a lookup gives fine control
45                 $aZoomRank = array(
46                         0 => 2, // Continent / Sea
47                         1 => 2,
48                         2 => 2,
49                         3 => 4, // Country
50                         4 => 4,
51                         5 => 8, // State
52                         6 => 10, // Region
53                         7 => 10, 
54                         8 => 12, // County
55                         9 => 12,  
56                         10 => 17, // City
57                         11 => 17, 
58                         12 => 18, // Town / Village
59                         13 => 18, 
60                         14 => 22, // Suburb
61                         15 => 22,
62                         16 => 26, // Street, TODO: major street?
63                         17 => 26, 
64                         18 => 28, // or >, Building
65                         19 => 30, // or >, Building
66                         );
67                 $iMaxRank = isset($aZoomRank[$_GET['zoom']])?$aZoomRank[$_GET['zoom']]:28;
68
69                 // Find the nearest point
70                 $fSearchDiam = 0.0001;
71                 $iPlaceID = null;
72                 $aArea = false;
73                 $fMaxAreaDistance = 10;
74                 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
75                 {
76                         $fSearchDiam = $fSearchDiam * 2;
77
78                         // If we have to expand the search area by a large amount then we need a larger feature
79                         // then there is a limit to how small the feature should be
80                         if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
81                         if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
82                         if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
83                         if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
84                         if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
85                         if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
86                         if ($fSearchDiam > 0.01 && $iMaxRank > 22) $iMaxRank = 22;
87
88                         if ($iMaxRank >= 26)
89                         {
90                                 // Street level search is done using placex table
91                                 $sSQL = 'select place_id from placex';
92                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
93                                 $sSQL .= ' and rank_search >= 26 and rank_search <= '.$iMaxRank;
94                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
95                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
96                                 $sSQL .= ' ORDER BY rank_search desc, ST_distance('.$sPointSQL.', geometry) ASC limit 1';
97                                 $iPlaceID = $oDB->getOne($sSQL);
98                                 if (PEAR::IsError($iPlaceID))
99                                 {
100                                         var_Dump($sSQL, $iPlaceID); 
101                                         exit;
102                                 }
103                         }
104                         else
105                         {
106                                 // Other search uses the location_point and location_area tables
107
108                                 // If we've not yet done the area search do it now
109                                 if ($aArea === false)
110                                 {
111                                         $sSQL = 'select place_id,rank_address,ST_distance('.$sPointSQL.', centroid) as distance from location_area';
112                                         $sSQL .= ' WHERE ST_Contains(area,'.$sPointSQL.') and rank_search <= '.$iMaxRank;
113                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
114                                         $aArea = $oDB->getRow($sSQL);
115                                         if ($aArea) $fMaxAreaDistance = $aArea['distance'];
116                                 }
117
118                                 // Different search depending if we found an area match
119                                 if ($aArea)
120                                 {
121                                         // Found best match area - is there a better point match?
122                                         $sSQL = 'select place_id from location_point_'.($iMaxRank+1);
123                                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.') ';
124                                         $sSQL .= ' and rank_search > '.($aArea['rank_address']+3);
125                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
126                                         $iPlaceID = $oDB->getOne($sSQL);
127                                         if (PEAR::IsError($iPlaceID))
128                                         {
129                                                 var_Dump($sSQL, $iPlaceID); 
130                                                 exit;
131                                         }
132                                 }
133                                 else
134                                 {
135                                         $sSQL = 'select place_id from location_point_'.($iMaxRank+1);
136                                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.') ';
137                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
138                                         $iPlaceID = $oDB->getOne($sSQL);
139                                         if (PEAR::IsError($iPlaceID))
140                                         {
141                                                 var_Dump($sSQL, $iPlaceID); 
142                                                 exit;
143                                         }
144                                 }
145                         }
146                 }
147                 if (!$iPlaceID && $aArea) $iPlaceID = $aArea['place_id'];
148         }
149
150         if ($iPlaceID)
151         {
152                 $sSQL = "select placex.*,";
153                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
154                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
155                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref";
156                 $sSQL .= " from placex where place_id = $iPlaceID ";
157                 $aPlace = $oDB->getRow($sSQL);
158
159                 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
160         }
161         include('.htlib/output/address-'.$sOutputFormat.'.php');