当栅格数量可变时,在特定位置连接多个栅格

时间:2018-12-21 10:34:58

标签: r imagemagick raster

给出“ n”个栅格,每个栅格正好为100x100像素,我想将它们全部合并在一个图中,以便每行正好有4个图像。对于n> 4,应创建一个新行,依此类推。因此,创建的空图的尺寸将沿x轴固定,但是y轴将取决于栅格的数量。我使用“ magick”包中的image_montage()函数生成蒙太奇,如下所示:

mag_montage <- list()
for(ii in 1:n){
filelist_crop <- list.files()[grep(".png",list.files())]  
mag_montagetemp <- image_read(filelist_crop)
mag_montage[[ii]] <- image_montage(mag_montagetemp)
}

但是我不能以这种方式控制每个单个文件在文件蒙太奇中的特定位置。知道位置非常关键,因为我需要从组合栅格中选择某些xy坐标(使用“定位器”)以进行某些下游处理。任何帮助都感激不尽。谢谢。

1 个答案:

答案 0 :(得分:1)

通过这种方式,您可以将列表rlist中的所有栅格绘制成一个包含4列的图形:

library(raster)

n <- 26

rlist <- lapply(1:n,function(x) raster(system.file("external/test.grd", package="raster")))

par(mfrow=c(ceiling(n/4),4))

for (ii in 1:length(rlist)){

  plot(rlist[[ii]])

## additional options for plot to omit legend and box
#bty="n", box=FALSE, axes=F, legend=F

}

enter image description here