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)
120 $iPlaceID = $iParentPlaceID;
122 $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";
123 $iPlaceID = $oDB->getOne($sSQL);
124 if (PEAR::IsError($iPlaceID))
126 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
130 $iPlaceID = $aPlace['place_id'];
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 .= " st_y(centroid) as lat, st_x(centroid) as lon";
142 $sSQL .= " from placex where place_id = $iPlaceID ";
144 $aPlace = $oDB->getRow($sSQL);
145 //var_dump($sSQL, $aPlace); exit;
147 if ($bShowAddressDetails)
149 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
151 $aClassType = getClassTypes();
153 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
154 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
156 $sAddressType = $aClassType[$aClassType]['simplelabel'];
160 $sClassType = $aPlace['class'].':'.$aPlace['type'];
161 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
162 $sAddressType = $aClassType[$sClassType]['simplelabel'];
163 else $sAddressType = $aPlace['class'];
165 $aPlace['addresstype'] = $sAddressType;
169 if (CONST_Debug) exit;
171 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');