Skip to content
Prev 9949 / 29559 Next

RE : write NA with writeRaster

Hello Robert,

Thank you very much for your help, it's working perfectly with your code.

Thanks for the quick and complet answer

Bastien


________________________________________
De : Robert J. Hijmans [r.hijmans at gmail.com]
Date d'envoi : 9 novembre 2010 20:38
? : Bastien Ferland-Raymond
Cc : r-sig-geo at stat.math.ethz.ch
Objet : Re: [R-sig-Geo] write NA with writeRaster

Bastien,

The way it currently works with Byte type files is that all values
below zero become zero, and all values >= 255 become NA. That, is 255
is the "missing value flag"

What is clearly wrong, is that NA (and NaN) become zero, rather than
NA. I can solve this in raster, but it seems that is also a feature of
rgdal, where it could also be fixed.

Further, what you try here should not work. This to set the NA flag
for reading, not writing.
To set the NA flag for writing (from raster version 1.6-16 and
onwards) you can use the argument NAflag=  in writeRaster (and other
functions that write files).

To use NaN as a NA flag is also questionable, because it is not really
a flag. It is (in most cases) equivalent, unless you want to
distinguish in your file between NaN and NA. And you can save NaN
values to disk with floating point values because you can store NaN
(invalid numbers) on disk. However, with byte values all you have is 8
bits and any combination is a valid number, so all you have is
0...255.

GeoTiff has "Internal nodata masks" that I have not explored. Not sure
if that will work (http://www.gdal.org/frmt_gtiff.html)

For now, this might work:

make.tiff <- function(NV=newValues,TT=Type,img=imgRaster,nom){
 pixelNDVIMatrix <- calculate_NDVI(TT,img,NV[c(1,2,3)])
 pixelNDVIMatrix[pixelNDVIMatrix == 255] <- 254
 pixelNDVIMatrix[is.na(pixelNDVIMatrix)] <- 255
 newRaster <- raster(pixelNDVIMatrix)
 NAvalue(newRaster)<- NaN
 nnom<-nom[NV[4]]
 writeRaster(newRaster, filename=nnom,datatype="INT1U",format="GTiff",
overwrite=FALSE)
}


Best, Robert


On Tue, Nov 9, 2010 at 8:46 AM, Bastien Ferland-Raymond
<bastien.ferland-raymond.1 at ulaval.ca> wrote: