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 .= ' and indexed_status = 0 ';
 
  92                         $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
 
  93                         $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
 
  94                         if (CONST_Debug) var_dump($sSQL);
 
  95                         $aPlace = $oDB->getRow($sSQL);
 
  96                         if (PEAR::IsError($aPlace))
 
  98                                 failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
 
 100                         $iPlaceID = $aPlace['place_id'];
 
 101                         $iParentPlaceID = $aPlace['parent_place_id'];
 
 104                 // The point we found might be too small - use the address to find what it is a child of
 
 105                 if ($iPlaceID && $iMaxRank < 28)
 
 107                         if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
 
 109                                 $iPlaceID = $iParentPlaceID;
 
 111                         $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";
 
 112                         $iPlaceID = $oDB->getOne($sSQL);
 
 113                         if (PEAR::IsError($iPlaceID))
 
 115                                 failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
 
 119                                 $iPlaceID = $aPlace['place_id'];
 
 126                 $sSQL = "select placex.*,";
 
 127                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
 
 128                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
 
 129                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref,";
 
 130                 $sSQL .= " st_y(centroid) as lat, st_x(centroid) as lon";
 
 131                 $sSQL .= " from placex where place_id = $iPlaceID ";
 
 133                 $aPlace = $oDB->getRow($sSQL);
 
 134                 //var_dump($sSQL, $aPlace); exit;
 
 136                 if ($bShowAddressDetails)
 
 138                         $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['calculated_country_code']);
 
 140                 $aClassType = getClassTypes();
 
 142                 $sClassType = $aPlace['class'].':'.$aPlace['type'].':'.$aPlace['admin_level'];
 
 143                 if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 145                         $sAddressType = $aClassType[$aClassType]['simplelabel'];
 
 149                         $sClassType = $aPlace['class'].':'.$aPlace['type'];
 
 150                         if (isset($aClassType[$sClassType]) && isset($aClassType[$sClassType]['simplelabel']))
 
 151                                 $sAddressType = $aClassType[$sClassType]['simplelabel'];
 
 152                         else $sAddressType = $aPlace['class'];
 
 154                 $aPlace['addresstype'] = $sAddressType;
 
 158         if (CONST_Debug) exit;
 
 160         include(CONST_BasePath.'/lib/template/address-'.$sOutputFormat.'.php');