[GIS] Multiply two 0, 1 rasters together

qgisrasterraster-calculator

I am trying to use the Raster Calculator to multiply 2 rasters together. They were created by using the raster calculator to keep values exceeding a threshold in two other rasters. I have tried numerous combinations but always end up with zeros everywhere.

I have 2 rasters A1 and B1 of the same area. I want to locate areas where A1 > 5 and 30 <= B1 <= 45.

I could not figure out how to use AND or OR commands in the Raster Calculator (the RasterCalc plugin suggested elsewhere doesn't appear to still be available).

I created two new rasters A2 and B2, using the commands

("A1" > 5)    {saved as A2}

("B1" <=45 )* "B1"   {saved as B2a}
(B2a >= 35)          {saved as B2}

I now have 2 rasters that are 0,1 values (or rather 0.999000). I want to multiply these together. I have tried wrapping them in float(), or using float with the above commands to see if that made a difference (it didn't).

If I look at the rasters/use the inspection tool I can see that there are regions where both A2 and B2 are 1 – yet any multiplication gives only 0 as the output, any addition gives only 0 or 1.

I have not been able to find any reason as to why this might be happening.

If I simply multiply the two original rasters A1 and B1 this works as expected, so it does not appear to be multiplication that is the problem.

Best Answer

Make sure you use the "A1@1" notation. This tells QGIS to use band 1 of raster A1.

It looks as if Raster Calculator allows you to just use A1, but the end results are all 1 (at least for me, QGIS 2.18.10), or all 0 if I use "A1".

You should be able to do this in one step without needing multiple intermediate rasters.

A1 > 5 and 30 <= B1 <= 45

should be doable as

( ("A1@1" > 5) + ("B1@1" >= 30) + ("B1@1"<=45) ) = 3

or as

( ("A1@1" > 5) * ("B1@1" >= 30) * ("B1@1"<=45) ) 

this is how I convert boolean logic into raster calc terms:-

a and b = a*b
a or b = (a+b)>0

Also, the fact you're seeing 0.99 suggests you need to use Contrast Enhancement, Stretch to min/max, and Load min/max values. That should fix the legend to show 0/1 and show the raster in black and white.

You also mentioned the rastercalc plugin. This has vanished from the Plugin Manager, it seems to still be available. The source is no longer available, it would need to be installed manually, and it's tagged as being for QGIS 1.x , so I suspect it's been withdrawn. At the time, QGIS raster calculator was a lot less powerful. I think most of those features and functions are available via r.mapcalc , SAGA's Grid Calculator and indeed QGIS Raster Calculator itself.

Related Question