3

In R I create a raster stack using the following code

#load rasters in a file list and store it to a variable        
env_data<- list.files(path="C:/R_Data/WORK/env_stack", pattern='tif$', full.names = TRUE)
#create layer stack of predictors
env_predictors<- stack(env_data, quick=TRUE)
env_predictors

This works and I don't get an error. But when I try to extract values to my point data with

pc_values<- extract(env_predictors, pc_select)

I get the following

Error: Failure during raster IO

I created/prepared my rasters in QGIS and ArcGIS (slope, aspect, climate data,...). In ArcGIS I masked all rasters by another raster (always the same) and set this raster as snap raster in the environmental settings to get the same extent. All rasters have the same resolution of 50 meter. When I visually check this in QGIS or ArGIS it seems to be ok and all rasters are congruent.

I also excluded some of the rasters and sometimes I don't get the error.

Why do I get this error? I'm just starting to work with R

EDIT: I could limit the error to some files (the DEM and derivatives) and recreated them. That doesn't help either. I also got the error when I try to plot one of these rasters plot() and when I write the raster stack on disk writeRaster()

2
  • The most likely cause for a raster I/O error is an I/O error during raster operations. There are a number of possible causes, but a bad block on the disk device is a likely option. There is no way that we can explore possibilities for you, but the conditional nature of the failure offers an opportunity to isolate the files with bad data block(s) in a "DontDelete" folder and copy in replacement data.
    – Vince
    Commented Nov 1, 2016 at 10:21
  • Thank you Vince for answering. I recreated my rasters and put them to another (external) disk and got the same error message. I found out that it must have to do with my DEM and the derivatives.
    – parallax
    Commented Nov 3, 2016 at 7:28

2 Answers 2

4

You are using the quick=TRUE argument. While this is convenient in overriding checks for extent, resolution and dimensions it does cause problems down the line. I would bet that if this argument was FALSE, your call to stack would fail because something does not match in your rasters. I am also fairly sure that this is the source of your error in extract.

Please follow good data management practices and establish a common analysis environment (matching projection, extent, rows, columns and resoultion) for your rasters. The quick=TRUE argument is intended to override this raster environment consistency check so, one can store multiple rasters in a single object without creating multiple raster objects. This is a nice feature when your intent is single raster analysis iand you just need a container to store your rasters in that your can iterate through. The extract function expect a common environment.

There is always the possibility that there is a true corrupt raster giving you a disk IO error. You can perform a consistency check by looping through your raster list and storing the important characteristics in a list object, then collapsing to a data.frame. Everything should match except the raster name.

env_data<- list.files(path="C:/R_Data/WORK/env_stack", pattern='tif$', full.names = TRUE)

        r.env <- list()
          for(i in env_data) {
            r <- try( raster(i) )
            r.env[i] <- data.frame(name = i, nrow=nrow(r), ncol=ncol(r), res=res(r)[1],               
                                   proj=proj4string(r), xmin=extent(r)[1],             
                                   xmax=extent(r)[2], ymin=extent(r)[3],                     
                                   ymax=extent(r)[4])
        }

do.call("rbind", r.env)   
1
  • Thank you, Jeffrey Evans,I could manage this issue with a for loop that makes all my raster layers congruent see here. After that I didn't need quick=TRUE and got rid of the error.
    – parallax
    Commented Apr 15, 2017 at 17:59
3

Sometimes another error messaged is masked by this message. I am not sure why this happens but might be related to an outdated package version & large file sizes (make sure extents overlap, same projections etc).

In other instances a portion of the raster file is damaged, but everything else works fine. I discovered this by looping through watersheds and having several continually fail.

  • Try recreating your original raster layer or open it in a program like QGIS hit save as and rename it 'my_raster_2.tif' open it up in R again and you'll see that the raster IO error is gone.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.