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 .= ' and indexed_status = 0 ';
104 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
105 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
106 if (CONST_Debug) var_dump($sSQL);
107 $aPlace = $oDB->getRow($sSQL);
108 if (PEAR::IsError($aPlace))
110 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
112 $iPlaceID = $aPlace['place_id'];
113 $iParentPlaceID = $aPlace['parent_place_id'];
116 // The point we found might be too small - use the address to find what it is a child of
117 if ($iPlaceID && $iMaxRank < 28)
119 if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
121 $iPlaceID = $iParentPlaceID;
123 $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";
124 $iPlaceID = $oDB->getOne($sSQL);
125 if (PEAR::IsError($iPlaceID))
127 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
131 $iPlaceID = $aPlace['place_id'];
138 $sSQL = "select placex.*,";
139 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
140 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
141 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
142 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
143 $sSQL .= " from placex where place_id = $iPlaceID ";
145 $aPlace = $oDB->getRow($sSQL);
146 //var_dump($sSQL, $aPlace); exit;
148 if ($bShowAddressDetails)
150 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
152 $aClassType = getClassTypes();
154 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
155 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
157 $sAddressType = $aClassType[$aClassType]['simplelabel'];
161 $sClassType = $aPlace['class'].':'.$aPlace['type'];
162 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
163 $sAddressType = $aClassType[$sClassType]['simplelabel'];
164 else $sAddressType = $aPlace['class'];
166 $aPlace['addresstype'] = $sAddressType;
170 if (CONST_Debug) exit;
172 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');