3         require_once('.htlib/init.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";
 
  17         ini_set('memory_limit', '200M');
 
  20         $sOutputFormat = 'xml';
 
  21         if (isset($_GET['format']) && ($_GET['format'] == 'xml' || $_GET['format'] == 'json'))
 
  23                 $sOutputFormat = $_GET['format'];
 
  27         $aLangPrefOrder = getPrefferedLangauges();
 
  28         $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
 
  30         $hLog = logStart($oDB, 'reverse', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
 
  32         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'))
 
  34                 $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");
 
  35                 if (!$iPlaceID) $sError = 'OSM ID Not Found';
 
  39                 // Location to look up
 
  40                 $fLat = (float)$_GET['lat'];
 
  41                 $fLon = (float)$_GET['lon'];
 
  42                 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
 
  44                 // Zoom to rank, this could probably be calculated but a lookup gives fine control
 
  46                         0 => 2, // Continent / Sea
 
  58                         12 => 18, // Town / Village
 
  62                         16 => 26, // Street, TODO: major street?
 
  64                         18 => 28, // or >, Building
 
  65                         19 => 30, // or >, Building
 
  67                 $iMaxRank = isset($aZoomRank[$_GET['zoom']])?$aZoomRank[$_GET['zoom']]:28;
 
  69                 // Find the nearest point
 
  70                 $fSearchDiam = 0.0001;
 
  73                 $fMaxAreaDistance = 10;
 
  74                 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
 
  76                         $fSearchDiam = $fSearchDiam * 2;
 
  78                         // If we have to expand the search area by a large amount then we need a larger feature
 
  79                         // then there is a limit to how small the feature should be
 
  80                         if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
 
  81                         if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
 
  82                         if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
 
  83                         if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
 
  84                         if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
 
  85                         if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
 
  86                         if ($fSearchDiam > 0.01 && $iMaxRank > 22) $iMaxRank = 22;
 
  90                                 // Street level search is done using placex table
 
  91                                 $sSQL = 'select place_id from placex';
 
  92                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
 
  93                                 $sSQL .= ' and rank_search >= 26 and rank_search <= '.$iMaxRank;
 
  94                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
 
  95                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
 
  96                                 $sSQL .= ' ORDER BY rank_search desc, ST_distance('.$sPointSQL.', geometry) ASC limit 1';
 
  97                                 $iPlaceID = $oDB->getOne($sSQL);
 
  98                                 if (PEAR::IsError($iPlaceID))
 
 100                                         var_Dump($sSQL, $iPlaceID); 
 
 106                                 // Other search uses the location_point and location_area tables
 
 108                                 // If we've not yet done the area search do it now
 
 109                                 if ($aArea === false)
 
 111                                         $sSQL = 'select place_id,rank_address,ST_distance('.$sPointSQL.', centroid) as distance from location_area';
 
 112                                         $sSQL .= ' WHERE ST_Contains(area,'.$sPointSQL.') and rank_search <= '.$iMaxRank;
 
 113                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
 
 114                                         $aArea = $oDB->getRow($sSQL);
 
 115                                         if ($aArea) $fMaxAreaDistance = $aArea['distance'];
 
 118                                 // Different search depending if we found an area match
 
 121                                         // Found best match area - is there a better point match?
 
 122                                         $sSQL = 'select place_id from location_point_'.($iMaxRank+1);
 
 123                                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.') ';
 
 124                                         $sSQL .= ' and rank_search > '.($aArea['rank_address']+3);
 
 125                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
 
 126                                         $iPlaceID = $oDB->getOne($sSQL);
 
 127                                         if (PEAR::IsError($iPlaceID))
 
 129                                                 var_Dump($sSQL, $iPlaceID); 
 
 135                                         $sSQL = 'select place_id from location_point_'.($iMaxRank+1);
 
 136                                         $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.') ';
 
 137                                         $sSQL .= ' ORDER BY rank_address desc, ST_distance('.$sPointSQL.', centroid) ASC limit 1';
 
 138                                         $iPlaceID = $oDB->getOne($sSQL);
 
 139                                         if (PEAR::IsError($iPlaceID))
 
 141                                                 var_Dump($sSQL, $iPlaceID); 
 
 147                 if (!$iPlaceID && $aArea) $iPlaceID = $aArea['place_id'];
 
 152                 $sSQL = "select placex.*,";
 
 153                 $sSQL .= " get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
 
 154                 $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
 
 155                 $sSQL .= " get_name_by_language(name, ARRAY['ref']) as ref";
 
 156                 $sSQL .= " from placex where place_id = $iPlaceID ";
 
 157                 $aPlace = $oDB->getRow($sSQL);
 
 159                 $aAddress = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPlace['country_code']);
 
 161         include('.htlib/output/address-'.$sOutputFormat.'.php');