I have a crop data raster layer in QGIS with one band with values from 10 to 230.

I have written an expression in the Raster Calculator to keep only a number of values (e.g. when = 145 OR when = to 146, etc..

This works but the resulting layer has 0 values for areas that did not satisfy the expression and 1's for areas that did. As a result, I appear to have lost the original raster values which indicate crop types.

Am I doing this wrong or maybe there's another way to filter that will keep my original values?

  • Sorry, the question is unclear. If you have 145, you want to get 145. for 146 -> 146. But what values do you want to have for 10, 50, 100 etc.?
    – Babel
    Jan 10 at 22:19
  • Sorry I was unclear...those other values I'm not interested in can be 0. So for example: 10--->0 20-->0 140-->140 145--->145 190--->0 etc. Jan 10 at 22:50

1 Answer 1


If you use QGIS 3.22 or later, you have the new conditional if in raster calculator:

IF ("RASTER@1" >= 145 AND "RASTER@1" <= 146, "RASTER@1", 0)

For older versions, use:

("RASTER@1" >= 145 and "RASTER@1" <= 146) * "N45E006@1"

I would not advice using something like "RASTER@1" = 145 or "RASTER@1" = 146: probably pixel values will not match exactly integers (145.00, 146.00), but be rather like 145.7 and using this expression, it will not find anything.

  • Thanks @Babel I'll try that. SO would that keep those values as 145 or 146 and all other values set to 0? Jan 11 at 0:18
  • It keeps all values from 145 to 146 and sets all others to 0
    – Babel
    Jan 11 at 6:21
  • Thanks @Babel this worked perfectly! Jan 11 at 23:24

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.