8

I have an image which i cliped with an AOI polygon. The contour area was set to pixel value = 0 and I have a nodata value of -32768. I want to redefine the nodata value into zero. I used to do this with ArcMap easily in the raster calculator, but in QGIS I don't see how i could do it. Any ideas?

  • 2
    Do you want it purely for visualization or you need to set it to null wherein you will conduct further raster calculations? – maning Jul 20 '11 at 11:14
11

I found a solution for this. First i convert the image into a vrt layer (gdalbuildvrt) with -srcnodata 0, then i translate it back into a tif file with gdal_translate -a_nodata. Worked fine! :)

11

In QGIS, you can use Raster Calculator with the following calculation:

("your_raster" != -32768) * "your_raster"

With this calculation, if the cell value is -32768 you will get a 0 in that cell and if it is different from -32768 the cell will keep the value it had.

9

The simplest one-step and, IMO, most consistently reliable solution to reclassifying NoData to zero is to use the Reclassify Grid Values tool (SAGA) in the processing toolbox.

After selecting the raster to be reclassified, simply scroll to the bottom of the dialog, ensure the box replace no data values box is checked. The default value is zero (but you can substitute something else of course). Then uncheck the replace other values box and leave all other fields alone (so all you are doing is ensureing one box is checked and unchecking another - no other settings are required).

  • 1
    I agree. Nothing else really worked for me (my nodata value was something like -3.4e+38). – ArMoraer Apr 28 '16 at 13:29
  • Had exactly the same problem as @ArMoraer. This was the easiest solution, worked perfectly. – Rauni Nov 22 '16 at 10:48
  • I fixed the nodata to 0 value issue, but by the mean time it applied little changes to my other raster values – ZKB Jul 27 '18 at 14:52
8

I guess you can do that with the raster calculator.

If not with the help of GRASS plugin and r.null you will do it easily. That option means you will have first to create a GRASS mapset to import your raster in.

  • 5
    could you elaborate on how to do that with raster calculator? – rudivonstaden Dec 2 '12 at 10:48
  • 1
    r.null did it for me. – metasequoia Aug 16 '16 at 2:18
7

Having the same problem, in the end I used Python directly -- you may have to adjust numpy.where for your specific purpose. In the case below, the pixel values are kept as they are if they are >= 0, all other pixels -- in this case this is only ones with the no-data value -- are set to "0"

import gdal, gdalconst, numpy
maskfile = gdal.Open('C:\Users\max\Desktop\Baltic2.tif', gdalconst.GA_Update)
maskraster = maskfile.ReadAsArray()
maskraster = numpy.where((maskraster >= 0), maskraster, 0 ) 
maskband = maskfile.GetRasterBand(1)
maskband.WriteArray( maskraster )
maskband.FlushCache()
1

I have tried using both rastercalc plugin and Raster Calculator and neither worked - Syntax error. What did work was the Warp tool and specifying the source and destination nodata values (see underdark's answer to a similar question How to exclude missing values from raster layer?)

1

For python script you can use the following line before WriteArray.

maskband.SetNoDataValue(0)

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.