Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gdal: How to conditionally assign a new value to pixels of a raster image?

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

# download:
curl -o ETOPO1.zip 'http://www.ngdc.noaa.gov/mgg/global/relief/ETOPO1/data/ice_surface/grid_registered/georeferenced_tiff/ETOPO1_Ice_g_geotiff.zip'
# unzip:
unzip ETOPO1.zip
# crop:
gdal_translate -projwin -005.50 051.30 10.00 041.00 ETOPO1_Ice_g_geotiff.tif crop.tif

Given an elevation threshold n = 50 (meters)

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


Related to : https://stackoverflow.com/questions/18300210/, Gdal: How to get a pixel's value from raster image?

Help?: gdallocationinfo can get the value of one point (without iterating the whole image):

$ gdallocationinfo crop.tif 1 1 -valonly
> 73
like image 978
Hugolpz Avatar asked Aug 19 '13 13:08

Hugolpz


1 Answers

Use gdal_calc.py. By example :

gdal_calc.py -A crop.tif --outfile=level0100.tif --calc="100*(A>100)"     --NoDataValue=0

to read as : `given an input -A (crop.tif), with output level0100.tif, when a pixel have a first band's value A>100 set value to 100. Else, set is as 0.

Within --calc="", you may set other conditions. If you want to set values such 100 < v < 200 to 100, then :

gdal_calc.py -A crop.tif --outfile=level0100.tif --calc="100*(100<A<200)"     --NoDataValue=0

should work. (this last one was not tested)

like image 94
Hugolpz Avatar answered Nov 12 '22 19:11

Hugolpz