I have a large raster on file. It can be read like so:
my_rast <- rast("my_large_file.tif")
names(my_rast)
[1] "value_a"
Using R or command-line GDAL I would like to change the name of the layer to something different, without totally re-writing the whole raster. How can I do this?
Note: I see there is a command in the Python rasterio library to do this, but I don't see a similar one in R.
You can now use the update(x, names=TRUE)
to write the changed layer names to disk (for file formats that support layer names, such as GTiff).
Example data
library(terra)
#terra 1.6.29
s <- rast(system.file("ex/logo.tif", package="terra"))
names(s)
#[1] "red" "green" "blue"
fname <- paste0(tempfile(), ".tif")
x <- writeRaster(s, fname)
Change the names, and write them to the file.
names(x) <- c("A", "B", "C")
update(x, names=TRUE)
Show that it worked
r <- rast(fname)
names(r)
#[1] "A" "B" "C"