User Tools

Site Tools


wiki:gdalbasic_kenya

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:gdalbasic_kenya [2017/12/05 22:53] (current)
Line 1: Line 1:
 +====== Basic GDAL/​OGR/​PKTOOLS Commands ======
 +===== GDAL Commands =====
 +
 +Data Directory: /​home/​user/​ost4sem/​exercise/​geodata\_SDM/​ \\
 +Script: /​home/​user/​ost4sem/​exercise/​geodata_SDM/​gdal\_pktools.sh \\
 +
 +
 +  cd /​home/​user/​ost4sem/​exercise/​
 +  wget http://​www.spatial-ecology.net/​ost4sem/​exercise/​geodata_SDM.tar.gz
 +  tar -zxvf geodata_SDM.tar.gz
 +  gedit /​home/​user/​ost4sem/​exercise/​geodata_SDM/​gdal_pktools.sh & 
 +
 +Change the directory, and explore the files.
 +
 +<code bash>
 +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
 +</​code>​
 +
 +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 ===
 +
 +<code bash>
 +gdalinfo -mm vegetation/​ETmean08-11.tif
 +gdal_edit.py ​ -a_nodata -9999 vegetation/​ETmean08-11.tif
 +gdalinfo -mm vegetation/​ETmean08-11.tif
 +gdal_edit.py ​ -a_nodata -339999995214436424907732413799364296704.000 ​   vegetation/​ETmean08-11.tif
 +</​code>​
 +
 +
 +
 +=== Calculate minimum and maximum values for all the images ===
 +<code bash>
 +for file in vegetation/​*.tif ; do 
 +gdalinfo ​ -mm  $file  # write a bash command to get only min/max values
 +done
 +</​code>​
 +
 +
 +<code bash>
 +for file in vegetation/​*.tif ; do 
 +pkinfo ​ -mm -i $file # write an awk line to to get only min/max values
 +done
 +</​code>​
 +
 +\\
 +=== Exercise: ===
 +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. ​
 +\\
 +<code bash>
 +   gedit cropscript.sh ​ & # this will create a script in the current directory
 +</​code>​
 +
 +  - 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 
 +
 +   ​bash ​ cropscript.sh.
 +
 +\\
 +
 +===== 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
 +   ​gdal_calc.py --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 ==
 +
 +   ​INDIR=/​home/​user/​ost4sem/​exercise/​geodata_SDM/​LST
 +   # 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 ​
 +   ​done ​
 +
 +   # 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 ​
 +  done 
 +   
 +You can find more example [[ http://​spatial-ecology.net/​dokuwiki/​doku.php?​id=wiki:​pk_tools ​ | here ]] or the in official [[ http://​pktools.nongnu.org/​html/​md_doc_apps.html#​available_tools | pktools page  ]]
  
wiki/gdalbasic_kenya.txt ยท Last modified: 2017/12/05 22:53 (external edit)