Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set raster metadata (band names) in R on disk

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.

like image 989
TheRealJimShady Avatar asked Jan 29 '26 17:01

TheRealJimShady


1 Answers

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"
like image 158
Robert Hijmans Avatar answered Feb 01 '26 09:02

Robert Hijmans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!