Sometimes you need to use a subset of an image which covers an area of interest for a project. In fact, areas of an image outside your area of interest can distract your audience from the idea you are trying to convey. Clipping a raster to a vector boundary allows you to only use the portions of the raster you need. It can also save processing time by eliminating areas outside your area of interest.
We will use the SatImage raster again available at the following URL if you haven't downloaded it from previous recipes:
https://geospatialpython.googlecode.com/files/SatImage.zip
Place this raster in your /qgis_data/rasters
directory.
Clipping is a common operation and GDAL is well suited for it.
import processing
processing.runandload("gdalogr:cliprasterbymasklayer","/qgis_data/rasters/SatImage.tif","/qgis_data/hancock/hancock.shp","none",False,False,"","/qgis_data/rasters/clipped.tif")
output
raster looks like the following screenshot: