4

I have a script where I read a raster stored as .tif :

f_treecover   <- raster('Landcover_data/treecover_res_100_q.tif')       

The script used to run fine a few months ago, but now I get the following error message:

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

I can read the file with the 'rast' function in terra:

f_treecover   <- rast('Landcover_data/treecover_res_100_q.tif')      
> f_treecover
class       : SpatRaster 
dimensions  : 1400, 3600, 1  (nrow, ncol, nlyr)
resolution  : 0.1, 0.1  (x, y)
extent      : -180, 180, -60, 80  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
source      : treecover_res_100_q.tif 
name        : treecover_res_100_q 

However, all the other objects in the script are RasterLayers , so I need this object to be one as well. I tried coercing the resulting SpatRaster into a Raster with

raster(f_treecover)

, but that leads to the same error message as when originally trying to read the .tif file.

I probably installed an updated version of the raster package in the meanwhile (now using 3.4-10), could that be causing the issue?

Lena
  • 311
  • 2
  • 10

4 Answers4

4

If you do not have the rgdal package installed, you get

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create RasterLayer object from this file; perhaps you need to install rgdal first

In which case you should install rgdal

Otherwise, make sure you are not loading an old workspace (use unlink(".RData"), quit R without saving the workspace and start again.

If that does not help: uninstall R and install the latest version and packages again.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Hi Robert, thanks for the workaround, that works. Here's a linkt to the file: https://wetransfer.com/downloads/0ff262b5b3ff0b815b74a6cc976985ff20210629082827/98a957 Still curious what the issue is.. – Lena Jun 29 '21 at 08:28
  • It seems to be an issue with '.tif' files in general.. further down in the same script, the line "writeRaster(N2O_em_CO2eq, 'N2O_em_CO2eq.tif', overwrite=TRUE)" gives the exact same error message ("Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file.") – Lena Jun 29 '21 at 08:53
  • You probably need to (re-)install `rgdal` – Robert Hijmans Jun 29 '21 at 15:28
  • Thanks for the suggestion. rgdal was installed and loaded. I uninstalled rgdal and installed it again (version 1.5-23), but the problem remains. – Lena Jun 30 '21 at 07:33
  • Also make sure you are loading an old workspace (use `unlink(.RData)`, and close and start R again. Only load the raster package and try again. It works fine for me and nobody else is complaining so it must be something on your system. Does `readGDAL(filename)` work? – Robert Hijmans Jun 30 '21 at 15:42
  • I restarted R, my whole PC and un-/reinstalled all relevant packages, but the problem remains. Yes, readGDAL(filename) works. Apparently other people have the same issue (see kaz's reply below). A weird thing that I noticed is that the raster package is listed twice under packages in RStudio - "V3.4-13" under 'user library' and "V3.3-13" under 'system library'. I tried everything to uninstall the older version (remove.packages("raster", lib="C:/Program Files/R/R-4.0.2/library") doesn't give an error message) but it keeps appearing in the list.. – Lena Jul 07 '21 at 12:19
  • 2
    This suggests that the is indeed that you are loading an old workspace on startup, or have two version installed in different locations. I would suggest uninstalling R and then install R 4.1. And before you install any packages check if raster is not there. – Robert Hijmans Jul 07 '21 at 16:03
  • 1
    This solved it! I had previously uninstalled and reinstalled R via the 'Software Center' of our IT department. However, it seems that this re-installation never really removed all the old files in the system settings (for example, previously installed packages would appear again after re-installation of R). After installing R 4.1 directly (not via the 'Software Center'), the workspace was 'clean' again - and after reinstalling all required packages - the script works fine again! – Lena Jul 08 '21 at 12:27
  • This seems like a long shot since the post is from 07/2021 but...why would old workspaces via .Rdata affect `rgdal`? – hiperhiper Aug 25 '22 at 15:07
  • All I know is that loading old workspaces is a sure way to get into trouble. It is a nice option to have, I suppose, but it would be much better if R did not ask you to save them, let alone load them automatically. – Robert Hijmans Aug 26 '22 at 09:46
0

Finally, the problem was solved by completely removing and re-installing R. I had tried that earlier but apparently the un-installation was incomplete, as evidenced by the fact that packages I had installed previously were available again upon re-installation of R.

Lena
  • 311
  • 2
  • 10
0

Can also be if a tif name has a character not recognised, e.g. ā in Māui.

TJeff
  • 29
  • 8
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 12 '22 at 12:36
0

This happened to me after having no issues prior. I realized what happened was my Dropbox account was not synced and so the file was simply not being found. If you store your raster .tif object in your environment and run the code chunk below it should find your file. If not, Dropbox sync (if your file is from Dropbox) may also be your issue:

readr::read_file(tif_file)