Raster stack does not seem to want to read file

46 Views Asked by At

Im trying to put several raster files (.tif) into one stack using the stack function from the raster package. As i do this i run into two errors, namely:

> covStack <- stack(paste0(getwd(),"/", cov.lst))
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file.
In addition: Warning message:
`C:\Users\"fileway"\jbas_14.tif.vat.cpg' not recognized as a supported file format. (GDAL error 4) 

The code that I am trying to run is the following:

setwd(file.path("C:/Users/..."))

library(raster)
library(RSAGA) 
library(rgdal)
library(sf)
library(terra)

cov.lst <- list.files(path = getwd(), pattern =".tif")

covStack <- stack(paste0(getwd(),"/", cov.lst))

I have tried reinstalling all versions of R and all the packages aswell and individually installing only the packages that are needed and their dependencies. I have also tried unlink(".RData"). Further I have tried using the rast function in terra aswell which returns the same errors.

Thank you for any assistance.

1

There are 1 best solutions below

0
Robert Hijmans On BEST ANSWER

The error message seems clear enough

C:\Users\"fileway"\jbas_14.tif.vat.cpg' not recognized as a supported file format

To only use the files that end on tif you can add the $ sign to the pattern

cov.lst <- list.files(pattern =".tif$")

And now you can do

s <- raster::stack(cov.lst)

Or use terra instead

r <- terra::rast(cov.lst)