Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for adding a dictionary of layer-specific metadata tags? #1071

Closed
mikoontz-vp opened this issue Mar 22, 2023 · 4 comments
Closed

support for adding a dictionary of layer-specific metadata tags? #1071

mikoontz-vp opened this issue Mar 22, 2023 · 4 comments

Comments

@mikoontz-vp
Copy link

I'm following up on this StackOverflow answer (https://stackoverflow.com/questions/71367060/write-rasterstack-and-preserve-metadata-in-r) and the request to make a feature request here if this is functionality that we might find useful.

It's my understanding that python's rasterio can write arbitrary metadata to the layers of multiband raster as a dictionary when writing a GTIFF to disk. E.g., to include a "start_date" tag for each layer of a 6-band raster that each represents a new year, we might write:

# need input_raster_array and input_raster_profile
with rasterio.open(
    output_filename, "w", **(input_raster_profile)
) as dst:
    dst.write(input_raster_array)
    for index in range(0, 6):
        year = 2017 + index
        dst.update_tags(index + 1, **{'start_date': f"{year}-01-01"})

I know that we can now write band names on disk (which is great!) and I think the extra tagging functionality would also add to metadata management.

@mikoontz-vp
Copy link
Author

I've figured out a workaround to use the {reticulate} interface to Python's {rasterio} package. The following will add a tag called start_date to the first band of the raster in the local file "my_file.tif":

# reticulate::py_install("rasterio")
rasterio = reticulate::import("rasterio")
src = rasterio$open("my_file.tif", mode = "r+")

src$update_tags(1, start_date="2019-01-01")

# Tags will be written when the file is closed for writing
src$close()

# Make sure it works
print(src$tags(1))

# Use {terra} to confirm it works
terra::meta("my_file.tif", layers = TRUE)

@rhijmans
Copy link
Member

rhijmans commented Nov 14, 2023

file level tags can now be set with metags<- but I need to expand that to layer level tags.

@rhijmans rhijmans reopened this Nov 14, 2023
@mikoontz-vp
Copy link
Author

Awesome, thanks @rhijmans!

@rhijmans
Copy link
Member

I took me a while, but with e069502 it is now possible to add user defined layer specific tags and write/read them to/from GTiff. Note that "time" variables are handled automatically so that they are recognized and recovered as such.

library(terra)
#terra 1.8.2

r <- rast(ncol=5, nrow=5, nlyr=2, vals=1)
m <- cbind(c("one", "two", "three"), c("ABC", "123", "hello"))
metags(r) <- m
metags(r, layer=1) <- c(another_tag="44", `one more`="that value")
metags(r, layer=2) <- c(another_tag="55")

x <- writeRaster(r, "test.tif", overwrite=TRUE)
metags(x)
#    one   three     two 
#  "ABC" "hello"   "123" 
metags(x, 1:2)
#  layer        name      value
#1     1 another_tag         44
#2     1    one more that value
#3     2 another_tag         55
 
metags(x, 1, "one more")
#  layer     name      value
#2     1 one more that value

metags(x, 2, "one more")
#[1] layer name  value
#<0 rows> (or 0-length row.names)

Please let me know if this is not flexible enough for you (by opening a new issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants