Water cycle, land use, and ecosystem studies requires the evaluation of satellite imagery from a long observation period. Images need be clipped to the area of interest, filtered, scaled, converted to desired formats and stored on defined folder structure. Current GIS desktop applications are fully capable of this spatial management and analysis when the amount of raster images is limited; however when we deal with high amount of images the spatial processing on a graphical user interface (GUI) can be slow and most commonly impractical. The use of programming / processing languages like Python and advanced spatial libraries as GDAL (gdal.org) helps on the spatial data transformation on a more abstract and effective way. This tutorial shows the complete procedure to clip the complete set of bands from a Landsat 8 image and store them with a suffix on every band file on another folder.
The tutorial is done on a interactive Python programming platform called Jupyter Notebook. The input files: raster bands and area of interest (AOI) shapefile need to be on the same system of reference (SRC), otherwise the GDAL library cannot locate the spatial data on the right position. The tutorial shows the procedure for the whole set of band form a Landsat 8 image, an example for a single band is provided on the scripts of the input data. Finally the tutorial shows the complete and clipped raster on a GIS desktop software as QGIS.
For beginners GIS users with few programming experience, it is advice to complete the tutorial with the sample data provided on the input data part of this tutorial. Once the user has more experience with the software, the user can modify the code for custom raster image processing.
For the installation of GDAL and other spatial libraries please check this tutorial: https://www.youtube.com/watch?v=4ybddFC80fU
This is the python code for multiple Landsat 8 raster band clipping:
Import requires libraries
This tutorial only requires the GDAL library and core Python libraries as the Operating System (OS) library.
from osgeo import gdal import os
Setting up the input and output files
We set the routes for the input and outfiles, then we create a list comprehension that reads all files on a folder and filter only the TIF files.
#Input and output paths #inputPath = '../Images/' inputPath = '../Images_c1/' outputPath = '../Output/' #Input Raster and Vector Paths bandList = [band for band in os.listdir(inputPath) if band[-4:]=='.TIF'] bandList
['LC08_L1TP_029047_20190203_20190206_01_T1_B10_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B11_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B1_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B2_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B3_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B4_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B5_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B6_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B7_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B8_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_B9_c1.TIF', 'LC08_L1TP_029047_20190203_20190206_01_T1_BQA_c1.TIF']
#Shapefile of Area of Influence shp_clip = '../Shp/AOI_1.shp'
We clip all the selected raster files with the Warp option from GDAL
for band in bandList: print(outputPath + band[:-4]+'_c2'+band[-4:]) options = gdal.WarpOptions(cutlineDSName=shp_clip,cropToCutline=True) outBand = gdal.Warp(srcDSOrSrcDSTab=inputPath + band, destNameOrDestDS=outputPath + band[:-4]+'_c2'+band[-4:], options=options) outBand= None
../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B10_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B11_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B1_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B2_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B3_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B4_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B5_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B6_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B7_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B8_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_B9_c1_c2.TIF ../Output/LC08_L1TP_029047_20190203_20190206_01_T1_BQA_c1_c2.TIF
You can download the input data and scripts for the tutorial on this link.