2 @define('CONST_ConnectionBucket_PageType', 'Reverse');
4 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5 require_once(CONST_BasePath.'/lib/log.php');
7 if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
9 $fLoadAvg = getLoadAverage();
10 if ($fLoadAvg > 2) sleep(60);
11 if ($fLoadAvg > 4) sleep(120);
14 echo "Bulk User: Temporary block due to high server load\n";
20 ini_set('memory_limit', '200M');
23 $sOutputFormat = 'xml';
24 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
26 $sOutputFormat = $_GET['format'];
29 // Show address breakdown
30 $bShowAddressDetails = true;
31 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
34 $aLangPrefOrder = getPreferredLanguages();
35 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
37 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
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'))
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';
47 // Location to look up
48 $fLat = (float)$_GET['lat'];
49 $fLon = (float)$_GET['lon'];
50 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
52 // Zoom to rank, this could probably be calculated but a lookup gives fine control
54 0 => 2, // Continent / Sea
66 12 => 18, // Town / Village
70 16 => 26, // Street, TODO: major street?
72 18 => 30, // or >, Building
73 19 => 30, // or >, Building
75 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
77 // Find the nearest point
78 $fSearchDiam = 0.0004;
81 $fMaxAreaDistance = 1;
82 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
84 $fSearchDiam = $fSearchDiam * 2;
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;
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 .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
104 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
105 if (CONST_Debug) var_dump($sSQL);
106 $aPlace = $oDB->getRow($sSQL);
107 if (PEAR::IsError($aPlace))
109 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
111 $iPlaceID = $aPlace['place_id'];
112 $iParentPlaceID = $aPlace['parent_place_id'];
115 // The point we found might be too small - use the address to find what it is a child of
116 if ($iPlaceID && $iMaxRank < 28)
118 if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
119 $iPlaceID = $iParentPlaceID;
121 $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";
122 $iPlaceID = $oDB->getOne($sSQL);
123 if (PEAR::IsError($iPlaceID))
125 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
129 $iPlaceID = $aPlace['place_id'];
136 $sSQL = "select placex.*,";
137 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
138 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
139 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
140 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
141 $sSQL .= " from placex where place_id = $iPlaceID ";
143 $aPlace = $oDB->getRow($sSQL);
144 //var_dump($sSQL, $aPlace); exit;
146 if ($bShowAddressDetails)
148 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
150 $aClassType = getClassTypes();
152 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
153 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
155 $sAddressType = $aClassType[$aClassType]['simplelabel'];
159 $sClassType = $aPlace['class'].':'.$aPlace['type'];
160 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
161 $sAddressType = $aClassType[$sClassType]['simplelabel'];
162 else $sAddressType = $aPlace['class'];
164 $aPlace['addresstype'] = $sAddressType;
168 if (CONST_Debug) exit;
170 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');