有没有办法将多层栅格(Terra)转换为 R 中的 rasterStack 对象(Raster)?

Is there a way to convert a multi-layer raster (Terra) to a rasterStack object (Raster) in R?

如问题所述,我正在尝试将多层 terra 栅格转换为 rasterStack 对象,以便我可以将它与另一个只接受的包 (biomod2) 一起使用较旧的 raster 个对象。

有什么有效的方法吗?我唯一的其他选择似乎是将其保存为 .tif 然后使用 raster.

将其重新导入到 R

当我使用stack(terraRaster)时,它复制了一些图层。例如,我有一个堆栈,其中包含 19 个 WorldClim 生物气候变量以及地质层。这是它的样子:

> names(current.clim) # Terra rast object
 [1] "bio1"  "bio2"  "bio3"  "bio4"  "bio5"  "bio6"  "bio7"  "bio8"  "bio9"  "bio10" "bio11" "bio12" "bio13"
[14] "bio14" "bio15" "bio16" "bio17" "bio18" "bio19"

> names(stack(current.clim)) # Converted to rasterStack
 [1] "bio1"  "bio2"  "bio3"  "bio4"  "bio5"  "bio6"  "bio7"  "bio8"  "bio9"  "bio10" "bio11" "bio12" "bio13"
[14] "bio14" "bio15" "bio16" "bio17" "bio18" "bio19"

> names(stack(c(current.clim, geo))) # Converted to rasterStack with added geology variable
Error in `names<-`(`*tmp*`, value = names(from)) : 
  incorrect number of layer names
  [1] "currentclim_30sec.1.1"   "currentclim_30sec.2.1"   "currentclim_30sec.3.1"   "currentclim_30sec.4.1"  
  [5] "currentclim_30sec.5.1"   "currentclim_30sec.6.1"   "currentclim_30sec.7.1"   "currentclim_30sec.8.1"  
  [9] "currentclim_30sec.9.1"   "currentclim_30sec.10.1"  "currentclim_30sec.11.1"  "currentclim_30sec.12.1" 
 [13] "currentclim_30sec.13.1"  "currentclim_30sec.14.1"  "currentclim_30sec.15.1"  "currentclim_30sec.16.1" 
 [17] "currentclim_30sec.17.1"  "currentclim_30sec.18.1"  "currentclim_30sec.19.1"  "currentclim_30sec.1.2"  
 [21] "currentclim_30sec.1.3"   "currentclim_30sec.2.2"   "currentclim_30sec.3.2"   "currentclim_30sec.4.2"  
 [25] "currentclim_30sec.5.2"   "currentclim_30sec.6.2"   "currentclim_30sec.7.2"   "currentclim_30sec.8.2"  
 [29] "currentclim_30sec.9.2"   "currentclim_30sec.10.2"  "currentclim_30sec.11.2"  "currentclim_30sec.12.2" 
 [33] "currentclim_30sec.13.2"  "currentclim_30sec.14.2"  "currentclim_30sec.15.2"  "currentclim_30sec.16.2" 
 [37] "currentclim_30sec.17.2"  "currentclim_30sec.18.2"  "currentclim_30sec.19.2"  "currentclim_30sec.1.4"  
 [41] "currentclim_30sec.2.3"   "currentclim_30sec.3.3"   "currentclim_30sec.4.3"   "currentclim_30sec.5.3"  
 [45] "currentclim_30sec.6.3"   "currentclim_30sec.7.3"   "currentclim_30sec.8.3"   "currentclim_30sec.9.3"  
 [49] "currentclim_30sec.10.3"  "currentclim_30sec.11.3"  "currentclim_30sec.12.3"  "currentclim_30sec.13.3" 
 [53] "currentclim_30sec.14.3"  "currentclim_30sec.15.3"  "currentclim_30sec.16.3"  "currentclim_30sec.17.3" 
 [57] "currentclim_30sec.18.3"  "currentclim_30sec.19.3"  "currentclim_30sec.1.5"   "currentclim_30sec.2.4"  
 [61] "currentclim_30sec.3.4"   "currentclim_30sec.4.4"   "currentclim_30sec.5.4"   "currentclim_30sec.6.4"  
 [65] "currentclim_30sec.7.4"   "currentclim_30sec.8.4"   "currentclim_30sec.9.4"   "currentclim_30sec.10.4" 
 [69] "currentclim_30sec.11.4"  "currentclim_30sec.12.4"  "currentclim_30sec.13.4"  "currentclim_30sec.14.4" 
 [73] "currentclim_30sec.15.4"  "currentclim_30sec.16.4"  "currentclim_30sec.17.4"  "currentclim_30sec.18.4" 
 [77] "currentclim_30sec.19.4"  "currentclim_30sec.1.6"   "currentclim_30sec.2.5"   "currentclim_30sec.3.5"  
 [81] "currentclim_30sec.4.5"   "currentclim_30sec.5.5"   "currentclim_30sec.6.5"   "currentclim_30sec.7.5"  
 [85] "currentclim_30sec.8.5"   "currentclim_30sec.9.5"   "currentclim_30sec.10.5"  "currentclim_30sec.11.5" 
 [89] "currentclim_30sec.12.5"  "currentclim_30sec.13.5"  "currentclim_30sec.14.5"  "currentclim_30sec.15.5" 
 [93] "currentclim_30sec.16.5"  "currentclim_30sec.17.5"  "currentclim_30sec.18.5"  "currentclim_30sec.19.5" 
 [97] "currentclim_30sec.1.7"   "currentclim_30sec.2.6"   "currentclim_30sec.3.6"   "currentclim_30sec.4.6"  
[101] "currentclim_30sec.5.6"   "currentclim_30sec.6.6"   "currentclim_30sec.7.6"   "currentclim_30sec.8.6"  
[105] "currentclim_30sec.9.6"   "currentclim_30sec.10.6"  "currentclim_30sec.11.6"  "currentclim_30sec.12.6" 
[109] "currentclim_30sec.13.6"  "currentclim_30sec.14.6"  "currentclim_30sec.15.6"  "currentclim_30sec.16.6" 
[113] "currentclim_30sec.17.6"  "currentclim_30sec.18.6"  "currentclim_30sec.19.6"  "currentclim_30sec.1.8"  
[117] "currentclim_30sec.2.7"   "currentclim_30sec.3.7"   "currentclim_30sec.4.7"   "currentclim_30sec.5.7"  
[121] "currentclim_30sec.6.7"   "currentclim_30sec.7.7"   "currentclim_30sec.8.7"   "currentclim_30sec.9.7"  
[125] "currentclim_30sec.10.7"  "currentclim_30sec.11.7"  "currentclim_30sec.12.7"  "currentclim_30sec.13.7" 
[129] "currentclim_30sec.14.7"  "currentclim_30sec.15.7"  "currentclim_30sec.16.7"  "currentclim_30sec.17.7" 
[133] "currentclim_30sec.18.7"  "currentclim_30sec.19.7"  "currentclim_30sec.1.9"   "currentclim_30sec.2.8"  
[137] "currentclim_30sec.3.8"   "currentclim_30sec.4.8"   "currentclim_30sec.5.8"   "currentclim_30sec.6.8"  
[141] "currentclim_30sec.7.8"   "currentclim_30sec.8.8"   "currentclim_30sec.9.8"   "currentclim_30sec.10.8" 
[145] "currentclim_30sec.11.8"  "currentclim_30sec.12.8"  "currentclim_30sec.13.8"  "currentclim_30sec.14.8" 
[149] "currentclim_30sec.15.8"  "currentclim_30sec.16.8"  "currentclim_30sec.17.8"  "currentclim_30sec.18.8" 
[153] "currentclim_30sec.19.8"  "currentclim_30sec.1.10"  "currentclim_30sec.2.9"   "currentclim_30sec.3.9"  
[157] "currentclim_30sec.4.9"   "currentclim_30sec.5.9"   "currentclim_30sec.6.9"   "currentclim_30sec.7.9"  
[161] "currentclim_30sec.8.9"   "currentclim_30sec.9.9"   "currentclim_30sec.10.9"  "currentclim_30sec.11.9" 
[165] "currentclim_30sec.12.9"  "currentclim_30sec.13.9"  "currentclim_30sec.14.9"  "currentclim_30sec.15.9" 
[169] "currentclim_30sec.16.9"  "currentclim_30sec.17.9"  "currentclim_30sec.18.9"  "currentclim_30sec.19.9" 
[173] "currentclim_30sec.1.11"  "currentclim_30sec.2.10"  "currentclim_30sec.3.10"  "currentclim_30sec.4.10" 
[177] "currentclim_30sec.5.10"  "currentclim_30sec.6.10"  "currentclim_30sec.7.10"  "currentclim_30sec.8.10" 
[181] "currentclim_30sec.9.10"  "currentclim_30sec.10.10" "currentclim_30sec.11.10" "currentclim_30sec.12.10"
[185] "currentclim_30sec.13.10" "currentclim_30sec.14.10" "currentclim_30sec.15.10" "currentclim_30sec.16.10"
[189] "currentclim_30sec.17.10" "currentclim_30sec.18.10" "currentclim_30sec.19.10" "currentclim_30sec.1.12" 
[193] "currentclim_30sec.2.11"  "currentclim_30sec.3.11"  "currentclim_30sec.4.11"  "currentclim_30sec.5.11" 
[197] "currentclim_30sec.6.11"  "currentclim_30sec.7.11"  "currentclim_30sec.8.11"  "currentclim_30sec.9.11" 
[201] "currentclim_30sec.10.11" "currentclim_30sec.11.11" "currentclim_30sec.12.11" "currentclim_30sec.13.11"
[205] "currentclim_30sec.14.11" "currentclim_30sec.15.11" "currentclim_30sec.16.11" "currentclim_30sec.17.11"
[209] "currentclim_30sec.18.11" "currentclim_30sec.19.11" "currentclim_30sec.1.13"  "currentclim_30sec.2.12" 
[213] "currentclim_30sec.3.12"  "currentclim_30sec.4.12"  "currentclim_30sec.5.12"  "currentclim_30sec.6.12" 
[217] "currentclim_30sec.7.12"  "currentclim_30sec.8.12"  "currentclim_30sec.9.12"  "currentclim_30sec.10.12"
[221] "currentclim_30sec.11.12" "currentclim_30sec.12.12" "currentclim_30sec.13.12" "currentclim_30sec.14.12"
[225] "currentclim_30sec.15.12" "currentclim_30sec.16.12" "currentclim_30sec.17.12" "currentclim_30sec.18.12"
[229] "currentclim_30sec.19.12" "currentclim_30sec.1.14"  "currentclim_30sec.2.13"  "currentclim_30sec.3.13" 
[233] "currentclim_30sec.4.13"  "currentclim_30sec.5.13"  "currentclim_30sec.6.13"  "currentclim_30sec.7.13" 
[237] "currentclim_30sec.8.13"  "currentclim_30sec.9.13"  "currentclim_30sec.10.13" "currentclim_30sec.11.13"
[241] "currentclim_30sec.12.13" "currentclim_30sec.13.13" "currentclim_30sec.14.13" "currentclim_30sec.15.13"
[245] "currentclim_30sec.16.13" "currentclim_30sec.17.13" "currentclim_30sec.18.13" "currentclim_30sec.19.13"
[249] "currentclim_30sec.1.15"  "currentclim_30sec.2.14"  "currentclim_30sec.3.14"  "currentclim_30sec.4.14" 
[253] "currentclim_30sec.5.14"  "currentclim_30sec.6.14"  "currentclim_30sec.7.14"  "currentclim_30sec.8.14" 
[257] "currentclim_30sec.9.14"  "currentclim_30sec.10.14" "currentclim_30sec.11.14" "currentclim_30sec.12.14"
[261] "currentclim_30sec.13.14" "currentclim_30sec.14.14" "currentclim_30sec.15.14" "currentclim_30sec.16.14"
[265] "currentclim_30sec.17.14" "currentclim_30sec.18.14" "currentclim_30sec.19.14" "currentclim_30sec.1.16" 
[269] "currentclim_30sec.2.15"  "currentclim_30sec.3.15"  "currentclim_30sec.4.15"  "currentclim_30sec.5.15" 
[273] "currentclim_30sec.6.15"  "currentclim_30sec.7.15"  "currentclim_30sec.8.15"  "currentclim_30sec.9.15" 
[277] "currentclim_30sec.10.15" "currentclim_30sec.11.15" "currentclim_30sec.12.15" "currentclim_30sec.13.15"
[281] "currentclim_30sec.14.15" "currentclim_30sec.15.15" "currentclim_30sec.16.15" "currentclim_30sec.17.15"
[285] "currentclim_30sec.18.15" "currentclim_30sec.19.15" "currentclim_30sec.1.17"  "currentclim_30sec.2.16" 
[289] "currentclim_30sec.3.16"  "currentclim_30sec.4.16"  "currentclim_30sec.5.16"  "currentclim_30sec.6.16" 
[293] "currentclim_30sec.7.16"  "currentclim_30sec.8.16"  "currentclim_30sec.9.16"  "currentclim_30sec.10.16"
[297] "currentclim_30sec.11.16" "currentclim_30sec.12.16" "currentclim_30sec.13.16" "currentclim_30sec.14.16"
[301] "currentclim_30sec.15.16" "currentclim_30sec.16.16" "currentclim_30sec.17.16" "currentclim_30sec.18.16"
[305] "currentclim_30sec.19.16" "currentclim_30sec.1.18"  "currentclim_30sec.2.17"  "currentclim_30sec.3.17" 
[309] "currentclim_30sec.4.17"  "currentclim_30sec.5.17"  "currentclim_30sec.6.17"  "currentclim_30sec.7.17" 
[313] "currentclim_30sec.8.17"  "currentclim_30sec.9.17"  "currentclim_30sec.10.17" "currentclim_30sec.11.17"
[317] "currentclim_30sec.12.17" "currentclim_30sec.13.17" "currentclim_30sec.14.17" "currentclim_30sec.15.17"
[321] "currentclim_30sec.16.17" "currentclim_30sec.17.17" "currentclim_30sec.18.17" "currentclim_30sec.19.17"
[325] "currentclim_30sec.1.19"  "currentclim_30sec.2.18"  "currentclim_30sec.3.18"  "currentclim_30sec.4.18" 
[329] "currentclim_30sec.5.18"  "currentclim_30sec.6.18"  "currentclim_30sec.7.18"  "currentclim_30sec.8.18" 
[333] "currentclim_30sec.9.18"  "currentclim_30sec.10.18" "currentclim_30sec.11.18" "currentclim_30sec.12.18"
[337] "currentclim_30sec.13.18" "currentclim_30sec.14.18" "currentclim_30sec.15.18" "currentclim_30sec.16.18"
[341] "currentclim_30sec.17.18" "currentclim_30sec.18.18" "currentclim_30sec.19.18" "layer"

地质似乎是这里的问题...

class       : SpatRaster 
dimensions  : 848, 3084, 1  (nrow, ncol, nlyr)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -141, -115.3, 67.49167, 74.55833  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
source      : memory 
name        : geology_30sec 
min value   :             1 
max value   :            15 

或作为 rasterLayer...

class      : RasterLayer 
dimensions : 848, 3084, 2615232  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : -141, -115.3, 67.49167, 74.55833  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : geology_30sec 
values     : 1, 15  (min, max)

您可以使用 raster::stackraster::brick

library(terra)
s = rast(system.file("ex/logo.tif", package="terra"))   

library(raster)
sr = stack(s)
# or
br = brick(s)

您目前可能需要光栅的开发版本。您可以使用 install.packages('raster', repos='rspatial.r-universe.dev')

安装它