3

Given a topographic GIS raster of one country crop.tif:

Given a know pixel value such as elevation (z) is +73 :

$ gdallocationinfo crop.tif 1 1 -valonly
> 73

Given an elevation threshold n = 50 (meters)

How to set the value of all/this pixels where z >= n to 50 ?

The solution should be with gdal or in console.


Related to : Gdal: How to get a pixel's value from raster image?, gdal_polygonize.py.


EDIT: I eventually used the formula --calc="50*(A>50)" --NoDataValue=0 standing for "new calc where value set as 50 when value in input A > 50, else value set as 0"

gdal_calc.py -A input.tif --outfile=result.tif --calc="50*(A>50)" --NoDataValue=0

1 Answer 1

14

The best all round tool here is a raster calculator.

gdal_calc is a GDAL raster calculator implemented in Python here, with some examples here.

If you e.g. wants to keep values above +50:

gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>50)" --NoDataValue=0

You can specify several files -A to -Z, where each of them get a corresponding variable in the expression.

In this case, the boolean expression (A>50) returns either 1 or 0, and is then multiplied with A. The result is then stored in the corresponding pixel in outputfile.

Not sure if you need a programmatic approach or a manual one, but QGis has a raster calculator. See related question with answer here.

5
  • +1 untill test for validation. I was looking for a programmatic approach, but others may be interested by the manual one. :)
    – Hugolpz
    Commented Aug 20, 2013 at 3:19
  • Note: I need to polygonize afterward. So I need to set values >= 50 to 50 (one same value).
    – Hugolpz
    Commented Aug 20, 2013 at 3:35
  • Ok ! new calc is equal to 50 when -A>50 : gdal_calc.py -A input.tif --outfile=result.tif --calc="50*(A>50)" --NoDataValue=0 . Please add this operation and explanation to your answer so I validate it !
    – Hugolpz
    Commented Aug 20, 2013 at 3:39
  • 2
    Thanks, I made some changes. Note that 50*(A>50) results in either 50 or 0.
    – Oyvind
    Commented Aug 20, 2013 at 3:59
  • Maybe a silly addition, but when inserting the calc expression with these conditionals, remember to wrap the "" in outer '' quotes. This is necessary here where if the expression was just A*50 it doesn't want the double quotes.
    – kpierce8
    Commented Feb 26 at 18:41

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.