R: Error thrown while using RGDAL and RASTER packa

2019-05-12 07:23发布

问题:

To whom this may concern:

Here is the source code:

GRA_D1<- raster(files[[1]])
//Sets up an empty output raster: 
GRA_D1<- writeStart(GRA_D1,filename='GRA_D1.tif', format='GTiff', overwrite=TRUE)

//Write to the raster, for loop:
for(i in 1:dim(GRA_D1)[1]){

//Extract raster values at rows
d.Frame<- matrix(NA,ncol=2,nrow=dim(GRA_D1)[2])
d.Frame[,1]<- getValues(r1[[1]],i) 
d.Frame[,2]<- getValues(r1[[2]],i)

w.Frame<- as.data.frame(d.Frame)
names(w.Frame)<- c("D1_pred_disAg","D1_pred_RK")
//Apply the predictive model:
m.pred<-predict(mod.1, w.Frame) 

//Write the predictions to the empty TIFF raster
GRA_D1<-writeValues(GRA_D1,m.pred,i) 
print(i)}

//Finish writing to the raster
GRA_D1<- writeStop(GRA_D1) 

I am attempting to write output to an empty TIFF raster, but I keep receiving the following error message:

#Error in .local(.Object, ...) : 
`general_file_path\GRA_D1.tif' does not exist in the file system,
and is not recognised as a supported dataset name.

I wonder if this is related to misusing functions in either the RGDAL or RASTER package.

Could someone please assist me?

Thanks in advance for your generosity.

Cheers, AD

回答1:

Super simple fix. Cannot believe that it is this simple and that it took me this long, but here is the answer:

"rgdal" and/or "GTiff" files don't like the use of underscores in their dataset names.

When running the code with "GRAD1.tif" (instead of "GRA_D1.tif"), all works well.



回答2:

You really should not be doing any of this, I think, as you could do:

 p <- predict(r1, mod.1, filename='GRA_D1.tif')

(and that filename works just fine)