1

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

“矩阵中的错误(unlist(ini),ncol = 2,byrow = TRUE):'数据'必须是向量类型,是'NULL'调用:do.call ... .rasterObjectFromFile -> .rasterFromRasterFile -> readIniFile -> 矩阵”

我的代码片段:

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

1 回答 1

1

这究竟发生在哪里?它在读取输入文件(但不是 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那么你应该回到你拥有的循环,看看哪个文件是罪魁祸首。

于 2018-12-14T21:59:38.173 回答