2 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
3 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";
19 ini_set('memory_limit', '200M');
22 $sOutputFormat = 'xml';
23 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
25 $sOutputFormat = $_GET['format'];
29 $aLangPrefOrder = getPrefferedLangauges();
30 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
32 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
34 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'))
36 $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");
37 if (!$iPlaceID) $sError = 'OSM ID Not Found';
41 // Location to look up
42 $fLat = (float)$_GET['lat'];
43 $fLon = (float)$_GET['lon'];
44 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
46 // Zoom to rank, this could probably be calculated but a lookup gives fine control
48 0 => 2, // Continent / Sea
60 12 => 18, // Town / Village
64 16 => 26, // Street, TODO: major street?
66 18 => 30, // or >, Building
67 19 => 30, // or >, Building
69 $iMaxRank = isset($aZoomRank[$_GET['zoom']])?$aZoomRank[$_GET['zoom']]:28;
71 // Find the nearest point
72 $fSearchDiam = 0.0001;
75 $fMaxAreaDistance = 10;
76 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
78 $fSearchDiam = $fSearchDiam * 2;
80 // If we have to expand the search area by a large amount then we need a larger feature
81 // then there is a limit to how small the feature should be
82 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
83 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
84 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
85 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
86 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
87 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
88 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
89 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
91 $sSQL = 'select place_id,parent_place_id from placex';
92 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
93 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
94 $sSQL .= ' and (name is not null or housenumber is not null)';
95 $sSQL .= ' and class not in (\'waterway\')';
96 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
97 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
98 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
100 $aPlace = $oDB->getRow($sSQL);
101 $iPlaceID = $aPlace['place_id'];
102 if (PEAR::IsError($iPlaceID))
104 var_Dump($sSQL, $iPlaceID);
109 // The point we found might be too small - use the address to find what it is a child of
110 $sSQL = "select address_place_id from place_addressline where cached_rank_address <= $iMaxRank and place_id = $iPlaceID order by cached_rank_address desc,isaddress desc,distance desc";
112 $iPlaceID = $oDB->getOne($sSQL);
113 if (PEAR::IsError($iPlaceID))
115 var_Dump($sSQL, $iPlaceID);
118 if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
120 $sSQL = "select address_place_id from place_addressline where cached_rank_address <= $iMaxRank and place_id = ".$aPlace['place_id']." order by cached_rank_address desc,isaddress desc,distance desc";
122 $iPlaceID = $oDB->getOne($sSQL);
123 if (PEAR::IsError($iPlaceID))
125 var_Dump($sSQL, $iPlaceID);
131 $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 .= " from placex where place_id = $iPlaceID ";
142 $aPlace = $oDB->getRow($sSQL);
144 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
146 $aClassType = getClassTypes();
148 if (isset($aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]))
149 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]['simplelabel'];
150 elseif (isset($aClassType[$aPlace['class'].':'.$aPlace['type']]))
151 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type']]['simplelabel'];
152 else $sAddressType = $aPlace['class'];
153 $aPlace['addresstype'] = $sAddressType;
156 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');