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