3

I have a large number of rasters in grid format ("asc"). I created a list of files:

files <- list.files("D:/......../EnvironData", pattern='asc',
 full.names = TRUE)

Then I want to stack all files:

dataStack <- raster::stack(files)

Unfortunately, I get the following error:

Error in .local(.Object, ...) :

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

All raster files have the same spatial dimension and extent.

pat-s
2911 silver badge17 bronze badges
asked Jan 15, 2017 at 14:53
7
  • 1
    Likely one of your files is invalid somehow. Try lapply(seg_along(files), function(x) {print(x); raster(files[x])}) - the last number printed will be the first bad file (or just the last file). Commented Jan 15, 2017 at 15:07
  • @mdsumner Thanks for suggestion. I did not find problem in my files. All are perfect. Commented Jan 15, 2017 at 16:02
  • Can you insert the output of the lapply call suggested by mdsummer into your question? Commented Jan 15, 2017 at 16:05
  • 1
    @pat-s I have 54 rasters, I tried if any of two rasters get stacked. Unfortunately none. variables Commented Jan 15, 2017 at 16:26
  • 1
    raster has some dusty corners, it's expected for something so general. You could use rgdal::readGDAL to read them individually then stack those, and there's another reader for this format in maptools, or maybe sp. The more you can explore and report specifics about the easier to debug. Commented Jan 15, 2017 at 21:29

1 Answer 1

0

This problem happened to me too, but it was because my directory was large. R was unable to find the way, so he gave this answer: Error in .rasterObjectFromFile (x, band = band, objecttype = "RasterLayer",: Cannot create a RasterLayer object from this file. (File does not exist)

After I put it in another folder with a smaller path, it worked.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jan 26, 2021 at 2:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.