6

我在制作程度略有不同的光栅堆栈时遇到了麻烦。这里给出的答案(第一个)很有用,但对我来说没有帮助。例如,我想为 Australia 和this Australian raster使用bio2 raster制作一个栅格堆栈。第二个栅格仅适用于澳大利亚,第一个栅格是全球性的。因此,我使用函数将全局 bio2 栅格裁剪到与澳大利亚栅格相同的范围,但生成的栅格范围(即)略有不同(因此,我无法使用裁剪栅格和澳大利亚栅格制作栅格,)。示例代码如下:crop()bio2.auawc

library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))

# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent

我也尝试过quick=TRUEstack()函数内使用。但在这种情况下,单元格值awc会丢失。注意:awc光栅的大小为 4GB。

# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc

您的建议将不胜感激。我的最终目标是将几个bioclim 栅格裁剪到与澳大利亚栅格 相同的程度awc并将它们堆叠在一起,这样栅格单元值就不会丢失。

编辑(@Cobin 评论后):

下面是每个栅格的属性

# global raster (bigger raster)
> r
class       : RasterLayer 
dimensions  : 21600, 43200, 933120000  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : D:\Worldclim2_Bioclim\wc2.0_bio_30s_02.tif 
names       : wc2.0_bio_30s_02 
values      : 0, 37.06667  (min, max)


# Australian raster (smaller raster)
> r1
class       : RasterLayer 
dimensions  : 43201, 49359, 2132358159  (nrow, ncol, ncell)
resolution  : 0.0008333333, 0.0008333333  (x, y)
extent      : 112.8921, 154.0246, -44.00042, -7.999583  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : D:\SoilAWC5cm.EV1.tif 
names       : SoilAWC5cm.EV1 
values      : 2.997789, 27.86114  (min, max)

# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class       : RasterLayer 
dimensions  : 4320, 4936, 21323520  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : 112.8917, 154.025, -44, -8  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\Anwar\AppData\Local\Temp\Rtmpmg9fyF\raster\r_tmp_2018-11-23_164300_11308_65747.grd 
names       : wc2.0_bio_30s_02 
values      : 1.933333, 18.15833  (min, max)

# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])

Error in setValues(r, vals) : 
  length(values) is not equal to ncell(x), or to 1
4

1 回答 1

6

我想两个栅格的范围是不同的,尽管栅格被crop函数掩盖了。您应该根据相同的分辨率、行和列来检查范围awc和范围。bio.au因为我无法从超链接下载数据,所以我举一个我自己的数据的例子。

r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class       : RasterLayer 
dimensions  : 74, 157, 11618  (nrow, ncol, ncell)
resolution  : 0.0833333, 0.0833333  (x, y)
extent      : 89.2185, 102.3018, 30.96238, 37.12905  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : D:\D\temp\Rtest\modis8km.tif 
names       : modis8km 
values      : -32768, 32767  (min, max)

r2
class       : RasterLayer 
dimensions  : 74, 157, 11618  (nrow, ncol, ncell)
resolution  : 0.08333333, 0.08333333  (x, y)
extent      : 89.25, 102.3333, 31, 37.16667  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : g201401a 
values      : -32768, 7789  (min, max)

虽然 r1 和 r1 具有相同的分辨率和尺寸,但范围有微小的偏移。它会导致堆栈错误。

 stack(r1,r2)
 Error in compareRaster(x) : different extent

所以,你应该重建r2 匹配r1

r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
                  nrows=dim(r1)[1],ncols=dim(r1)[2])

现在stack(r22,r1)会成功的。

于 2018-11-23T05:05:02.143 回答