Raster – Solving ‘Error: Failure during Raster IO’ in Data Extraction

errorextractrraster

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()

Best Answer

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)   
Related Question