5

I need to use the r.series tool (that makes average raster out of multiple rasters) in a model. the idea is that each time the user will specify a folder with rasters, and the r.series tool will output a single raster that is the average of all the rasters in the folder (the number of rasters is changing for each folder).

When I use the modeler I can insert raster as parameter but only one-by-one. I know that it is possible to use "folder" as parameter but I cannot use it as an input for the r.series tool.

So.. is there a way to use a folder as an input for the r.series tool? or would you recommend a different way to accomplish that?

enter image description here

3

Great answer by @Micha! You could also create a script and use this instead of the modeller. Benefits of a script is that you can control how things should be executed and when etc. You can create a script by going to:

Processing Toolbox > Scripts > Tools > Create new script

And then use something like the following (note: change the parameters inside the rseries algorithm to fit your needs):

##Example=name
##Select_directory=Folder

import glob, os
from PyQt4.QtCore import QFileInfo
from qgis.core import QgsRasterLayer, QgsRectangle

os.chdir(Select_directory)
rlist = []
extent = QgsRectangle()
extent.setMinimal()
for raster in glob.glob("*.tif"):
    fileInfo = QFileInfo(raster)
    baseName = fileInfo.baseName()
    rlayer = QgsRasterLayer(raster, baseName)
    # Combine raster layers to list
    rlist.append(rlayer)
    # Combine raster extents
    extent.combineExtentWith(rlayer.extent())

# Get extent    
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()
# Run algorithm and set relevant parameters
processing.runalg("grass7:r.series",
                {"input":rlist,
                "-n":False,
                "method":0,
                "range":'-10000000000,10000000000', 
                "GRASS_REGION_PARAMETER":"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax), 
                "GRASS_REGION_CELLSIZE_PARAMETER":0,
                "output":Select_directory + "/Output.tif"})

Remember to save your script in the .qgis2/processing/scripts/ directory. You can then run this from the processing toolbox by calling the name or use it in your modeller.

  • 1
    In my experience, running r.series in a python script with a python list as the input fails, due to an extra newline at the end of the list. You might need to add r.list.strip() just to be sure. – Micha Dec 22 '16 at 19:55
  • @Micha - Although I haven't come across that problem, it is a good point. Thanks for sharing :) – Joseph Jan 4 '17 at 10:21
  • 1
    Thank you both your answers. As I'm more comfortable with python, I tried Joseph's code and it worked. – user88484 Jan 14 '17 at 8:39
2

To use the r.series module (and any other module in GRASS) the input rasters have to be available as GRASS maps. I'm not sure which modeler you are using from the image above, but you could just grab all the input rasters as "external" rasters in GRASS, then run r.series on those. The commands would be something like:

INPUT_DIR="your/input/directory"
i=1

# define all tiffs in input dir as externals
for t in ${INPUT_DIR}/*.tif; do
    r="rast_${i}"
    r.external input=${t} output=${r}
    (( i += 1 ))
done

# Now run r.series
rlist = $(g.list rast pat=rast_* sep=comma)
r.series input=${rlist} output=rast_avg method=average
g.remove -f rast name=${rlist}

The above can be done in python, or whatever you're comfortable with.

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.