May 15, 2008

Reading GeoTIFFs with PDL

For this example I'll use some free satellite imagery from GeoEye.  Go to http://www.geoeye.com/CorpSite/resource/sample_imagery.aspx and pick the Geo 1m BW 8bit from under 'More IKONOS Samples.'  Unzip this file.  Among other things, you will find po_120502_pan_0000000.tif along with its world file. This is a black and white 1-band GeoTIFF. Using the following script, you can read in the image with PDL and start working on it. For this example we'll just print out part of the image.

use PDL;

#read in a single band image
my $singleBand = rim("po_120502_pan_0000000.tif");
#print part of the original image
print $singleBand ->slice("60:80,60:80");

To read in a 3 band TIFF image, let's use po_120504_rgb_0000000.tif from Geo 4m TC 8bit, also under the 'More IKONOS Samples' drop down.

#read in a 3 band image, and assign each band to a variable
$threeBands = rim("po_119995_rgb_0000000.tif");
($a, $b, $c) = dog($threeBands);
#print part of the first band of the image
print $a->slice("60:80,60:80");

The functions used in this example are 'slice' and 'dog.' 

Slice can be used to give you a rectangular subset of the original image. The arguments it receives are ("column_start:column_end,row_start:row_end"). 

Dog is used in this case to break up the 3 bands of the source image, into 1 band images. Or, in piddle talk: Takes a single N-dimensional piddle and splits it into a list of N-1 dimensional piddles.