Given a series of rasters, and the raster image of the minimum values of the series, I am looking for a python script capable of selecting, for each raster, the pixels whose values are greater than the minimum, i.e. "A/(A>minimum)", being A a multiple raster.
I made the following script, it seems like is calculating something, but at the end I get the message: 'NoneType' object is not iterable. Any idea for solution?
##NAME=name ##Raster=group ##A=multiple raster ##minimum=raster ##X=output raster import qgis, os, processing from PyQt4.QtCore import QFileInfo from qgis.core import QgsMapLayerRegistry, QgsRasterLayer from qgis.analysis import QgsRasterCalculatorEntry, QgsRasterCalculator # Split rasters layers = A.split(';') output = X + "/" suffix = "_suffix.tif" # Get layer object layer1 = processing.getObject(minimum) def x(A,minimum,output): for ras in layers: fileInfo = QFileInfo(ras) baseName = fileInfo.baseName() lyr = QgsRasterLayer(ras, baseName) # Get layer object lyr = processing.getObjectFromUri(ras) entries =  ras = QgsRasterCalculatorEntry() ras.ref = 'lyr@1' ras.raster = lyr ras.bandNumber = 1 entries.append(ras) #define raster 1 ("minimum") raster1=QgsRasterCalculatorEntry() raster1.ref='minimum@1' raster1.raster=minimum raster1.bandNumber=1 entries.append(raster1) calc = QgsRasterCalculator( 'lyr@1/(lyr@1>minimum@1)', output + lyr.name() + suffix, 'GTiff', lyr.extent(), lyr.width(), lyr.height(), entries ) calc.processCalculation() for results in x(layers, layer1,output + "*" + suffix): fileInfo = QFileInfo(results) path = fileInfo.filePath() baseName = fileInfo.baseName() layer = QgsRasterLayer(path, baseName) QgsMapLayerRegistry.instance().addMapLayer(layer)
In the case of solution proposed by Ali, I don't understand if the solution is suitable only for a fixed number of input rasters (7). Actually I need a flexible tool which work independently of the number of input, being them 3 or 10 for instance. Anyway I was not able to make a useful script due to my basic knowledge about python.
On the other hand I also tried the solution as proposed by xunilk but I get the following messages:
xmin = layers.extent().xMinimum() IndexError: list index out of range
In both cases I would need to use the script within the "processing toolbox" as a standalone tool as in the image:
Actually in the case of using just two layers I have a solution which is the following:
##NAME=name ##A=raster ##Minimum=raster ##X=output raster from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry # Get layer object layer1 = processing.getObject(A) # Get layer object layer2 = processing.getObject(Minimum) def x(A,Minimum,output): entries= #define raster 1 ("A") raster1=QgsRasterCalculatorEntry() raster1.ref='A@1' raster1.raster=A raster1.bandNumber=1 entries.append(raster1) #define raster 2 ("Minimum") raster2=QgsRasterCalculatorEntry() raster2.ref='Minimum@1' raster2.raster=Minimum raster2.bandNumber=1 entries.append(raster2) #X Processing calc=QgsRasterCalculator('A@1/(A@1>Minimum@1)',output,'GTiff',Minimum.extent(),Minimum.width(),Minimum.height(),entries) calc.processCalculation() x(layer1,layer2,X)
The problem here is that I need the factor A as a multiple raster instead of a single raster in order to iterate the same operation, but I don't know how to solve it.
Is there the possibility of using the script for making a tool from the "processing toolbox" by entering the variables directly from the QGIS workspace by using the following way:
##NAME=name ##Raster=group ##A=multiple raster ##minimum=raster ##X=output raster
Instead of referring the script to folder path?