Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?,

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" -A input.tif --outfile=result.tif --calc="50*(A>50)" --NoDataValue=0

share|improve this question
up vote 8 down vote accepted

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: -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.

share|improve this answer
+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 : -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
I actually made the relevant edits to your answer, hope they will be accepted. – Hugolpz Aug 20 '13 at 3:44
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 posting your answer, you agree to the privacy policy and terms of service.

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