2

Is there any way to add multiple grids on the first parameter of saga:rastercalculator?

If I added one file as string at first parameters and the other files at second parameter as list, the algorithm worked, but it resampled my data and I don't need this.

I have some raster files with the same grid system generated in a for loop. I want to sum their values in a single raster.

This is my code:

s1 = 'C:/Users/.../OUTPUT.sdat' 
s2 = 'C:/Users/.../OUTPUT.sdat'
s3 = 'C:/Users/.../OUTPUT.sdat'
list = [s1,s2,s3]
rezult = processing.runalg('saga:rastercalculator', list, [], "g1+g2+g3", True, 8, None)

To simplify the code above, I used the files path as a string, but in my algorythm, the files paths are dynamically added to the list and the formula is dynamically updated.

When I run the above code, I get this error:

Wrong parameter value: ['C:/Users/Flaviu/AppData/Local/Temp/processing6750b371ece74c718b442b56c8f3ef1f/c99bbf3a6dc64ba798285e21a5040648/OUTPUT11.sdat', 'C:/Users/Flaviu/AppData/Local/Temp/processing6750b371ece74c718b442b56c8f3ef1f/c99bbf3a6dc64ba798285e21a5040648/OUTPUT22.sdat', 'C:/Users/Flaviu/AppData/Local/Temp/processing6750b371ece74c718b442b56c8f3ef1f/c99bbf3a6dc64ba798285e21a5040648/OUTPUT33.sdat']

Is there an easier way to do this operation using QgsRasterCalculator ?

1

From the help of the SAGA Raster Calculator:

ALGORITHM: Raster calculator
GRIDS <ParameterRaster>
XGRIDS <ParameterMultipleInput>
FORMULA <ParameterString>
USE_NODATA <ParameterBoolean>
TYPE <ParameterSelection>
RESULT <OutputRaster>

it is necessary to specify the main raster and the additional (optional) rasters. For doing this, you need to select the first element of the list as main raster and then join the other elements from the same list.

Try this:

s1 = 'C:/Users/.../OUTPUT.sdat' 
s2 = 'C:/Users/.../OUTPUT.sdat'
s3 = 'C:/Users/.../OUTPUT.sdat'
list = [s1,s2,s3]
rezult = processing.runalg('saga:rastercalculator', list[0], ';'.join(list[1:]), "a+b+c", True, 8, None)

For a deeper understanding,

print list[0]
print ';'.join(list[1:])

gives

C:/Users/.../OUTPUT.sdat
C:/Users/.../OUTPUT.sdat;C:/Users/.../OUTPUT.sdat
  • 1
    This works in the same way as rezult = processing.runalg('saga:rastercalculator', s1, list[1:], "a+h1+h2", True, 8, None) So, in a cell where all 3 input rasters have value 0, the result has value 0.33 because of resampling and it is not ok. I will try to use QgsRasterCalculator – Flaviu Meseșan Dec 29 '16 at 14:39
  • It's not the same. If you write list[1:], you will print a list starting from the second value, while you need a join of your elements. In the first case you have a list, in the second one the elements from the list. However, have a try with QgsRasterCalcuator and please accept the answer if it helped you. =) – mgri Dec 29 '16 at 14:49
  • 1
    The problem with SAGA Raster Calculator syntax is solved and you are right. But i'm afraid this algorithm is not what I need because 0+0+0 =0.33. I tried also processing.runalg('saga:gridssum',list,None) and then 1+1+1 = 7. I don't understand why it sees 7 inputs when the list has 3 items. Anyway, thanks a lot for the effort done to help me! – Flaviu Meseșan Dec 29 '16 at 18:36
  • Maybe there was a problem with my input files. I solved the problem with QgsRasterCalculator. – Flaviu Meseșan Dec 30 '16 at 11:28

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.