0

I am trying to force a maximum value for a raster. Currently the raster has values ranging from 0 to 9, I would like to force values greater than 7 to 7.

I have tried a couple ways of doing this in the raster calculator and am not having any luck. I've also tried sieving values greater than 7 to a new raster and subtracting that new raster from the existing raster, but that returned values of 7 across the whole raster.

2
  • 2
    Welcome to GIS SE. Could you please describe in more detail what your input data looks like?
    – Aaron
    Commented Jan 1, 2019 at 0:26
  • GDAL_Edit.py -setstats 0 7 3.5 1 c:\full\path\to\your_raster.tif gdal.org/gdal_edit.html should do it though you might want to calculate the real stats first and use the real mean and standard deviation (last 2 parameters), an arbitrary mean and standard deviation may not look nice when displayed. Try that and if it works post your experience as an answer including the steps you took and a screen shot of the before/after. Commented Jan 1, 2019 at 1:08

2 Answers 2

1

I used the following (slightly convoluted) expression in the raster calculator:

(("myraster@1" > 7) * 7) + (("myraster@1" <= 7) * "myraster@1")

Explanation:

(("myraster@1" > 7) * 7) converts everything >7 to 7 and anything <=7 to 0.

(("myraster@1" <= 7) * "myraster@1") converts everything >7 to 0 and anything <=7 keeps the same value.

The two in memory rasters are then added together.

enter image description here

0

A simpler approach is to use the "Reclassify by Table" algorithm from the Processing Toolbox.

Click the "..." button next to the "Reclassification table" option, and change the first row to read:

enter image description here

This means "if the raster value is between 7 and +infinity, reclassify it to 7". Make sure the "Use no data when no range matches value" is NOT checked, or values < 7 will be converted to nodata. If you leave that option unchecked it means any input values which don't match the defined rules (i.e., values < 7) will be returned unchanged.

The end result is that pixels with value >= 7 will be set to 7, and other pixels left unchanged.

Your Answer

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

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