I have an image which I clipped 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?
16 Answers
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.
-
5Nice 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.– JohnCommented Aug 16, 2019 at 5:22
-
6
-
This should work, but doesn't for me! My values are either 1 or "no data." The "no data" values are reported as "n/a" in the properties but "no data" via the identify features tool. No variation seems to make the calculation work (i.e. "n/a", "no data", !=1, etc). I can't get the "no data" values replaced either. Pain! Commented Jan 21, 2022 at 3:49
-
1you can define/redefine the nodata value through raster tools/conversion/translate. I've found this to be a good workflow step when using the raster calculator. The other useful step is right-click/export. in this step, you can set pixel values to nodata (Tick the nodata option: add a nodata category: leave 'from' blank, and set 'to' to 0). this does work with values like -3.40282e+38.– WerafaCommented Nov 16, 2022 at 21:50
-
First use Raster/Conversion/Translate to specify a different no-data value, then use Egidi's answer with the original no-data value. Otherwise the tool skips over the no-data values in the calculation.– TedCommented Mar 20, 2023 at 22:37
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).
-
2I agree. Nothing else really worked for me (my nodata value was something like -3.4e+38).– ArMoraerCommented Apr 28, 2016 at 13:29
-
Had exactly the same problem as @ArMoraer. This was the easiest solution, worked perfectly.– RauniCommented Nov 22, 2016 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– ZKBCommented Jul 27, 2018 at 14:52
-
1Unfortunately 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: ...."– geordieCommented Mar 8, 2020 at 22:58
-
1for QGIS 3+ select
SAGA=>Raster tools=>Reclassify values (single)
– annebCommented Aug 24, 2021 at 15:30
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!
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.
-
8could you elaborate on how to do that with raster calculator? Commented Dec 2, 2012 at 10:48
-
3
-
1Agreed. This is a simple solution in QGIS for replacing nodata with values and vice versa.– Eli SCommented May 10, 2020 at 1:03
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()
For QGIS 3.28 there is a "Fill NoData cells" tool under Raster tools
-
1This answer should be up voted as of today it is the easiest and best one– BabelCommented Oct 24, 2023 at 8:08
-
For python script you can use the following line before WriteArray.
maskband.SetNoDataValue(0)
I had a similar problem but in my case, the original raster had no data as no data.
So, at first, I tried using the following approach but didn't work fine because gdal.open procedure read my no data as 255.
ds_Raster = gdal.Open(PathRaster)
if ds_Raster is None:
print('Could not open', + ds_Raster)
sys.exit(1)
## **Raster Georreference** #
geotransform = ds_Raster.GetGeoTransform()
wkt = ds_Raster.GetProjection()
Raster = ds_Raster.GetRasterBand(1).ReadAsArray()
Raster[np.isnan(Raster)] = 0
So instead of use, Raster[np.isnan(Raster)] = 0 I had to use the following comand which worked fine
Raster = np.where((Raster != 255), Raster, 0 )
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?)
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.
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).
An other solution is to use the "Reclassify by table" in the processing menu. With it you can reclass your input pixel with the -32768 nodata value to 0 and set the output nodata to 0 in the advance parameter.
I ended up using python3 and the rasterio library, to change nodata into 0.
import rasterio
x = rasterio.open('./Venice_Combined.tif',"r+")
data = x.read()
data[data == x.nodata] = 0
x.write(data)
It would be prudent to make a copy of the input file before doing this because it overwrites the original.
-
to note: the XML file is updated rapidly, but for some reason the tif file updates about 1 minute later after the code has finished running. But to me, this is the most straight forward method.– B-C B.Commented Mar 16, 2022 at 4:36
-
[AttributeError: 'DatasetReader' object has no attribute 'write']-----I am getting this error for "data[data == x.nodata] = 0" Commented Sep 7, 2022 at 11:20
-
1I think you need to open it with read-write permissions. Did you include the "r+" part? Commented Sep 29, 2022 at 21:33
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.
To replace the "-3.4028230607371e+38" value with "no data", I used Raster Calculator with the following expression:
( "myraster" ) / ( "myraster" > 0 )
I guess you can change the 0 value to your convenience....
-
For me this replaced everything less than 0 EXCEPT "-3.4028230607371e+38" with the value "-3.4028230607371e+38" Commented Feb 21 at 19:33
After trying many of the suggestions mentioned above, I found a simple solution which worked for me in QGIS 3.20.1, with a NODATA value of -3.40282e+38. I could not get the normal raster calculator method described in other answers to work.
However, using the gdal raster calculator in the Processing Toolbox (gdal_calc), I was able to do this with a calculation of "greater(A ,0) * A" and adding the command line parameter "--hideNoData". The calculation causes anything 0 and under to become 0, and the command line parameter hides the no-data from the script so it's calculated as a regular number.