[GIS] Clipping raster by shapefile in PyQGIS

clippyqgis

Currently I am working with some satellite images (grib files) in pyqgis. As I want to work only with the cells in my study area, I need to clip the raster by a shapefile. I know I can do this by Raster tools in QGIS, but since I have a lot of rasters to work with, I wanted to use python and save time.

Perhaps somebody knows the pyqgis commands to do this?

I am new to QGIS and python.

Best Answer

The raster tool of QGIS uses GDAL commands

You can also use these GDAL commands (gdalwrap or gdal_translate) in a Python script, look at how to for loop a folder to batch clip rasters by polygon using python and QGIS or Extract raster windows from satellite imagery with GDAL and in pure Python Python GDAL/OGR Cookbook: Python Clip a GeoTiff with Shapefile

But you can do the same thing without PyQGIS or GDAL with the Python module PyShp: (Shapefile): GeospatialPython: Clip a Raster using a Shapefile

And you can also use Fiona and rasterio -> Warping images with rasterio

Related Question