[R-sig-Geo] write NA with writeRaster

Bastien Ferland-Raymond bastien.ferland-raymond.1 at ulaval.ca
Tue Nov 9 17:46:47 CET 2010


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


More information about the R-sig-Geo mailing list