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?

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:

This: output + "*" + suffix is, probably, part of your issue. Caracter "*" is in the name of output raster. By the way, its easier to solve this by using the 'identify' method of QgsRasterDataProvider.
– xunilkJun 12 '16 at 16:04

2 Answers
2

It looks like your issue here is with your function x. Since nothing is being returned from the function, you cannot iterate over it.

I would suggest making use of the yield keyword. This will return a generator which you can iterate over. Essentially it returns each result in the function in turn. For example:

def a_function(x):
for i in range(x):
yield i

You can then use your for results... loop to loop through each yielded result. Based on the function above:

for result in a_function(7):
# Optionally do something with the result
print result

This will simply print each value in range(7) in turn, i.e.,

0
1
2
3
4
5
6

If I understand your script correctly, I think you should yield your new raster path in your x function. This will then be passed into your final loop as results and therefore your QFileInfo call will point to your new raster :

UPDATE

My answer above does indeed work for any number of rasters, however, having spent a bit more time with this, I have noticed some more fundamental issues with your initial script e.g. unused variables, redundant objects. I have therefore written my own interpretation of your script.

The following is my Python script which I successfully used to remove the black borders from Landsat imagery and have them loaded into the QGIS map canvas. As you mentioned limited python knowledge I have tried to provide useful comments throughout the script.

Essentially the process_raster function will, given an input raster, minimum raster, and output path, carry out the processing you require and return the path to the new file.

This script will loop through a list of rasters (this list can be any length!) and, for each one, create the new raster and store its path. It then loads the new raster into the map canvas before continuing to the next raster in the list.

Note: This requires only a single tif specified as the output; the path of each individual output raster will be derived from this and therefore the specified output wont actually exist. Therefore, be sure to uncheck Open output file after running algorithm. The process of adding the outputs to the map canvas is taken care of in the script.

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?
– Nico_77Jun 18 '16 at 20:00

@Nico_77 I've added a further update which I've successfully used as a tool in QGIS.
– AliJun 18 '16 at 21:33

It was exactly what I was looking for! it works to me as well, perfectly. Thanks a lot!
– Nico_77Jun 18 '16 at 22:28

Its easier to solve this issue by using the 'identify' method of QgsRasterDataProvider and a little bit of GDAL Python. I used a very simple rasters (20 x 20) to test my approach. The first one was an aleatory raster with values between 1 and 100. The another one was the 'minimum' raster; whose values were all 50. It looks like this:

After running the above code at the Python Console of QGIS, I got a raster (values_gtmin.tif) where pixels greater than minimum image were adequately selected and pixels lesser (or equal) than minimum image were replaced by -1. At the next image, it can be observed the corroboration of obtained results with the help of 'Value Tool' plugin. Black areas are pixels with -1 values.