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("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 (!$iPlaceID) $sError = 'OSM ID Not Found';
46 // Location to look up
47 $fLat = (float)$_GET['lat'];
48 $fLon = (float)$_GET['lon'];
49 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
51 // Zoom to rank, this could probably be calculated but a lookup gives fine control
53 0 => 2, // Continent / Sea
65 12 => 18, // Town / Village
69 16 => 26, // Street, TODO: major street?
71 18 => 30, // or >, Building
72 19 => 30, // or >, Building
74 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
76 // Find the nearest point
77 $fSearchDiam = 0.0001;
80 $fMaxAreaDistance = 1;
81 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
83 $fSearchDiam = $fSearchDiam * 2;
85 // If we have to expand the search area by a large amount then we need a larger feature
86 // then there is a limit to how small the feature should be
87 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
88 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
89 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
90 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
91 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
92 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
93 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
94 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
96 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
97 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
98 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
99 $sSQL .= ' and (name is not null or housenumber is not null)';
100 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
101 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
102 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
103 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
105 $aPlace = $oDB->getRow($sSQL);
106 if (PEAR::IsError($aPlace))
108 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
110 $iPlaceID = $aPlace['place_id'];
111 $iParentPlaceID = $aPlace['parent_place_id'];
114 // The point we found might be too small - use the address to find what it is a child of
115 if ($iPlaceID && $iMaxRank < 28)
117 if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
118 $iPlaceID = $iParentPlaceID;
120 $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";
121 $iPlaceID = $oDB->getOne($sSQL);
122 if (PEAR::IsError($iPlaceID))
124 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
128 $iPlaceID = $aPlace['place_id'];
135 $sSQL = "select placex.*,";
136 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
137 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
138 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
139 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
140 $sSQL .= " from placex where place_id = $iPlaceID ";
142 $aPlace = $oDB->getRow($sSQL);
144 if ($bShowAddressDetails)
146 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
148 $aClassType = getClassTypes();
150 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
151 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
153 $sAddressType = $aClassType[$aClassType]['simplelabel'];
157 $sClassType = $aPlace['class'].':'.$aPlace['type'];
158 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
159 $sAddressType = $aClassType[$sClassType]['simplelabel'];
160 else $sAddressType = $aPlace['class'];
162 $aPlace['addresstype'] = $sAddressType;
165 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');