Data serving scripts

From World Wind Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 12:25, 29 May 2005 (edit)
Nosilleg (Talk | contribs)
(added cat:DD)
← Previous diff
Revision as of 13:56, 19 March 2008 (edit) (undo)
Mkpl (Talk | contribs)
(a few scripts added)
Next diff →
Line 1: Line 1:
-Perl script used for NLT Landsat and SRTM tile serving:+==PHP scripts==
 +===Images===
 +<pre>
 +<?
 +// This scipt is for serving out your own WW Cached tiles to World Wind, useful for own imagery and for working "off line"
 +// This script can be named what ever you want, it is controlled via a World Wind XML
 +//
 +// thanks to MaurizoZA and Nowak for the script
 + 
 +$X = $_GET['X'];
 +$Y = $_GET['Y'];
 +$L = $_GET['L'];
 +$T = $_GET['T'];
 + 
 +function addzeros($string){
 + //echo $string;
 + if(strlen($string) >= 4){
 + return $string;
 + }
 + 
 + $string = "0" . "$string";
 + 
 + if(strlen($string) < 4){
 + $string = addzeros($string);
 + }
 + return $string;
 +}
 + 
 + 
 +$ext = ".jpg";
 + 
 +// Change the following to the location of your local root cache folder
 +$url = 'D:/cache/';
 +$doneurl = $url . $T . "/" . $L . "/" . addzeros($Y) . "/" . addzeros($Y) .
 +"_" . addzeros($X) . $ext;
 + 
 +// Debug tools
 +//header("Location: $doneurl");
 +//exit;
 +// print ($doneurl);
 +//exit;
 + 
 + $tileData = file_exists($doneurl) ? file_get_contents($doneurl) :
 +false;
 + if ($tileData === false) die();
 + 
 + @header('Content-type: image/jpeg');
 + print($tileData);
 + 
 +?>
 +</pre>
 + 
 +===Elevation===
 +*simple serving script:
 +<pre>
 +<?
 +// This scipt is for serving out your own WW Cached tiles to World Wind, useful for own imagery and for working "off line"
 +// This script can be named what ever you want, it is controlled via a World Wind XML
 +//
 +// thanks to MaurizoZA and Nowak for the script
 + 
 +$X = $_GET['X'];
 +$Y = $_GET['Y'];
 +$L = $_GET['L'];
 +$T = $_GET['T'];
 + 
 +function addzeros($string){
 + //echo $string;
 + if(strlen($string) >= 4){
 + return $string;
 + }
 + 
 + $string = "0" . "$string";
 + 
 + if(strlen($string) < 4){
 + $string = addzeros($string);
 + }
 + return $string;
 +}
 + 
 + 
 +$ext = ".bil.zip";
 + 
 +// Change the following to the location of your local root cache folder
 +$url = 'D:/cache/';
 +$doneurl = $url . $T . "/" . $L . "/" . addzeros($Y) . "/" . addzeros($Y) . "_" . addzeros($X) . $ext;
 + 
 +// Debug tools
 +//header("Location: $doneurl");
 +//exit;
 +// print ($doneurl);
 +//exit;
 + 
 + $tileData = file_exists($doneurl) ? file_get_contents($doneurl) :
 +false;
 + if ($tileData === false) die();
 + 
 + @header('Content-type: application/zip');
 + print($tileData);
 + 
 +?>
 +</pre>
 + 
 +*another script which can also create new tiles from tif files:
 +<pre>
 +<?
 +//PHP script for dishing out BILs behind a web server or a geo-trawler.
 +//It will take a source TIF, use GDAL to cut up the source elevation data into WW tiles
 +//(and corresponding directory structure), and then GZip the files for delivery.
 + 
 +$szMapCacheDir="G:/CFSImagery/wwcache/DEM";
 +/* create the main cache directory if necessary */
 +if (!@is_dir($szMapCacheDir))
 + makeDirs($szMapCacheDir);
 + 
 +/* get the various request parameters
 + * also need to make sure inputs are clean, especially those used to
 + * build paths and filenames
 + */
 +$X = isset( $_REQUEST['X'] ) ? intval($_REQUEST['X']) : 0;
 +$Y = isset( $_REQUEST['Y'] ) ? intval($_REQUEST['Y']) : 0;
 +$L = isset( $_REQUEST['L'] ) ? intval($_REQUEST['L']) : 0;
 +$T = isset( $_REQUEST['T'] ) ? intval($_REQUEST['T']) : 103;
 +$szExt = ".7z";
 + 
 +$szLevelcache = $szMapCacheDir."/$L";
 +$szYcache = sprintf($szLevelcache."/%04d",$Y);
 +if (!@is_dir($szYcache))
 + makeDirs($szYcache);
 +$szCacheFile = sprintf($szYcache."/%04d_%04d".$szExt,$Y,$X);
 +$szIntFile = sprintf($szYcache."/%04d_%04d.bil",$Y,$X);;
 +/* Hit Test the cache tile */
 +if (!file_exists($szCacheFile) || $bForce){
 + $lzts = 1.0;
 + //Our Layer Zero Tile Size
 +
 + $lat1 = ($Y*$lzts*pow(0.5,$L))-90;
 + $lon1 = ($X*$lzts*pow(0.5,$L))-180;
 + //Lat2 and Lon2 as figured from tile size and level
 + $lat2 = $lat1 + $lzts*pow(0.5,$L);
 + $lon2 = $lon1 + $lzts*pow(0.5,$L);
 + if($T==103){
 + if(($lat1>-33)||($lon1<138)||($lat2<-36)||($lon2>140)){
 + header("HTTP/1.0 404 Not Found");
 + exit();
 + }
 + else{
 + $gdalwarp = "gdalwarp.exe -te $lon1 $lat1 $lon2 $lat2 -ot Int16 -ts 150 150 -of ENVI ".
 + "G:/CFSImagery/latlong/dem/hillsdem.tif ".$szIntFile;
 + exec($gdalwarp);
 + $za7 = "7za.exe a ".$szCacheFile." ".$szIntFile;
 + exec($za7);
 + }
 + }
 + if($T==104){
 + if(($lat1>28)||($lon1<9)||($lat2<27)||($lon2>11)){
 + header("HTTP/1.0 404 Not Found");
 + exit();
 + }
 + else{
 + $gdalwarp = "gdalwarp.exe -te $lon1 $lat1 $lon2 $lat2 -ot Int16 -ts 250 250 -of ENVI ".
 + "G:/CFSImagery/latlong/dem/LibyaDuneslatlonint ".$szIntFile;
 + exec($gdalwarp);
 + $za7 = "7za.exe a ".$szCacheFile." ".$szIntFile;
 + exec($za7);
 + }
 + }
 +}
 +$h = fopen($szCacheFile, "r");
 +header("Content-Type: "."application/x-7z-compressed");
 +header("Content-Length: " . filesize($szCacheFile));
 +header("Expires: " . date( "D, d M Y H:i:s GMT", time() + 31536000 ));
 +header("Cache-Control: max-age=31536000, must-revalidate" );
 +fpassthru($h);
 +fclose($h);
 +?>
 +</pre>
 +==Perl script used for NLT Landsat and SRTM tile serving==
<pre> <pre>
#!/usr/bin/perl #!/usr/bin/perl
Line 161: Line 338:
exit 0; exit 0;
</pre> </pre>
 +
 +==Note==
 +Some of these scripts were designed for World Wind 1.3 which used 7-zip compression for .bil tiles. Version 1.4 supports both 7-zip and zip.
[[Category:Dev Documentation]] [[Category:Dev Documentation]]

Revision as of 13:56, 19 March 2008

Contents

PHP scripts

Images

<?
// This scipt is for serving out your own WW Cached tiles to World Wind, useful for own imagery and for working "off line"
// This script can be named what ever you want, it is controlled via a World Wind XML
//
// thanks to MaurizoZA and Nowak for the script

$X = $_GET['X'];
$Y = $_GET['Y'];
$L = $_GET['L'];
$T = $_GET['T'];

function addzeros($string){
       //echo $string;
       if(strlen($string) >= 4){
               return $string;
       }

       $string = "0" . "$string";

       if(strlen($string) < 4){
               $string = addzeros($string);
       }
       return $string;
}


$ext = ".jpg";

// Change the following to the location of your local root cache folder
$url = 'D:/cache/';
$doneurl = $url . $T . "/" . $L . "/" . addzeros($Y) . "/" . addzeros($Y) .
"_" . addzeros($X) . $ext;

// Debug tools
//header("Location: $doneurl");
//exit;
// print ($doneurl);
//exit;

     $tileData = file_exists($doneurl) ? file_get_contents($doneurl) :
false;
     if ($tileData === false) die();

     @header('Content-type: image/jpeg');
     print($tileData);

?>

Elevation

  • simple serving script:
<?
// This scipt is for serving out your own WW Cached tiles to World Wind, useful for own imagery and for working "off line"
// This script can be named what ever you want, it is controlled via a World Wind XML
//
// thanks to MaurizoZA and Nowak for the script

$X = $_GET['X'];
$Y = $_GET['Y'];
$L = $_GET['L'];
$T = $_GET['T'];

function addzeros($string){
       //echo $string;
       if(strlen($string) >= 4){
               return $string;
       }

       $string = "0" . "$string";

       if(strlen($string) < 4){
               $string = addzeros($string);
       }
       return $string;
}


$ext = ".bil.zip";

// Change the following to the location of your local root cache folder
$url = 'D:/cache/';
$doneurl = $url . $T . "/" . $L . "/" . addzeros($Y) . "/" . addzeros($Y) . "_" . addzeros($X) . $ext;

// Debug tools
//header("Location: $doneurl");
//exit;
// print ($doneurl);
//exit;

     $tileData = file_exists($doneurl) ? file_get_contents($doneurl) :
false;
     if ($tileData === false) die();

     @header('Content-type: application/zip');
     print($tileData);

?>
  • another script which can also create new tiles from tif files:
<?
//PHP script for dishing out BILs behind a web server or a geo-trawler.
//It will take a source TIF, use GDAL to cut up the source elevation data into WW tiles
//(and corresponding directory structure), and then GZip the files for delivery.

$szMapCacheDir="G:/CFSImagery/wwcache/DEM";
/* create the main cache directory if necessary */
if (!@is_dir($szMapCacheDir))
    makeDirs($szMapCacheDir);

/* get the various request parameters 
 * also need to make sure inputs are clean, especially those used to
 * build paths and filenames
 */
$X = isset( $_REQUEST['X'] ) ? intval($_REQUEST['X']) : 0;
$Y = isset( $_REQUEST['Y'] ) ? intval($_REQUEST['Y']) : 0;
$L = isset( $_REQUEST['L'] ) ? intval($_REQUEST['L']) : 0;
$T = isset( $_REQUEST['T'] ) ? intval($_REQUEST['T']) : 103;
$szExt = ".7z";

$szLevelcache = $szMapCacheDir."/$L";
$szYcache = sprintf($szLevelcache."/%04d",$Y);
if (!@is_dir($szYcache))
    makeDirs($szYcache);
$szCacheFile = sprintf($szYcache."/%04d_%04d".$szExt,$Y,$X);
$szIntFile = sprintf($szYcache."/%04d_%04d.bil",$Y,$X);;
/* Hit Test the cache tile */
if (!file_exists($szCacheFile) || $bForce){
 $lzts = 1.0;
 //Our Layer Zero Tile Size
 
 $lat1 = ($Y*$lzts*pow(0.5,$L))-90;
 $lon1 = ($X*$lzts*pow(0.5,$L))-180;
 //Lat2 and Lon2 as figured from tile size and level
 $lat2 = $lat1 + $lzts*pow(0.5,$L);
 $lon2 = $lon1 + $lzts*pow(0.5,$L);
 if($T==103){
 if(($lat1>-33)||($lon1<138)||($lat2<-36)||($lon2>140)){
  header("HTTP/1.0 404 Not Found");
  exit();
 }
 else{
  $gdalwarp = "gdalwarp.exe -te $lon1 $lat1 $lon2 $lat2 -ot Int16 -ts 150 150 -of ENVI ".
  "G:/CFSImagery/latlong/dem/hillsdem.tif ".$szIntFile;
  exec($gdalwarp);
  $za7 = "7za.exe a ".$szCacheFile." ".$szIntFile;
  exec($za7);
 }
 }
 if($T==104){
 if(($lat1>28)||($lon1<9)||($lat2<27)||($lon2>11)){
  header("HTTP/1.0 404 Not Found");
  exit();
 }
 else{
  $gdalwarp = "gdalwarp.exe -te $lon1 $lat1 $lon2 $lat2 -ot Int16 -ts 250 250 -of ENVI ".
  "G:/CFSImagery/latlong/dem/LibyaDuneslatlonint ".$szIntFile;
  exec($gdalwarp);
  $za7 = "7za.exe a ".$szCacheFile." ".$szIntFile;
  exec($za7);
 }
 }
}
$h = fopen($szCacheFile, "r");
header("Content-Type: "."application/x-7z-compressed");
header("Content-Length: " . filesize($szCacheFile));
header("Expires: " . date( "D, d M Y H:i:s GMT", time() + 31536000 ));
header("Cache-Control: max-age=31536000, must-revalidate" );
fpassthru($h);
fclose($h);
?>

Perl script used for NLT Landsat and SRTM tile serving

#!/usr/bin/perl
use strict;
use CGI;
use Apache::Connection ();

my $r = shift;
my $c = $r->connection;
my $q = new CGI;

# Initialize
my $inDataset = "";
my $inLevel = "";
my $inX = "";
my $inY = "";
my $outInvalid = 0;
my $outDataType = 0;
my $outPathname = "";
my $outMimeType = "";
my $outExtension = "";
my $buff = "";
my $bytesRead = 0;
my $haveFile = 0;

# Get posted variables
if (length($q->param('T'))) {     # dataset
  $inDataset = $q->param('T');
  $inDataset = int($inDataset);
} else {
  $outInvalid = 1;
}

if (length($q->param('L'))) {    # level
  $inLevel = $q->param('L');
  $inLevel = int($inLevel);
} else {
  $outInvalid = 1;
}

if (length($q->param('X'))) {    # x
  $inX = $q->param('X');
  $inX = int($inX);
} else {
  $outInvalid = 1;
}

if (length($q->param('Y'))) {    # y
  $inY = $q->param('Y');
  $inY = int($inY);
} else {
  $outInvalid = 1;
}

# Verify input, create pathname
$outPathname = "/wwdata/";
$outMimeType = "";

if ($inDataset == 100) {
    $outPathname .= "100/";
    $outMimeType = "application/x-7z-compressed";
    $outExtension = ".7z";
    $outDataType = 1;
} elsif ($inDataset == 106) {
    $outPathname .= "106/";
    $outMimeType = "application/x-7z-compressed";
    $outExtension = ".7z";
    $outDataType = 1;
} elsif ($inDataset == 105) {
    $outPathname .= "105/";
    $outMimeType = "image/jpeg";
    $outExtension = ".jpg";
    $outDataType = 2;
} else {
    $outInvalid = 1;
}

if (($inLevel >= 0) && ($inLevel <= 9)) {
  $outPathname .= "${inLevel}/";
} else {
  $outInvalid = 1;
}

#0005_0006.7z
#
#pad with 3 zeroes, if less than 1000
if ($inY < 1000) {
    $inY = sprintf("%04u", $inY);
}
$outPathname .= "${inY}/${inY}_";

#pad with 3 zeroes, if less than 1000
if ($inX < 1000) {
    $inX = sprintf("%04u", $inX);
}
$outPathname .= "${inX}";
$outPathname .= "${outExtension}";


# Display (for debugging purposes)
#
#if (!$outInvalid) {
#  $r->content_type('text/plain');
#
#  print "$inDataset $inLevel $inX $inY\n";
#  print "$outPathname\n\n";
#}

if ($outInvalid) {
  $r->status(400);     # BAD_REQUEST
  exit 0;
}


# Do we have the file?
if (-e $outPathname && -r $outPathname) {
  $haveFile = 1;
} else {
  # handle error


#  if ($outDataType == 2) {
#     # Can't find the Landsat7 tile, push /wwdata/105/blank.jpg
#     $outPathname = "/wwdata/105/blank.jpg";
#     if (!(-e $outPathname) || !(-r $outPathname)) {
#       print "Can't find blank Landsat7 tile";
#       exit 0;
#     }
#  } else {
#     $r->content_type('text/plain');
#
#     print "Can't find tile\n";
#     print "$inDataset $inLevel $inX $inY $outMimeType $outExtension";


     $r->status(404);      # NOT_FOUND
     exit 0;
#  }
}
 
# Send file

# Set up headers
$r->content_type("$outMimeType");
$r->set_content_length((-s($outPathname)));

# Open and display file
open my $fh, "$outPathname";
binmode $fh;
binmode STDOUT;
  
$bytesRead = read ($fh, $buff, 8192);
while ($bytesRead) {
  print $buff;
  $r->rflush;
  last if $c->aborted;
  $bytesRead = read ($fh, $buff, 8192);
}
close $fh;
exit 0;

Note

Some of these scripts were designed for World Wind 1.3 which used 7-zip compression for .bil tiles. Version 1.4 supports both 7-zip and zip.

Personal tools