I've created a list with raster filenames from a folder, then want to import those rasters into R in a "stack" using {terra}, and eventually am wanting to use {terra} to extract values from those rasters at points in a shapefile. My raster files are all uniquely named in my file folder; however, in some cases I have rasters that are named similarly (e.g., canflax2017.tif
, canflax2018.tif
, canflax2019.tif
, canflax2018_137.tif
, canflax2018_51.tif
, canflax2017_137.tif
, etc). When I get all the rasters in a "stack" it seems to cut off the year in the resulting raster name, but only for the filenames without a "_"
in the raster name. For example, canflax2017
, canflax2018
, canflax2019
all have the name canflax
and aren't unique, but canflax2018_137
, canflax2018_51
, and canflax2019_137
are fine and show the whole filename. Of course, then when I want to extract values from the stack, the columns for canflax2017
, canflax2018
, canflax2019
are just named canflax
, canflax.1
, canflax.2
etc. in the output dataframe. Any thoughts to ensure that the whole filename somehow gets incoporated into the stack information?
Code below:
library(terra)
rastsfilelist<-list.files(path="XXX/AllRasters",pattern="tif$",full.names = TRUE)
owl_stack<-rast(rastsfilelist)
owllocs <- vect('XX/shapefiletest.shp')
test<-extract(owl_stack,owllocs)
Here's the information regarding names for each raster in the stack enter image description here
Just make sure, the layer names in different rasters (files) have different names. An example below: layer names are the same regardless of file names until I change them.
l <- list.files(path = "/home/sapi/projekty/test", pattern = "above", full.names = TRUE)
l
#> [1] "/home/sapi/projekty/test/above_a.tif"
#> [2] "/home/sapi/projekty/test/above_b.tif"
#> [3] "/home/sapi/projekty/test/above_c.tif"
r <- terra::rast(l)
r
#> class : SpatRaster
#> dimensions : 25, 22, 3 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> sources : above_a.tif
#> above_b.tif
#> above_c.tif
#> names : lyr.1, lyr.1, lyr.1
#> min values : 10, 10, 10
#> max values : 10, 10, 10
w <- terra::rast("/home/sapi/projekty/test/above_a.tif")
names(w) <- "other_name"
w
#> class : SpatRaster
#> dimensions : 25, 22, 1 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : above_a.tif
#> name : other_name
#> min value : 10
#> max value : 10
terra::writeRaster(w, filename = "/home/sapi/projekty/test/above_d.tif", overwrite = TRUE)
l <- list.files(path = "/home/sapi/projekty/test", pattern = "above", full.names = TRUE)
x <- terra::rast(l)
x
#> class : SpatRaster
#> dimensions : 25, 22, 4 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> sources : above_a.tif
#> above_b.tif
#> above_c.tif
#> above_d.tif
#> names : lyr.1, lyr.1, lyr.1, other_name
#> min values : 10, 10, 10, 10
#> max values : 10, 10, 10, 10
To change names you can use setNames()
function like:
l[[1]] |>
terra::rast() |>
setNames(basename(l[[1]]))
#> class : SpatRaster
#> dimensions : 25, 22, 1 (nrow, ncol, nlyr)
#> resolution : 8.181818, 7.2 (x, y)
#> extent : 0, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : above_a.tif
#> name : above_a.tif
#> min value : 10
#> max value : 10
Created on 2024-01-22 with reprex v2.1.0