Tin*_*per 6 stack r raster spatial extent
我在制作范围略有不同的光栅堆栈时遇到了麻烦。这里给出的答案(第一个)很有用,但对我的情况没有帮助。例如,我想使用bio2 raster for Australia 和this Australian raster制作光栅堆栈。第二个栅格仅适用于澳大利亚,第一个栅格是全球栅格。因此,我使用crop()function将 global bio2 栅格裁剪为与澳大利亚栅格相同的范围,但生成的栅格范围(即bio2.au)略有不同(因此,我无法使用裁剪后的栅格和澳大利亚栅格制作栅格,awc)。示例代码如下:
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
Run Code Online (Sandbox Code Playgroud)
我也试过quick=TRUE在stack()函数内使用。但在这种情况下,单元格值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
Run Code Online (Sandbox Code Playgroud)
您的建议将不胜感激。我的最终目标是将多个生物气候栅格裁剪到与澳大利亚栅格 相同的范围awc并将它们堆叠在一起,以便栅格单元值不会丢失。
下面是每个栅格的属性
# 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
Run Code Online (Sandbox Code Playgroud)
我想两个栅格的范围是不同的,虽然被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)
Run Code Online (Sandbox Code Playgroud)
虽然r1和r1具有相同的分辨率和尺寸,但范围有微小的偏移。它导致堆栈错误。
stack(r1,r2)
Error in compareRaster(x) : different extent
Run Code Online (Sandbox Code Playgroud)
因此,您应该重新构建r2 以匹配r1:
r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])
Run Code Online (Sandbox Code Playgroud)
现在stack(r22,r1)会成功。