Search code examples
rrasterr-rasterr-sprgdal

stack and brick function error despite all of the rasters have been


Good day everyone..

I have 13 bioclimatic variables (in .tiff format) that I will used to perform sdm by using dismo package. I followed the tutorial written by Robert J. Hijmans and Jane Elith. However, when I tried to stack all of the variables, I got the following error

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

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

All of my file's coordinate system, extent, and cell size have been adjusted so they are all the same.. when I tried to used the alternative brick function, I got the following error :

Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. In addition: There were 12 warnings (use warnings() to see them)

I used the warning() message but it was empty..

do any of you have any hints regarding what may be the cause of such errors? i've tried to google it, but unfortunately, no answer can solve it. Thank you in advance..

Here presented is the fraction of the transcript

#setting the workspace
 setwd("D:/Riset/MaxentSelaginella/newpaperproject_part2/MakalahVI/Workspace_R")

#Loading Libraries
 library("sp")
 library("raster")
 library("maptools")
 library("rgdal")
 library("dismo")
 library("rJava")

 #open the csv file
 obs.data <- read.csv(file = "data3/Selaginella_plana.csv", sep = ",")

 #open Environmental Data
 files <- list.files(path = "data3/tif/", pattern = ".tif", full.names=TRUE)

 #stacking all the files
  predictors <- brick(files)

Solution

  • I guess you need to use stack instead of brick. As per brick help, in fact:

    A RasterBrick is a multi-layer raster object. They are typically created from a multi-layer (band) file; but they can also exist entirely in memory. They are similar to a RasterStack (that can be created with stack), but processing time should be shorter when using a RasterBrick. Yet they are less flexible as they can only point to a single file.

    So, if we try to “stack” multiple files:

    library(raster)
    r <- raster(ncols = 100, nrows = 100, vals = 1:10000)
    rfile1 <- tempfile(fileext = ".tif")
    writeRaster(r, filename = rfile1)
    rfile2 <- tempfile(fileext = ".tif")
    writeRaster(r, filename = rfile2)
    
    files_to_stack <- c(rfile1, rfile2)
    

    This fails:

    brick(files_to_stack)
    #> Warning in if (x == "" | x == ".") {: the condition has length > 1 and only
    #> the first element will be used
    #> Warning in if (!start %in% c("htt", "ftp")) {: the condition has length > 1
    #> and only the first element will be used
    #> Warning in if (fileext %in% c(".GRD", ".GRI")) {: the condition has length
    #> > 1 and only the first element will be used
    #> Warning in if (!file.exists(x)) {: the condition has length > 1 and only
    #> the first element will be used
    .....
    #> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...): Cannot create a RasterLayer object from this file.
    

    While this works:

    stack(files_to_stack)
    #> class       : RasterStack 
    #> dimensions  : 100, 100, 10000, 2  (nrow, ncol, ncell, nlayers)
    #> resolution  : 3.6, 1.8  (x, y)
    #> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    #> names       : file46e41bcd78e3, file46e43ea75bad 
    #> min values  :                1,                1 
    #> max values  :            10000,            10000
    

    If you want to have a brick to get some gain in “efficiency” in further processing, you can save the different "layers" as a multiband tiff, and then open using brick:

    rfile_multi <- tempfile(fileext = ".tif")
    writeRaster(stack(files_to_stack), filename = rfile_multi)
    brick(rfile_multi)
    
    #> class       : RasterBrick 
    #> dimensions  : 100, 100, 10000, 2  (nrow, ncol, ncell, nlayers)
    #> resolution  : 3.6, 1.8  (x, y)
    #> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
    #> data source : D:\RTemp\RtmpacXztJ\file4808784f268c.tif 
    #> names       : file4808784f268c.1, file4808784f268c.2 
    #> min values  :                  1,                  1 
    #> max values  :              10000,              10000
    

    Created on 2018-11-10 by the reprex package (v0.2.1)