2 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
3 require_once(CONST_BasePath.'/lib/log.php');
5 if (preg_match(CONST_BlockedUserAgents, $_SERVER["HTTP_USER_AGENT"]) > 0)
7 $fLoadAvg = getLoadAverage();
8 if ($fLoadAvg >= CONST_BlockReverseMaxLoad) {
9 header('HTTP/1.0 403 Forbidden');
10 header('Content-type: text/html; charset=utf-8');
11 echo "<html><body><h1>App temporarily blocked</h1>";
12 echo "Your application has been temporarily blocked from the OpenStreetMap Nominatim ";
13 echo "geolocation service due to high server load.";
14 echo "\n</body></html>\n";
21 if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
23 $fLoadAvg = getLoadAverage();
24 if ($fLoadAvg > 2) sleep(60);
25 if ($fLoadAvg > 4) sleep(120);
28 echo "Bulk User: Temporary block due to high server load\n";
34 ini_set('memory_limit', '200M');
37 $sOutputFormat = 'xml';
38 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
40 $sOutputFormat = $_GET['format'];
43 // Show address breakdown
44 $bShowAddressDetails = true;
45 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
48 $aLangPrefOrder = getPrefferedLangauges();
49 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
51 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
53 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'))
55 $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");
56 if (!$iPlaceID) $sError = 'OSM ID Not Found';
60 // Location to look up
61 $fLat = (float)$_GET['lat'];
62 $fLon = (float)$_GET['lon'];
63 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
65 // Zoom to rank, this could probably be calculated but a lookup gives fine control
67 0 => 2, // Continent / Sea
79 12 => 18, // Town / Village
83 16 => 26, // Street, TODO: major street?
85 18 => 30, // or >, Building
86 19 => 30, // or >, Building
88 $iMaxRank = isset($aZoomRank[$_GET['zoom']])?$aZoomRank[$_GET['zoom']]:28;
90 // Find the nearest point
91 $fSearchDiam = 0.0001;
94 $fMaxAreaDistance = 1;
95 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
97 $fSearchDiam = $fSearchDiam * 2;
99 // If we have to expand the search area by a large amount then we need a larger feature
100 // then there is a limit to how small the feature should be
101 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
102 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
103 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
104 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
105 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
106 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
107 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
108 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
110 $sSQL = 'select place_id,parent_place_id from placex';
111 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
112 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
113 $sSQL .= ' and (name is not null or housenumber is not null)';
114 $sSQL .= ' and class not in (\'waterway\')';
115 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
116 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
117 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
119 $aPlace = $oDB->getRow($sSQL);
120 $iPlaceID = $aPlace['place_id'];
121 if (PEAR::IsError($iPlaceID))
123 var_Dump($sSQL, $iPlaceID);
128 // The point we found might be too small - use the address to find what it is a child of
131 $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";
133 $iPlaceID = $oDB->getOne($sSQL);
134 if (PEAR::IsError($iPlaceID))
136 var_Dump($sSQL, $iPlaceID);
140 if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
142 $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";
144 $iPlaceID = $oDB->getOne($sSQL);
145 if (PEAR::IsError($iPlaceID))
147 var_Dump($sSQL, $iPlaceID);
153 $iPlaceID = $aPlace['place_id'];
160 $sSQL = "select placex.*,";
161 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
162 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
163 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
164 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
165 $sSQL .= " from placex where place_id = $iPlaceID ";
167 $aPlace = $oDB->getRow($sSQL);
169 if ($bShowAddressDetails)
171 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
173 $aClassType = getClassTypes();
175 if (isset($aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]))
176 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level']]['simplelabel'];
177 elseif (isset($aClassType[$aPlace['class'].':'.$aPlace['type']]))
178 $sAddressType = $aClassType[$aPlace['class'].':'.$aPlace['type']]['simplelabel'];
179 else $sAddressType = $aPlace['class'];
180 $aPlace['addresstype'] = $sAddressType;
183 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');