Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 3 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:

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.

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 : 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
    
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
add comment

Your Answer

 
discard

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.