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