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