rgisrastermosaic

How do I address R raster mosaic error: 'data' must be of a vector type, was 'NULL'?


I have a list of rasters of varying sizes but the same dimensions and resolution. I am trying to mosaic them together to create one raster since I have to calculate Euclidean distance and it doesn't make sense when they are fragmented. I've read other questions on raster mosaic-ing (e.g. adding names(ras_list)<-NULL and ras_list <- ras_list[lapply(ras_list,length)>0]) but I keep receiving the message

"Error in matrix(unlist(ini), ncol = 2, byrow = TRUE) : 'data' must be of a vector type, was 'NULL' Calls: do.call ... .rasterObjectFromFile -> .rasterFromRasterFile -> readIniFile -> matrix"

my code snippet:

library(raster)
library(rgdal)
rnames<-list.files(path=".", pattern="*.tif")
ras_list<-list()
for (i in 1:length(rnames)){
  ras_list[[i]]<-raster(rnames[i])
}
names(ras_list) <- NULL
ras_list$fun <- mean
ras_list <- ras_list[lapply(ras_list,length)>0]
rast_mosaic <- do.call(mosaic,ras_list)
writeRaster(rast_mosaic, filename="ALL_00N.tif", format="GTiff", overwrite=TRUE)

doing typeof(ras_list[[i]]) gives back S4 for each one. Thank you.

EDIT: adding dput(ras_list[[1]]):

new("RasterLayer"
    , file = new(".RasterFile"
    , name = "[hidden]/output_00N_000E_010E.tif"
    , datanotation = "FLT4S"
    , byteorder = "little"
    , nodatavalue = -Inf
    , NAchanged = FALSE
    , nbands = 1L
    , bandorder = "BIL"
    , offset = 0L
    , toptobottom = TRUE
    , blockrows = 1L
    , blockcols = 72001L
    , driver = "gdal"
    , open = FALSE
)
    , data = new(".SingleLayerData"
    , values = logical(0)
    , offset = 0
    , gain = 1
    , inmemory = FALSE
    , fromdisk = TRUE
    , isfactor = FALSE
    , attributes = list()
    , haveminmax = TRUE
    , min = 0
    , max = 100
    , band = 1L
    , unit = ""
    , names = "output_00N_000E_010E"
)
    , legend = new(".RasterLegend"
    , type = character(0)
    , values = logical(0)
    , color = logical(0)
    , names = logical(0)
    , colortable = logical(0)
)
    , title = character(0)
    , extent = new("Extent"
    , xmin = -0.000138889
    , xmax = 20.000138889
    , ymin = -10.000138889
    , ymax = 0.000138889
)
    , rotated = FALSE
    , rotation = new(".Rotation"
    , geotrans = numeric(0)
    , transfun = function () 
NULL
)
    , ncols = 72001L
    , nrows = 36001L
    , crs = new("CRS"
    , projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
)
    , history = list()
    , z = list()
)

Solution

  • Where exactly does this occur? It looks when reading an input file (but not a tif file). My guess is that you have included a filename that is not correct. I have changed the argument to list.files to possibly fix that, and made some other suggestions.

    library(raster)
    library(rgdal)
    rnames <- list.files(path=".", pattern="\\.tif$")
    ras_list <- lapply(rnames, raster)
    names(ras_list) <- NULL
    ras_list$fun <- mean
    ras_list$filename="ALL_00N.tif"
    ras_list$overwrite = TRUE
    rast_mosaic <- do.call(mosaic,ras_list)
    

    If this does not help, please show is where exactly this happens. If it is in the lapply then you should go back to the loop you have, to see which file is the culprit.