Do you know how to do a pairwise o pixel wise raster comparison using PyQGIS or Processing Scripts?

Pairwise or pixel wise comparisons are made between two raster datasets pixel by pixel. That means a pixel in an image a at a[n,n] will be compared with an image b at pixel b[n,n].

Comparisons like this are performed with functions like maximun(a,b)->value, minimun(a,b)->value, equal(a,b)->boolean, etc.

I have found and article that said by using the QgsRasterCalculator I can use functions like this:

le([rast1]@1, [rast2]@1, [rast2]@1) or gt([rast1]@1, [rast2]@1, [rast2]@1)

but none of these worked.

Are there any QgsRasterCalculator function I can use for performing pairwise analysis?


Do I have to convert my QgsRater to a Numpy matrix and to a QgsRaster again to perform this kind of analysis? (at first look seems a no so very efficient and CPU time consuming method)

  • I have found that this question have a relationship with this other: similarity-between-two-raster-maps. gis.stackexchange.com/questions/72041/… Jun 7, 2019 at 13:39
  • WhiteBox GIS, can perform this kind of calculus (Mathematical analysis) but I need to perform it in same platform . Jun 7, 2019 at 13:48

1 Answer 1


Finally I have found a solution for my problem and is this:

rCalcEntry1        = QgsRasterCalculatorEntry()
rCalcEntry1.ref    = 'r1@1'
rCalcEntry1.raster = rasterInput1

rCalcEntry2        = QgsRasterCalculatorEntry()
rCalcEntry2.ref    = 'r2@1'
rCalcEntry2.raster = rasterInput2

MinValues  = '((r1@1 < r2@1) * r1@1 ) + ((r2@1 < r1@1) * r2@1 ) + ((r2@1 = r1@1) * r2@1 ) '
MaxValues   = '((r1@1 > r2@1) * r1@1 ) + ((r2@1 > r1@1) * r2@1 ) + ((r2@1 = r1@1) * r2@1 )' 

QgsRasterCalculator(MinValues, ...etc


QgsRasterCalculator(MaxValues, ...etc ...

I hope this solution will help others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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