Utilities
Martin Weinelt
mweinelt at PLANIGLOBE.COM
Thu Nov 25 01:17:04 PST 2004
On Wednesday 24 November 2004 19:40, Bob Basques wrote:
> All,
>
> I've used the tileindexing utility a couple of times. It there
> something available to actually tile up a large raster and or SHP file
> into the individual tiles?
>
> A combination program would be good too, where the tileindex is
> generated at the same time.
>
> Thanks
>
> bobb
For rasters you can make use of the excellent gdal utilities. Wrap -
for instance - gdalwarp in a shellscript. Or in a perl script similar
to the the geotiff tiler below (watch out for unintentional line breaks).
Cheers, Martin
############################
#!/usr/bin/perl -w
# Martin Weinelt 20030627 <mweinelt at planiglobe.com>
use strict;
my ($src_gtiff,$dimens,$p_x, $p_y, $width, $length, $t_i, $t_dir, $wd,$x_win,$y_win, $tmpname, $basename);
my @tiffinfo;
&usage unless (($dimens = $ARGV[0]) && ($src_gtiff = $ARGV[1]));
&usage unless ((($p_x, $p_y) = split (/x/,$dimens)) && ($src_gtiff));
($basename = $src_gtiff) =~ s/\..+//; # or use File::Basename
print STDERR "\n ---> Calling gdalinfo ...\n";
@tiffinfo = `gdalinfo $src_gtiff`;
foreach (@tiffinfo) {
if (/^Size is (\d+), (\d+)/) {
($width, $length) = ($1,$2);
last;
}
}
($width && $length) || die " Cannot figure $ARGV[0] dimensions with 'gdalinfo': $! \n";
# create dir for the tiles
$t_dir = 'tls';
if (!-e $t_dir) {mkdir ($t_dir) || die " Application 'unknown' has unexpectedly quit .... : error 30 $!\n";}
unlink <$t_dir/*>;
# figure where we are for absolute paths in index shp
chomp($wd = `pwd`);
$t_i = 0;
$x_win=$p_x; $y_win=$p_y;
for (my $i=0; $i< $width;$i+= $p_x) {
# x offset for tiles
(($i + $x_win) > $width)?($x_win= $width -$i):($x_win=$p_x);
for (my $k=0; $k < $length; $k+=$p_y) {
# y offset for tiles
(($k + $y_win) > $length)?($y_win= $length -$k):($y_win=$p_y);
$tmpname = $wd.'/tls/'.$t_i. '.tif';
# tiling with 'gdalwarp -te xmin ymin xmax ymax <input.tif> <croped.tif>'
print STDERR " ---> generating tile: $tmpname bounds: $i $k $p_x $p_y \n";
system " nice -n 19 gdal_translate -srcwin $i $k $x_win $y_win $src_gtiff $tmpname \n";
$t_i++;
}
}
# clean up previous runs of gdaltindex
unlink (<'index.'*>) if (-e 'index.shp');
print STDERR "\n ---> Calling gdaltindex ...\n ";
system "gdaltindex index ". $wd . "/tls/*.tif";
# generating index
print STDERR "\n ---> Calling shptree ...\n ";
system "shptree index";
print STDERR "\n ---> Done \n\n";
###########################################################
sub usage {
print <<"EOF";
gdaltiler.pl <window x pixels>x<window y pixels> geotiff_filename
Example: 'gdaltiler.pl 2000x3000 newgeo.tiff'
EOF
exit;
}
More information about the MapServer-users
mailing list