1

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)

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Gabriel Asato
  • 485
  • 3
  • 12
  • I have found that this question have a relationship with this other: similarity-between-two-raster-maps. https://gis.stackexchange.com/questions/72041/similarity-between-two-raster-maps?rq=1 – Gabriel Asato Jun 07 '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 07 '19 at 13:48

1 Answers1

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

or

QgsRasterCalculator(MaxValues, ...etc ...

I hope this solution will help others

Gabriel Asato
  • 485
  • 3
  • 12