7

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

10 Answers 10

13

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.

| improve this answer | |
  • 2
    Nice solution. Just a note for others trying this: In QGIS, the nodata value for a layer can be found under the layer Properties, then Information (top item), then scroll down to Bands. – John Aug 16 '19 at 5:22
  • Note that this does not work for values like -3.40282e+38. – geordie Mar 8 at 23:12
12

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

| improve this answer | |
  • 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
  • Unfortunately this method no longer appears to work in QGIS 3.8. The processing form will not execute without tweaking all of the other settings. Error: "Wrong or missing parameter value: ...." – geordie Mar 8 at 22:58
12

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!

| improve this answer | |
9

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.

| improve this answer | |
  • 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
  • 1
    Agreed. This is a simple solution in QGIS for replacing nodata with values and vice versa. – Eli S May 10 at 1:03
8

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()
| improve this answer | |
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?)

| improve this answer | |
1

What worked for me was exporting the raster as .asc file, then opening in a text editor and using find and replace to replace the old NoData value with the value that I wanted.

| improve this answer | |
1

In my case, it was a satellite image I needed to convert to 8 bit and it kept outputting no data values when using -scale in gdal_translate.

I was able to solve the issue by exporting the raster(right-click layer -> export -> save layer as...) to a new file as rendered data (checkbox at the top of the dialog window).

| improve this answer | |
0

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

maskband.SetNoDataValue(0)
| improve this answer | |
0

A previous answer by @simo suggested to use GRASS r.null.

My "no data" value was set to -9. To get r.null to work for me I had to first run r.null to set -9 as "NULL", and in a second step again run r.null to replace the NULL value with 0. My resulting raster has all pixels defined (STATISTICS_VALID_PERCENT=100), the new minimum is 0, and the maximum is the same as in the original raster.

| improve this answer | |

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.