I'm looking to apply a mask to a number of rasters in PyQGIS.
(updated but still not working)
import processing
rstr = "/Users/rasterfilepath/"
shp = "/Users/shapefile.shp"
outputDir = "/Users/output/"
for lyr in rstr:
processing.runandload("gdalogr:cliprasterbymasklayer", rstr, shp, "none", False, False, "", outputDir + lyr + ".tif")
What am I doing wrong?
Best Answer
There's a couple of things to notice:
rstr
(the path of the rasters) as the input instead of the actual rasters which you have defined aslyr
.This probably depends on the Processing plugin version but in v2.10.2, the algorithm
gdalogr:cliprasterbymasklayer
requires 7 parameters (you mentioned 6). You can check this by using the Python console:Lastly, you could use the following code to loop through rasters in a specified folder. I like to use the
glob
module to search for specific types of files alongside setting the current directory withos.chdir
to the path containing the files:Hope this helps!