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


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.

  • +1 untill test for validation. I was looking for a programmatic approach, but others may be interested by the manual one. :)
    – Hugolpz
    Aug 20 '13 at 3:19
  • Note: I need to polygonize afterward. So I need to set values >= 50 to 50 (one same value).
    – Hugolpz
    Aug 20 '13 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
    Aug 20 '13 at 3:39
  • 2
    Thanks, I made some changes. Note that 50*(A>50) results in either 50 or 0.
    – Oyvind
    Aug 20 '13 at 3:59

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.