2 @define('CONST_ConnectionBucket_PageType', 'Reverse');
4 require_once(dirname(dirname(__FILE__)).'/lib/init-website.php');
5 require_once(CONST_BasePath.'/lib/log.php');
8 ini_set('memory_limit', '200M');
11 $sOutputFormat = 'xml';
12 if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json' || $_GET['format'] == 'jsonv2'))
14 $sOutputFormat = $_GET['format'];
17 // Show address breakdown
18 $bShowAddressDetails = true;
19 if (isset($_GET['addressdetails'])) $bShowAddressDetails = (bool)$_GET['addressdetails'];
22 $aLangPrefOrder = getPreferredLanguages();
23 $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
25 $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
27 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'))
29 $iPlaceID = $oDB->getOne($sSQL = ("select place_id from placex where osm_type = '".$_GET['osm_type']."' and osm_id = ".(int)$_GET['osm_id']." order by type = 'postcode' asc"));
30 if (CONST_Debug) var_dump($sSQL);
31 if (!$iPlaceID) $sError = 'OSM ID Not Found';
35 // Location to look up
36 $fLat = (float)$_GET['lat'];
37 $fLon = (float)$_GET['lon'];
38 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
40 // Zoom to rank, this could probably be calculated but a lookup gives fine control
42 0 => 2, // Continent / Sea
54 12 => 18, // Town / Village
58 16 => 26, // Street, TODO: major street?
60 18 => 30, // or >, Building
61 19 => 30, // or >, Building
63 $iMaxRank = (isset($_GET['zoom']) && isset($aZoomRank[$_GET['zoom']]))?$aZoomRank[$_GET['zoom']]:28;
65 // Find the nearest point
66 $fSearchDiam = 0.0004;
69 $fMaxAreaDistance = 1;
70 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
72 $fSearchDiam = $fSearchDiam * 2;
74 // If we have to expand the search area by a large amount then we need a larger feature
75 // then there is a limit to how small the feature should be
76 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
77 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
78 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
79 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
80 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
81 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
82 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
83 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
85 $sSQL = 'select place_id,parent_place_id,rank_search from placex';
86 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
87 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
88 $sSQL .= ' and (name is not null or housenumber is not null)';
89 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
90 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
91 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
92 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
93 if (CONST_Debug) var_dump($sSQL);
94 $aPlace = $oDB->getRow($sSQL);
95 if (PEAR::IsError($aPlace))
97 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
99 $iPlaceID = $aPlace['place_id'];
100 $iParentPlaceID = $aPlace['parent_place_id'];
103 // The point we found might be too small - use the address to find what it is a child of
104 if ($iPlaceID && $iMaxRank < 28)
106 if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
108 $iPlaceID = $iParentPlaceID;
110 $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";
111 $iPlaceID = $oDB->getOne($sSQL);
112 if (PEAR::IsError($iPlaceID))
114 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
118 $iPlaceID = $aPlace['place_id'];
125 $sSQL = "select placex.*,";
126 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
127 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
128 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
129 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
130 $sSQL .= " from placex where place_id = $iPlaceID ";
132 $aPlace = $oDB->getRow($sSQL);
133 //var_dump($sSQL, $aPlace); exit;
135 if ($bShowAddressDetails)
137 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
139 $aClassType = getClassTypes();
141 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
142 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
144 $sAddressType = $aClassType[$aClassType]['simplelabel'];
148 $sClassType = $aPlace['class'].':'.$aPlace['type'];
149 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
150 $sAddressType = $aClassType[$sClassType]['simplelabel'];
151 else $sAddressType = $aPlace['class'];
153 $aPlace['addresstype'] = $sAddressType;
157 if (CONST_Debug) exit;
159 include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');