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