添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Suddenly error 'Cannot create a RasterLayer object from this file.' in script that worked before

Ask Question

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?

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.

Hi Robert, thanks for the workaround, that works. Here's a linkt to the file: wetransfer.com/downloads/… Still curious what the issue is.. – Lena Jun 29, 2021 at 8: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, 2021 at 8:53 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 7, 2021 at 16:03 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 8, 2021 at 12:27 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. – Community Apr 12, 2022 at 12:36

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)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.