Producing tiles from a GEOTIFF Raster with PHP and gdal
How to slice a big GEOTIFF raster into smaller tiles using gdal and PHP
This short script convert a single GEOTIFF raster file in smaller tiles, suitable for tileindex in MapServer.
Prerequisites
The script calls external programs such as gdalinfo and gdal_translate, hence you will need a working installation of gdal library and all the utilities that come with the library.
Overviews
If you also want to produce overviews at a given scale, comment out the exit line and add you desired scales to the “scales” array.
if($argc != 3){
die("Usage: {$argv[0]} filename desired_square_dimension_in_map_units\n");
}
$file_name = $argv[1];
$square = $argv[2];
$info = `gdalinfo $file_name`;
foreach(split("\n", $info) as $i){
if(strpos($i, 'Lower Left') !== false){
preg_match_all('/\d+\.\d+/', $i, $res);
$ll_x = $res[0][0];
$ll_y = $res[0][1];
}
if(strpos($i, 'Upper Right') !== false){
preg_match_all('/\d+\.\d+/', $i, $res);
$ur_x = $res[0][0];
$ur_y = $res[0][1];
}
}
// Extent
$x_ext = $ur_x - $ll_x;
$y_ext = $ur_y - $ll_y;
// Fit square
$iter_x = floor($x_ext / $square);
$iter_y = floor($y_ext / $square);
$square_x = floor($x_ext / $iter_x);
$square_y = floor($y_ext / $iter_y);
// Iter
$iter_x = floor($x_ext / $square_x);
$iter_y = floor($y_ext / $square_y);
$tiles_number = $iter_x * $iter_y;
print "Coords ($ll_x, $ll_y) - ($ur_x, $ur_y)\n";
print "Ext ($x_ext) - ($y_ext)\n";
print "Producing $iter_x x $iter_y = $tiles_number tiles for $square_x x $square_y\n";
for($ix = 0 ; $ix < $iter_x; $ix++){
$tile_llx = $ix * $square_x + $ll_x;
$tile_urx = $tile_llx + $square_x;
for($iy = 0 ; $iy < $iter_y; $iy++){
$tile_lly = $iy * $square_y + $ll_y;
$tile_ury = $tile_lly + $square_y;
print "Tiling \t($ix, $iy)\t($tile_llx, $tile_lly) - ($tile_urx, $tile_ury) \n";
// Tile filename
//$tilename = "$tile_llx-$tile_lly-$tile_urx-$tile_ury.tif"
// Proj wants UL LR
$tilename = "$tile_llx-$tile_ury-$tile_urx-$tile_lly.tif";
`gdal_translate -of GTiff -projwin $tile_llx $tile_ury $tile_urx $tile_lly $file_name $tilename`;
}
}
// Remove exit and define scales if you want to produce overviews at defined scales
exit();
// Build overviews
$scales = array(1000000, 500000);
// Conversion
$dpi = 72;
// Screen resolution in meters: pixel/meter of screen
$dpm = 0.0254 / $dpi;
$scale_pix = array();
foreach($scales as $s){
$scale_pix[] = $s * $dpm;
}
//var_dump($scale_pix);
$i = 0;
foreach($scale_pix as $sp){
$ovfile = 'OVV-' . $scales[$i++] . '-' . floor($sp) .'.tif';
`gdalwarp -of GTiff -rc -tr $sp $sp $file_name $ovfile`;
}



