# Doing pairwise or pixel wise raster comparison using PyQGIS & Processing Scripts?

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?

or

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/… – Gabriel Asato Jun 7 '19 at 13:39
• WhiteBox GIS, can perform this kind of calculus (Mathematical analysis) but I need to perform it in same platform . – Gabriel Asato Jun 7 '19 at 13:48

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

or

QgsRasterCalculator(MaxValues, ...etc ...

I hope this solution will help others