I have a number (21) of raster layers (19 WorldClim climate data, euroDEM digital elevation model [DEM] and Corine LandCover [CLC]) which I am trying to create a raster stack for.
I have used Align Raster processing tool in QGIS to ensure the extent, dimensions, resolution and CRS are the same before importing to R. I found that the extents where not 100% the same, so used resample() to tweak them further and they all now have the same extent, resolution etc.
The WorldClim variables I loaded all 19 files and saved as one object, before loading the DEM and CLC individually and resampling. I am now trying to create a raster stack of all 21 variables. However I cannot get it to work, and get a variety of errors:
files <- list.files(path = “Rasters/”,
pattern = “.tif$”, full.names=TRUE,recursive=TRUE)
climate <- stack(files[3:21])
avoiding the original DEM and CLC, also using raster::stack() as this creates a raster stack, whereas terra::rast() seems to create a single SpatRaster
clim_crop_ <- crop(climate, (roi_extent))
or just loading a single layer for the resampling
clim <- rast(‘Rasters/wc2.1_30s_bio_1.tif’)
clim_roi <- crop(clim, extent(roi_extent))
loading the CLC and DEM
clc <- rast(‘Rasters/final/clc_3.tif’)
dem <- rast(‘Rasters/final/dem_1.tif’)
resampling
clc_resample <- resample(clc, clim_roi, method=”near”) # categorical raster
dem_resample <- resample(dem, clim_roi, method=”bilinear”)
stacking
rast_stack <- stack(clim_crop, dem_resample, clc_resample)
if I use clim_crop or clim_roi I get the same issues – it is only stacking the first 19 (the WorldClim data).
rast_stack
class : RasterStack
dimensions : 484, 561, 271524, 19 (nrow, ncol, ncell, nlayers)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -10.66667, -5.991667, 51.41667, 55.45 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
names : wc2.1_30s_bio_1, wc2.1_30s_bio_10, wc2.1_30s_bio_11, wc2.1_30s_bio_12, wc2.1_30s_bio_13, wc2.1_30s_bio_14, wc2.1_30s_bio_15, wc2.1_30s_bio_16, wc2.1_30s_bio_17, wc2.1_30s_bio_18, wc2.1_30s_bio_19,
etc etc
alternatives i’ve tried include using a single worldclim raster:
r <- stack(clim_roi, dem_resample, clc_resample)
which returns:
Error in .local(x, …) :
unused arguments (new(“SpatRaster”, cpp = new(“Rcpp_SpatRaster”, .xData = )), new(“SpatRaster”, cpp = new(“Rcpp_SpatRaster”, .xData = )))
and:
r <- rast(clim_roi, dem_resample, clc_resample)
which returns:
Error: [rast] use ‘c()’ to combine SpatRasters
so I tried that suggestion, as using c() in terra can apparently create a raster stack and I got this:
r <- c(clim_roi, dem_resample, clc_resample)
which gives:
r
class : SpatRaster
dimensions : 484, 561, 3 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -10.66667, -5.991667, 51.41667, 55.45 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source(s) : memory
varnames : wc2.1_30s_bio_1
wc2.1_30s_bio_1
wc2.1_30s_bio_1
names : wc2.1_30s_bio_1, dem_1, clc_3
min values : 6.1375, -13, -128
max values : 11.3500, 32767, 44
seemingly only has 3 layers and is a SpatRaster, not a RasterStack (which I need the later for my analysis).
Any suggestions greatly recieved!