2 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
3 require_once(CONST_BasePath.'/lib/log.php');
5 if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
7 $fLoadAvg = getLoadAverage();
8 if ($fLoadAvg > 2) sleep(60);
9 if ($fLoadAvg > 4) sleep(120);
12 echo "Bulk User: Temporary block due to high server load\n";
18 ini_set('memory_limit', '200M');
21 $sOutputFormat = 'xml';
22 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
24 $sOutputFormat = $_GET['format'];
27 // Show address breakdown
28 $bShowAddressDetails = true;
29 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
32 $aLangPrefOrder = getPrefferedLangauges();
33 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
35 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
37 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'))
39 $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");
40 if (!$iPlaceID) $sError = 'OSM ID Not Found';
44 // Location to look up
45 $fLat = (float)$_GET['lat'];
46 $fLon = (float)$_GET['lon'];
47 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
49 // Zoom to rank, this could probably be calculated but a lookup gives fine control
51 0 => 2, // Continent / Sea
63 12 => 18, // Town / Village
67 16 => 26, // Street, TODO: major street?
69 18 => 30, // or >, Building
70 19 => 30, // or >, Building
72 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
74 // Find the nearest point
75 $fSearchDiam = 0.0001;
78 $fMaxAreaDistance = 1;
79 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
81 $fSearchDiam = $fSearchDiam * 2;
83 // If we have to expand the search area by a large amount then we need a larger feature
84 // then there is a limit to how small the feature should be
85 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
86 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
87 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
88 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
89 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
90 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
91 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
92 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
94 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
95 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
96 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
97 $sSQL .= ' and (name is not null or housenumber is not null)';
98 $sSQL .= ' and class not in (\'waterway\')';
99 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
100 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
101 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
103 $aPlace = $oDB->getRow($sSQL);
104 $iPlaceID = $aPlace['place_id'];
105 $iParentPlaceID = $aPlace['parent_place_id'];
106 if (PEAR::IsError($iPlaceID))
108 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
112 // The point we found might be too small - use the address to find what it is a child of
113 if ($iPlaceID && $iMaxRank < 28)
115 if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
116 $iPlaceID = $iParentPlaceID;
118 $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";
119 $iPlaceID = $oDB->getOne($sSQL);
120 if (PEAR::IsError($iPlaceID))
122 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
126 $iPlaceID = $aPlace['place_id'];
133 $sSQL = "select placex.*,";
134 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
135 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
136 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
137 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
138 $sSQL .= " from placex where place_id = $iPlaceID ";
140 $aPlace = $oDB->getRow($sSQL);
142 if ($bShowAddressDetails)
144 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
146 $aClassType = getClassTypes();
148 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
149 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
151 $sAddressType = $aClassType[$aClassType]['simplelabel'];
155 $sClassType = $aPlace['class'].':'.$aPlace['type'];
156 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
157 $sAddressType = $aClassType[$sClassType]['simplelabel'];
158 else $sAddressType = $aPlace['class'];
160 $aPlace['addresstype'] = $sAddressType;
163 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');