在 R 中拼接多波段光栅时出错

问题描述 投票:0回答:3

我正在尝试将多个多波段数据集镶嵌到 R 中的一个大型多波段栅格中。我试图用这段代码来做,但它返回一个单波段图像。

library(sp)  
library(raster)  
library(rgdal)  
setwd("C:\\Projects\\Rice-fallow_4_states\\Bihar\\S1")  
x <- list.files(".\\", pattern='tif$',
                    full.names = TRUE) # list all the rasters  
X1<- as.list(x)    

allrasters1 <- lapply(X1, raster)  
x.mosaic <- do.call(merge,allrasters1)   
names(x.mosaic)  
x.mosaic  
plot(x.mosaic)  
r r-raster r-mosaic
3个回答
1
投票

我会建议你应该使用

terra
代替
raster
包。
terra
raster
快得多。您可以使用以下代码

library(terra) 

setwd("C:\\Projects\\Rice-fallow_4_states\\Bihar\\S1")  
x <- list.files(".\\", pattern='tif$',
                full.names = TRUE) # list all the rasters  

allrasters1 <- lapply(x, rast)  
x.mosaic <- do.call(mosaic, allrasters1)  
names(x.mosaic)  
x.mosaic  
plot(x.mosaic) 

0
投票

当您使用

raster()
在 R 中加载栅格时,它只会加载图像的一层。如果要加载整个多波段图像,请使用
stack()
brick()
。我相信
brick()
是具有多个波段的图像最正确的一个,但我不经常处理这些,所以我还没有找到它和
stack()
之间有意义的区别。


0
投票

这就是你如何使用 terra(“光栅”的替代品)做到这一点

library(terra)
ff <- list.files(".\\", pattern='tif$',
                    full.names = TRUE) 
x <- sprc(ff) 
m <- mosaic(x)
© www.soinside.com 2019 - 2024. All rights reserved.