I am working with tif file rasters from the climateNA website (https://adaptwest.databasin.org/pages/adaptwest-climatena/), each file is about 18 to 40MB. So I cannot upload the files here, and downloading the whole set would take a good chuck of time (so not asking you to that yet!). So not sure how to make my issue more reproducible. But I will try to explain as best as I can.
When loading a raster file (each raster contains only 1 layer) with function rast()
from terra
I get the following:
class : SpatRaster
dimensions : 7617, 7688, 1 (nrow, ncol, nlyr)
resolution : 1000, 1000 (x, y)
extent : -4352000, 3336000, -3341000, 4276000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
source : Normal_1991_2020_bFFP.tif
name : bigfile[, varname]
min value : 0
max value : 292
Notice how the source is correct but the name has changed to "bigfile[, varname]".The source file may come with that layer name.
I can rename the layer with terra::names()
. I haven't really attempted to manually assign the name within the function with the name =
parameter. But this would be for only 1 file. And I need to work with several in an automated way.
So, my main issues is that I am actually using a vector of rasters and reading them within rast()
like this:
# produces a vector of name from the previously defined folder containing all the raster tif files
Normrast<- list.files(path = clim.folder, pattern='.tif$', all.files=TRUE, full.names=TRUE)
# Get and stack Climate rasters from folder above
NormStack<-terra::rast(Normrast)`
Which produces the following object:
> NormStack
class : SpatRaster
dimensions : 7617, 7688, 12 (nrow, ncol, nlyr)
resolution : 1000, 1000 (x, y)
extent : -4352000, 3336000, -3341000, 4276000 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
sources : Normal_1991_2020_CMD.tif
Normal_1991_2020_EMT.tif
Normal_1991_2020_FFP.tif
... and 9 more source(s)
names : bigfi~name], bigfi~name], bigfi~name], bigfi~name], bigfi~name], bigfi~name], ...
min values : 0, -67.9, 0, -27.3, -45.1, -14.7, ...
max values : 2185, 29.2, 365, 30.4, 28.4, 38.8, ...
If I attempt to change the names with names()
it seems to work by assigning a vector of names using list.files()
from the folder all the files are in.
I worry about being sure the assignment using names()
is working right, with more files making the cross checking more complicated than when using about 10 raster files or so. Maybe the order of the files is sometimes not respected (Which so far, it seems like it). I am actually migrating some code from raster to terra so I would like to come up with a good workflow for this.
Thanks for any advice you have! Cheers!
EDIT: modified for accuracy after comments from Robert Hijmans
You have a vector (not a "list") of filenames and make a SpatRaster.
ff <- list.files(pattern='.tif$', full.names=TRUE)
x <- terra::rast(ff)
If you do not like the layer names of x
, you can change them. For example like this
names(x) <- gsub(".tif$", basename(ff))
In that way, you are sure that the names match the layers correctly.
You are getting bigfile[, varname]
because this is what is specified in the files you are using. If you want that fixed you need to ask the creator of the files. This is clearly a bug in their processing.