User Tools

Site Tools



GDAL Commands

Data Directory: /home/user/ost4sem/exercise/geodata_SDM/
Script: /home/user/ost4sem/exercise/geodata_SDM/

cd /home/user/ost4sem/exercise/
tar -zxvf geodata_SDM.tar.gz
gedit /home/user/ost4sem/exercise/geodata_SDM/ & 

Change the directory, and explore the files.

cd /home/user/ost4sem/exercise/geodata_SDM
ls */*.tif  # list all the files with the extension .tif
gdalinfo vegetation/ETmean08-11.tif
# Visualize the image
qgis vegetation/ETmean08-11.tif
openev vegetation/ETmean08-11.tif

Reply to the following questions:
What is the pixel size?
How do you get min and max pixel values?

Understand data type

                      Ranges of GDAL data types        Image
GDAL data type	       minimum  	maximum        Size 
Byte  			     0  	    255        39M
UInt16			     0  	 65,535        78M
Int16, CInt16 	       -32,768  	 32,767        78M
UInt32			     0    4,294,967,295        155M  
Int32, CInt32 	-2,147,483,648    2,147,483,647        155M
Float32, CFloat32      -3.4E38           3.4E38        155M
Float64, CFloat64    -1.79E308         1.79E308        309M

Understand NoData Value

gdalinfo -mm vegetation/ETmean08-11.tif  -a_nodata -9999 vegetation/ETmean08-11.tif
gdalinfo -mm vegetation/ETmean08-11.tif  -a_nodata -339999995214436424907732413799364296704.000    vegetation/ETmean08-11.tif

Calculate minimum and maximum values for all the images

for file in vegetation/*.tif ; do 
gdalinfo  -mm  $file  # write a bash command to get only min/max values
for file in vegetation/*.tif ; do 
pkinfo  -mm -i $file # write an awk line to to get only min/max values


Use one of the gdal commands and try to clip all the images using a square box:
xmin : 32
xmax : 34
ymin : 1
ymax : 4

As first operation you have to create an empty script and save as .sh. You can call gedit by the GUI or run directly in the terminal. You can save the script where you want, but remember the location.

   gedit  & # this will create a script in the current directory
  • Create a working directory in your home
  • Identify the gdal command going to the gdal Internet site.
  • Identify the option of the command to make the clip using the given coordinates .
  • Do a test with one image and open it by openev.
  • Create a loop for all the images.
    Use all the previous examples stored in this wiki.
    All the commands have already been explained - you just need to combine them.
    You should be able to run the script by


Basic PKTOOLS Commands

Perform a composite (getting the mean) between two images.

Create a Coefficient of variation
 VEG=/home/user/ost4sem/exercise/geodata_SDM/vegetation --overwrite  -A $VEG/GPPstdev08-11.tif -B  $VEG/GPPmean08-11.tif \
 --calc="( A / ( B + 0.000000001 ) )" --outfile=$VEG/GPPcv08-11.tif

Check the pkgetmask -min and -max options and use them to create a mask with 0 and 1 value.

Create a a mask
 pkgetmask  -min  0.50  -max  1.1  -i  $VEG/GPPcv08-11.tif  -o $VEG/GPPcv08-11_01.tif
Change pixel resolution and create a composite
 # looping trough the images
 for file in $INDIR/LST_MOYDmax_month?.tif $INDIR/LST_MOYDmax_month??.tif; do 
     filename=$(basename $file .tif )
     gdalwarp -te 29 -1 35.0000000048 4.000000004 -tr 0.008333333340000 0.008333333340000 -co COMPRESS=LZW -co ZLEVEL=9 $file $INDIR/${filename}_crop.tif 
 # Calculate mean and standard deviation with several images
 pkcomposite $(for file in $INDIR/LST_MOYDmax_month*_crop.tif ; do echo -i $file ;  done ) -cr mean  -srcnodata -9999 -dstnodata -9999 -co  COMPRESS=LZW -co ZLEVEL=9 -o $INDIR/LST_MOYDmax_monthMean_crop.tif
 pkcomposite $(for file in $INDIR/LST_MOYDmax_month*_crop.tif ; do echo -i $file ;  done ) -cr stdev  -srcnodata -9999 -dstnodata -9999 -co  COMPRESS=LZW -co ZLEVEL=9 -o $INDIR/LST_MOYDmax_monthStdev_crop.tif
for file in $INDIR/*_crop.tif ; do 
filename=$(basename $file .tif )
pkfilter -co COMPRESS=LZW -ot Float32-dx 20 -dy 20 -f mean -d 20 -i $INDIR/${filename}.tif  -o $INDIR/${filename}_20p.tif 

You can find more example here or the in official pktools page

wiki/gdalbasic_kenya.txt · Last modified: 2021/01/20 20:36 (external edit)