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'];
28 // Show address breakdown
29 $bShowAddressDetails = true;
30 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
33 $aLangPrefOrder = getPrefferedLangauges();
34 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
36 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
38 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'))
40 $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");
41 if (!$iPlaceID) $sError = 'OSM ID Not Found';
45 // Location to look up
46 $fLat = (float)$_GET['lat'];
47 $fLon = (float)$_GET['lon'];
48 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
50 // Zoom to rank, this could probably be calculated but a lookup gives fine control
52 0 => 2, // Continent / Sea
64 12 => 18, // Town / Village
68 16 => 26, // Street, TODO: major street?
70 18 => 30, // or >, Building
71 19 => 30, // or >, Building
73 $iMaxRank = isset($aZoomRank[$_GET['zoom']])?$aZoomRank[$_GET['zoom']]:28;
75 // Find the nearest point
76 $fSearchDiam = 0.0001;
79 $fMaxAreaDistance = 1;
80 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
82 $fSearchDiam = $fSearchDiam * 2;
84 // If we have to expand the search area by a large amount then we need a larger feature
85 // then there is a limit to how small the feature should be
86 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
87 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
88 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
89 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
90 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
91 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
92 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
93 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
95 $sSQL = 'select place_id,parent_place_id from placex';
96 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
97 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
98 $sSQL .= ' and (name is not null or housenumber is not null)';
99 $sSQL .= ' and class not in (\'waterway\')';
100 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
101 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
102 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
104 $aPlace = $oDB->getRow($sSQL);
105 $iPlaceID = $aPlace['place_id'];
106 if (PEAR::IsError($iPlaceID))
108 var_Dump($sSQL, $iPlaceID);
113 // The point we found might be too small - use the address to find what it is a child of
116 $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 limit 1";
118 $iPlaceID = $oDB->getOne($sSQL);
119 if (PEAR::IsError($iPlaceID))
121 var_Dump($sSQL, $iPlaceID);
125 if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
127 $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";
129 $iPlaceID = $oDB->getOne($sSQL);
130 if (PEAR::IsError($iPlaceID))
132 var_Dump($sSQL, $iPlaceID);
138 $iPlaceID = $aPlace['place_id'];
145 $sSQL = "select placex.*,";
146 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
147 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
148 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
149 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
150 $sSQL .= " from placex where place_id = $iPlaceID ";
152 $aPlace = $oDB->getRow($sSQL);
154 if ($bShowAddressDetails)
156 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
158 $aClassType = getClassTypes();
160 if (isset($aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]))
161 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]['simplelabel'];
162 elseif (isset($aClassType[$aPlace['class'].':'.$aPlace['type']]))
163 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type']]['simplelabel'];
164 else $sAddressType = $aPlace['class'];
165 $aPlace['addresstype'] = $sAddressType;
168 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');