我正在处理MODIS MOD15A2H数据集,并希望将第一层的所有图块合并在一起。我使用gdal_translate()将.hdf层转换为.tif文件。并尝试使用do.call(merge,tiflist)来合并(see here)。我发现当列表的长度大于5时,单元格的值会发生变化。(更新:我发现与5无关。有时4个文件出错,而其中任意三个文件都运行良好。但是对于其他文件,没有这样的问题。)有人可以帮忙吗?只是不知道为什么以及在哪种情况下..call有这样的问题。
由于没有重叠的问题,我在马赛克中改变了乐趣,因为没有重叠的问题。
#transform to tif
for (i in 1:length(hdffilenames)){
gdal_translate(sdsnames[i,1], dst_dataset = paste("fpar_",substr(sdsnames[i,1],37,42),".tif",sep = ""))}
tifnames=paste("fpar_",substr(sdsnames[,1],37,42),".tif",sep = "")
h=as.integer(substr(sdsnames[,1],38,39))
subtifnames=list()
for (i in seq(1:36)){
subtifnames[i]=tifnames[h=i]
}
#merge function
mergetifList <- function(tifList){
#Internal function to make a list of raster objects from list of files.
ListRasters <- function(list_names) {
raster_list <- list() # initialise the list of rasters
for (i in 1:(length(list_names))){
tif_name <- list_names[i] # list_names contains all the names of the images in .tif format
raster_file <- raster::raster(tif_name)
raster_file[raster_file>1] <- NA
}
raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
return(raster_list)
}
#convert every raster path to a raster object and create list of the results
raster.list <-sapply(tifList, FUN = ListRasters)
# edit settings of the raster list for use in do.call and merge
names(raster.list) <- NULL
#####This function deals with overlapping areas
#raster.list$fun <- max
#run do call to implement mosaic over the list of raster objects.
mergeRas <- do.call(raster::merge, raster.list)
return(mergeRas)
}
然后我使用一些列表进行测试:
> tifname1=c("fpar_h29v09.tif", "fpar_h29v10.tif", "fpar_h29v11.tif", "fpar_h29v12.tif", "fpar_h29v13.tif")
> m_1=mergetifList(tifname1)
> plot(m_1)
> tifname2=c
("fpar_h29v08.tif", "fpar_h29v09.tif", "fpar_h29v10.tif", "fpar_h29v11.tif", "fpar_h29v12.tif")
> m_2=mergetifList(tifname2)
> plot(m_2)
> tifname3=c("fpar_h29v08.tif", "fpar_h29v09.tif", "fpar_h29v10.tif", "fpar_h29v11.tif", "fpar_h29v12.tif","fpar_h29v13.tif")
> m_3=mergetifList(tifname3)
> plot(m_3)
非常感谢!