2

How can I set all negative values in a raster to NaN or no data?

I've made a virtual raster to link data and coordinates from a HDF5 then using something like this:

gdalwarp -geoloc -tr 3000 3000 -srcnodata -9999 -dstnodata -9999 -r average -s_srs EPSG:4326 -t_srs '+proj=cea +lon_0=0 +lat_ts=30 +ellps=WGS84 +units=m' input1.vrt output1.tif
1

Gdal_calc.py manual http://www.gdal.org/gdal_calc.html has an example that might work for you:

set values of zero and below to null

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

  • Thanks for that! My concern is: what does gdalwarp do with the nodata or negative values? I'd like to get rid of them first then warp to "-tr 3000 300". – rakbar Jun 7 '16 at 17:38
  • One fixed value can be marked as nodata. You seem to have range of negative values which you are not interested in but gdalwarp can't handle such situation. Gdal_calc edits your raster so that zero and negative values are changed into zero which is marked as nodata. Nodata values are not considered when warping algorithm is calculating for example average values. – user30184 Jun 7 '16 at 18:08
  • Yes, I have a range of negative which need to be removed first. So I tried the following: 'gdal_translate -projwin_srs EPSG:4326 -of GTiff input1.vrt out1.tif gdal_calc.py -A out1.tif --outfile=out1_no_zero.tif --calc="A*(A>=0)" --NoDataValue=0 gdalwarp -tr 3000 3000 -srcnodata 0 -dstnodata 0 -r average -s_srs EPSG:4326 -t_srs '+proj=cea +lon_0=0 +lat_ts=30 +ellps=WGS84 +units=m' out1_no_zero.tif final_no_zero.tif' Which returns: Unable to compute a transformation between pixel/line and georeferenced coordinates for out1_no_zero.tif. There is no affine transformation and no GCPs – rakbar Jun 7 '16 at 19:20
  • Try to localize the error. Do one step at a time, check what you have with gdalinfo and by opening the interim result with QGIS. Check also projection and histograms.. – user30184 Jun 7 '16 at 19:36
  • I believe the issue is from using the virtual raster and the lat/lon coordinate information not carrying over. A quick and dirty fix was making a new vrt with the source data from the output of gdal_calc. – rakbar Jun 7 '16 at 20:03

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.