基本上就是这样。我知道你可以使用rast()读取一个栅格文件夹,但我只想将两个分别读入的栅格叠加在一起。谢谢。
raster::stack
,您可以将其用于多个参数 (stack(x1,x2,x3)
) 或列表(stack(list(x1,x2, x3))
)。c
就不再是这样。您需要区分:c(x1, x2, x3)
- 当提供参数作为列表时,请使用 rast(list(x1,x2,x3))
。library(terra)
#> terra 1.5.21
x <- rast(xmin=-110, xmax=-80, ymin=40, ymax=70, ncols=30, nrows=30)
values(x) <- 1:ncell(x)
many_rasters <- list(x,x)
## this works
rast( many_rasters)
#> class : SpatRaster
#> dimensions : 30, 30, 2 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : -110, -80, 40, 70 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> sources : memory
#> memory
#> names : lyr.1, lyr.1
#> min values : 1, 1
#> max values : 900, 900
## just using c creates a list
c(many_rasters)
#> [[1]]
#> class : SpatRaster
#> dimensions : 30, 30, 1 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : -110, -80, 40, 70 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source : memory
#> name : lyr.1
#> min value : 1
#> max value : 900
#>
#> [[2]]
#> class : SpatRaster
#> dimensions : 30, 30, 1 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : -110, -80, 40, 70 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source : memory
#> name : lyr.1
#> min value : 1
#> max value : 900
terra::c()
返回如下错误:
要在Error: 'c' is not an exported object from 'namespace:terra'
terra
中堆叠栅格,只需使用基本的c()
函数即可。terra::c()
。它可以组合SpatRaster
对象,只要这些对象的范围和分辨率相同。
do.call
不是一个好的方法,因为你可以使用rast(list(x1,x2,x3))
。 - Robert Hijmans