1

I have a large raster that I need to clip into many smaller tiles, however as there are nearly 200 tiles it will take too long to clip each tile individually and I need to automate the process.

The following python script will clip multiple rasters with a single extent box however, I need to clip a single raster with multiple masks.

Is it possible to simply adjust the parameters of this script to accomplish what I need?

# input variables

# where to save clipped rasters
outputDir = "/home/user/Desktop/clip"

# upper left point
UX, UY =  xxxxxx , xxxxxx

# lower right point
LX,  LY = xxxxxx , xxxxxx

# actual script
import os
lddLrs = qgis.utils.iface.legendInterface().layers()
for lyr in lddLrs:
 if (lyr.type()==QgsMapLayer.RasterLayer):
  os.system("gdal_translate -projwin " + str(UX) + " " + str(UY) + " " + str(LX) + " " + str(LY) + " -of GTiff " + str(lyr.source()) + " " + outputDir + "/" + str(lyr.name()) + ".tif")
  qgis.utils.iface.addRasterLayer(str(outputDir + "/" + lyr.name() + ".tif"), str(lyr.name() + "_clip.tif"))

1 Answer 1

1

You can do the same without pyqgis directly from processing tools. Create a polygon grid specifying the x,y sizes to be equal to the extent of each smaller tile using Vector Grid tool from Vector -> Research tools -> Vector Grid tool

enter image description here

Then Clip raster by Mask Layer from Processing toolbox -> OGR/GDAL -> [GDAL] Extraction -> Clip raster by Mask Layer

  • Select the raster file
  • Select the vector grid file created in the previous step
  • Click on iterator to iterate over each small polygon in the grid vector

enter image description here

Make sure that both raster and vector grid have the same projection (CRS)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.