3

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?

  • 1
    What is the error you get, if any? What is not working as expected and what would be the expected result? – Kersten Oct 13 '15 at 8:53
  • Hi Kersten, thanks for the fast reply! Error: TypeError: cannot concatenate 'str' and 'int' objects (Line 3) – Dazzle Oct 13 '15 at 8:57
  • 1
    for loops don't require a line like lyr +=1. lyr is a string and the plus sign with a string is concatenation. So, it's trying to concatenate the layer name and 1. take that line out. – Fezter Oct 14 '15 at 0:42
7

There's a couple of things to notice:

  1. In your algorithm, you are using rstr (the path of the rasters) as the input instead of the actual rasters which you have defined as lyr.
  2. 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:

    import processing
    processing.alghelp("gdalogr:cliprasterbymasklayer")
    >>>ALGORITHM: Clip raster by mask layer
           INPUT <ParameterRaster>
           MASK <ParameterVector>
           NO_DATA <ParameterString>
           ALPHA_BAND <ParameterBoolean>
           KEEP_RESOLUTION <ParameterBoolean>
           EXTRA <ParameterString>
           OUTPUT <OutputRaster>
    
  3. 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 with os.chdir to the path containing the files:

    import glob, os, processing
    rstr = "/Users/rasterfilepath//"
    shp = "/Users//shapefile.shp"
    outputDir = "/Users/output//"
    os.chdir(rstr)    # Sets the current directory to your rasterfilepath
    for lyr in glob.glob("*.tif"):
        processing.runandload("gdalogr:cliprasterbymasklayer", lyr, shp, 'none', False, False, '', outputDir + lyr)
    

Hope this helps!

  • 1
    You can eliminate the use of os.chdir() by calling glob.glob(os.path.join(rstr, "*.tif")) and get the full paths. I find this preferrable in scripts, so users don't encounter sudden directory changes. – Kersten Oct 13 '15 at 10:45
  • @Kersten - Thanks buddy, that also sounds like a good alternative. – Joseph Oct 13 '15 at 11:03
  • Hi guys, thanks for the help but I am still having trouble with this. I am not a Windows user so cannot use the glob solution. I updated the text above, can anyone offer any advice please? – Dazzle Oct 23 '15 at 12:06
  • 1
    @Dazzle - glob is a Python module, I don't think it matters which OS you use. Did you install QGIS via the OSGeo4W Installer? – Joseph Oct 23 '15 at 12:14
  • Hi Joseph. No just installed QGIS v2.10.1 as normal on macbook. Still no luck with glob or other methods – Dazzle Oct 23 '15 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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