0

I'm using raster::calc to make some calculations and write the output to GeoTiff in just one go. But I'd like to set the layer name of the output before writing to disk. I'm trying to do this by using setNames inside the function but it's now working. How can I change layer name inside a raster::calc call that saves the file to disk?

Here is a simple reprex

library(raster)
#> Loading required package: sp

r <- raster(ncols=36, nrows=18)
r[] <- 1:ncell(r)
names(r) <- 'my_name_is'

r2 <- calc(r, function(x) {
    y <- x * 10
    y <- setNames(y, 'my_name_is')
})

names(r2)
#> [1] "layer"

Created on 2020-05-07 by the reprex package (v0.3.0)

1
  • Great! Thanks for the info. I was getting confused because when I opened the created TIFF in R, it used the filename as the layer name. If you post the comment as an answer, I'd mark it as accepted
    – Daniel
    Commented May 8, 2020 at 13:18

1 Answer 1

0

This is not necessary. The only place the name will be retained is in the raster object, which is a pointer to the on disk raster. If you close your session and read the raster into a new session, the names will not be there. The tiff format does not store layer names like a format such as img. If you just want to write a tiff with a specific name you can use the filename argument in calc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.