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.hijm...@gmail.com] Date d'envoi : 9 novembre 2010 20:38 À : Bastien Ferland-Raymond Cc : r-sig-geo@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. > NAvalue(newRaster)<- NaN 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-raymon...@ulaval.ca> wrote: > Dear R-user, > > I'm not new in R but I'm new dealing with images with R. I checked over > google and the help files of the Raster package but I couldn't find my > answer. Sorry if it's simple. > > I have a matrix 100x100 that I want to transfert into a tif image. That's > pretty easy with writeRaster, however I have some NaN in my image that I want > to keep as NaN in the Tif image. > > Here is my code: > ### > make.tiff <- function(NV=newValues,TT=Type,img=imgRaster,nom){ > pixelNDVIMatrix <- calculate_NDVI(TT,img,NV[c(1,2,3)]) > newRaster <- raster(pixelNDVIMatrix) > NAvalue(newRaster)<- NaN > nnom<-nom[NV[4]] > writeRaster(newRaster, filename=nnom,datatype="INT1U",format="GTiff", > overwrite=FALSE) > } > ### > > pixelNDVIMatrix is the matrix I want to make as tif. It looks similar to: > > NaN NaN NaN 177 > NaN NaN 160 172 > NaN 212 163 179 > 230 211 166 174 > > In that example, the top left of the image in missing. When I use > writeRaster on that, it changes my NaN to 0, which is a problem. Do you know > how can I make the NaN bocome NODATA on the final image? > > My final objective is to create tif image in R that I would export to ENVI to > calculate textural variables. Having NODATA instead of 0 is important as I > don't want ENVI to evaluate the texture of the "black" area created with the > NODATA. If tif image is the problem, I could probably change to another > format. > > I'm using R 2.12 with the most recent packages > > Thanks for you help, > Bastien Ferland-Raymond > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo