为什么在从 SpatRaster 转换为 rasterStack 时我的栅格图层的数据会发生变化?

Why do my raster layers' data change when converting from SpatRaster to rasterStack?

我使用 terra 中的 rast() 函数导入了一个多层 tiff 文件(7 个“bio”层),然后使用以下方法从其他地方添加了其他层:

current.raster <- rast("./bio_layers.tif")

# add1, add2, and add3 are SpatRaster objects from tif files that were modified 
# in earlier steps and are stored in memory
new.rast <- c(current.raster, add1, add2, add3) 

new.rast
class       : SpatRaster 
dimensions  : 848, 3084, 10  (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) 
sources     : bio_layers.tif  (7 layers) 
              memory  
              memory  
              ... and 1 more source(s)

当我使用 stack() 将其转换为 rasterStack 对象时,“add”层的值更改为 current.raster 第一层的值。请参阅下面我的栅格摘要。

summary(new.rast)
      bio1             bio2            bio3             bio4           bio11            bio15           bio18       
 Min.   :-16.60   Min.   :1.00    Min.   : 12.29   Min.   : 0.00   Min.   :-32.15   Min.   :34.31   Min.   :  9.00  
 1st Qu.:-14.45   1st Qu.:6.95    1st Qu.: 15.29   1st Qu.:14.41   1st Qu.:-30.52   1st Qu.:55.30   1st Qu.: 53.00  
 Median :-11.74   Median :7.42    Median : 15.90   Median :14.67   Median :-27.87   Median :59.26   Median : 63.00  
 Mean   :-12.07   Mean   :7.53    Mean   : 16.19   Mean   :14.75   Mean   :-28.23   Mean   :59.21   Mean   : 70.24  
 3rd Qu.: -9.79   3rd Qu.:8.02    3rd Qu.: 16.79   3rd Qu.:15.27   3rd Qu.:-26.35   3rd Qu.:63.18   3rd Qu.: 83.00  
 Max.   :  0.00   Max.   :9.56    Max.   :100.00   Max.   :16.37   Max.   :  0.00   Max.   :91.12   Max.   :153.00  
 NA's   :78844    NA's   :78844   NA's   :78844    NA's   :78844   NA's   :78844    NA's   :78844   NA's   :78844   
      add1              add2            add3       
 Min.   : -15.42   Min.   : 0.00   Min.   :0.00   
 1st Qu.:  38.41   1st Qu.: 0.71   1st Qu.:0.00   
 Median : 114.79   Median : 1.74   Median :0.00   
 Mean   : 166.82   Mean   : 3.45   Mean   :0.00   
 3rd Qu.: 222.14   3rd Qu.: 3.84   3rd Qu.:0.00   
 Max.   :1584.36   Max.   :49.38   Max.   :0.02   
 NA's   :79057     NA's   :79326   NA's   :81712  

summary(stack(new.rast))
                 bio1         bio2         bio3         bio4         bio11        bio15   bio18          add1
Min.        -16.60000 1.000000e+00 1.253205e+01 0.000000e+00     -32.15000 3.511098e+01      10     -16.60000
1st Qu.     -14.45833 6.950000e+00 1.528698e+01 1.441101e+01     -30.51667 5.537055e+01      53     -14.45833
Median      -11.77917 7.425000e+00 1.589191e+01 1.467350e+01     -27.88333 5.927956e+01      63     -11.77917
3rd Qu.      -9.78750 8.016666e+00 1.679012e+01 1.526218e+01     -26.35000 6.308383e+01      83      -9.78750
Max.          0.00000 9.575000e+00 1.000000e+02 1.638678e+01       0.00000 8.897108e+01     148       0.00000
NA's    2046158.00000 2.046158e+06 2.046158e+06 2.046158e+06 2046158.00000 2.046158e+06 2046158 2046158.00000
                 add2          add3
Min.        -16.60000     -16.60000
1st Qu.     -14.45833     -14.45833
Median      -11.77917     -11.77917
3rd Qu.      -9.78750      -9.78750
Max.          0.00000       0.00000
NA's    2046158.00000 2046158.00000

请注意“add"层数据如何更改以复制“bio1”层。为什么会发生这种情况?有没有办法将这些层的数据保存在内存中作为我将它转换为 rasterStack 对象?

我已经解决了这个问题,现在有了开发版或光栅我得到:

library(raster)
library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))   
r1 <- s[[1]] * 2
r2 <- s[[2]] * 3

x <- c(s, r1, r2)
names(x) <- letters[1:5]
rst <- stack(x)

summary(x)
#       a               b               c               d               e        
# Min.   :  0.0   Min.   :  0.0   Min.   :  0.0   Min.   :  0.0   Min.   :  0.0  
# 1st Qu.:131.0   1st Qu.:138.0   1st Qu.:151.0   1st Qu.:262.0   1st Qu.:414.0  
# Median :196.0   Median :199.0   Median :215.0   Median :392.0   Median :597.0  
# Mean   :182.3   Mean   :185.4   Mean   :192.8   Mean   :364.6   Mean   :556.1  
# 3rd Qu.:254.0   3rd Qu.:255.0   3rd Qu.:254.0   3rd Qu.:508.0   3rd Qu.:765.0  
# Max.   :255.0   Max.   :255.0   Max.   :255.0   Max.   :510.0   Max.   :765.0  

summary(rst)
#          a   b   c   d   e
#Min.      0   0   0   0   0
#1st Qu. 131 138 151 262 414
#Median  196 199 215 392 597
#3rd Qu. 254 255 254 508 765
#Max.    255 255 255 510 765
#NA's      0   0   0   0   0

您可以安装开发版本 install.packages('raster', repos='https://rspatial.r-universe.dev')