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

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

我有一个大小不同但尺寸和分辨率相同的栅格列表。我试图将它们拼接在一起以创建一个栅格,因为我必须计算欧几里得距离,并且当它们被分割时它没有意义。我已经阅读了有关光栅镶嵌的其他问题(例如添加 names(ras_list)<-NULLras_list <- ras_list[lapply(ras_list,length)>0]),但我一直收到消息

"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"

我的代码片段:

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)

做 typeof(ras_list[[i]]) 为每个返回 S4。 谢谢。

编辑:添加 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()
)

具体发生在什么地方?它在读取输入文件(但不是 tif 文件)时查找。我的猜测是您包含了一个不正确的文件名。我已将参数更改为 list.files 以可能解决该问题,并提出了一些其他建议。

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)

如果这没有帮助,请说明发生这种情况的确切位置。如果它在 lapply 中,那么你应该回到你的循环,看看哪个文件是罪魁祸首。