Search code examples
rgisrgdal

R Raster error: Cannot create a RasterLayer object from this file. (file does not exist)


Trying to pull in and scale a Landsat image but receive an error, this seems to be a somewhat common issue given the older questions and various ways of creating a RasterLayer object.

After loading the libraries I need I get to this and hit an issue:

    Directory <- "D:/Geo Files/LANDSAT"
prefix <- "CU_LC08.001_"
suffix <- "_doy2020222_aid0001.tif"  


## Get band 2 reflectance (blue, 0.45-0.515 micron)
sr2 <- raster(paste0(Directory,prefix,"SRB2",suffix))
Error in .local(.Object, ...) : 

*Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file. (file does not exist)*
> sr2 = sr2*0.0001 #Scale
Error: object 'sr2' not found

The full name of the first (SRB2) file I want to use is: CU_LC08.001_SRB2_doy2020222_aid0001.tif - the name of the file as it's downloaded, unchanged.

The rest of the code continues through the other files to go through each SRB and later calculate NDVI, etc

i.e. ## Get band 3 reflectance (green, 0.533-0.590 micron) sr3 <- raster(paste0(Directory,prefix,"SRB3",suffix)) sr3 = sr3*0.0001 #Scale

Edit 1: I tried using try using \ instead of forward slashes and got exactly the same error. R version is R-4.0.3

Any help would be appreciated, this was provided to us in a hackathon and I am new to R, it should be simple to run but if the file cannot be read..?

EDIT 2: The issue was with the end of the path, it needed "\"

Directory <- "D:\\Geo Files\\LANDSAT\\"

But when trying to get another file to work from a different folder later in the code, the same issue appears, and the above fix did nothing:

 Directory <- "D:\\Geo Files\\ECOSTRESS\\USCities\\"
LST_ECO <- raster(paste0(Directory,"SDS_LST_doy2020260040819_aid0001.tif"))
Error in .local(.Object, ...) : 

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file. (file does not exist)

EDIT 3: Changed \Geo Files\ to "\GeoFiles\" as suggested, tried again but problem still occurs as above.


Solution

  • I had the same issue and the following was the process that finally worked.

    # location of the file
    loc_1 = "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif"
    
    # check if the file can be read into r
    tif1 = readr::read_file(loc_1)
    

    It raised the following error (edited):

    Error: Cannot read file "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif": The cloud file provider is not running.
    

    The reason was because R could not read the file as it was in an online folder.

    Confirm if the file is available offline. If it can be read by read_file function then the raster function should work.