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 (preg_match(CONST_BlockedUserAgents, $_SERVER["HTTP_USER_AGENT"]) > 0)
9 $fLoadAvg = getLoadAverage();
10 if ($fLoadAvg >= CONST_BlockReverseMaxLoad) {
11 header('HTTP/1.0 403 Forbidden');
12 header('Content-type: text/html; charset=utf-8');
13 echo "<html><body><h1>App temporarily blocked</h1>";
14 echo "Your application has been temporarily blocked from the OpenStreetMap Nominatim ";
15 echo "geolocation service due to high server load.";
16 echo "\n</body></html>\n";
23 if (strpos(CONST_BulkUserIPs, ','.$_SERVER["REMOTE_ADDR"].',') !== false)
25 $fLoadAvg = getLoadAverage();
26 if ($fLoadAvg > 2) sleep(60);
27 if ($fLoadAvg > 4) sleep(120);
30 echo "Bulk User: Temporary block due to high server load\n";
36 ini_set('memory_limit', '200M');
39 $sOutputFormat = 'xml';
40 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
42 $sOutputFormat = $_GET['format'];
45 // Show address breakdown
46 $bShowAddressDetails = true;
47 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
50 $aLangPrefOrder = getPreferredLanguages();
51 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
53 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
55 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'))
57 $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");
58 if (!$iPlaceID) $sError = 'OSM ID Not Found';
62 // Location to look up
63 $fLat = (float)$_GET['lat'];
64 $fLon = (float)$_GET['lon'];
65 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
67 // Zoom to rank, this could probably be calculated but a lookup gives fine control
69 0 => 2, // Continent / Sea
81 12 => 18, // Town / Village
85 16 => 26, // Street, TODO: major street?
87 18 => 30, // or >, Building
88 19 => 30, // or >, Building
90 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
92 // Find the nearest point
93 $fSearchDiam = 0.0001;
96 $fMaxAreaDistance = 1;
97 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
99 $fSearchDiam = $fSearchDiam * 2;
101 // If we have to expand the search area by a large amount then we need a larger feature
102 // then there is a limit to how small the feature should be
103 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
104 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
105 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
106 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
107 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
108 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
109 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
110 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
112 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
113 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
114 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
115 $sSQL .= ' and (name is not null or housenumber is not null)';
116 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
117 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
118 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
119 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
121 $aPlace = $oDB->getRow($sSQL);
122 if (PEAR::IsError($aPlace))
124 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
126 $iPlaceID = $aPlace['place_id'];
127 $iParentPlaceID = $aPlace['parent_place_id'];
130 // The point we found might be too small - use the address to find what it is a child of
131 if ($iPlaceID && $iMaxRank < 28)
133 if ($aPlace['rank_search'] > 28 && $iParentPlaceID) {
134 $iPlaceID = $iParentPlaceID;
136 $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";
137 $iPlaceID = $oDB->getOne($sSQL);
138 if (PEAR::IsError($iPlaceID))
140 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
144 $iPlaceID = $aPlace['place_id'];
151 $sSQL = "select placex.*,";
152 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
153 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
154 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
155 $sSQL .= " st_y(st_centroid(geometry)) as lat, st_x(st_centroid(geometry)) as lon";
156 $sSQL .= " from placex where place_id = $iPlaceID ";
158 $aPlace = $oDB->getRow($sSQL);
159 //var_dump($sSQL, $aPlace); exit;
161 if ($bShowAddressDetails)
163 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
165 $aClassType = getClassTypes();
167 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
168 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
170 $sAddressType = $aClassType[$aClassType]['simplelabel'];
174 $sClassType = $aPlace['class'].':'.$aPlace['type'];
175 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
176 $sAddressType = $aClassType[$sClassType]['simplelabel'];
177 else $sAddressType = $aPlace['class'];
179 $aPlace['addresstype'] = $sAddressType;
182 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');