-
Notifications
You must be signed in to change notification settings - Fork 4
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
longnames and varnames aren't overwritten #141
Comments
I think this is part of the known issue in |
This is something I have looked at a little bit-- I believe these labels are intended for specific file types such as netCDF, HDF5 but their handling is not consistent in terra. Note that even library(terra)
#> terra 1.8.11
x <- rast(system.file("ex", "elev.tif", package="terra"))
varnames(x) <- "new-varnames"
longnames(x) <- "really-long-new-name"
varnames(x)
#> [1] "new-varnames"
longnames(x)
#> [1] "really-long-new-name"
y <- unwrap(wrap(x))
varnames(y)
#> [1] ""
longnames(y)
#> [1] "" I am working on some PRs for terra to improve metadata writing/round-tripping for more drivers (rspatial/terra#1696 (comment)) I think that varnames and longnames could be stored in metadata tags or "user tags" for most drivers--either internally within the GDAL data file itself (as units and time are for GeoTIFF), or in a PAM (.aux.xml) file. If I get my changes implemented I will keep var/longnames on the radar and will try to make it so these work the same as units, time and custom user tags. Thinking about the concept of GDAL subdatasets with drivers other than GTiff and SpatRasterCollection, I have a couple other ideas to perhaps improve |
Thanks for chiming in @brownag. I'll close this since there's nothing to do about it in |
I think we can leave it open as something to fix once this gets resolved in terra |
Hopefully there will be nothing for us to do if these metadata end up getting written with the same mechanisms as layer names or units. |
https://search.r-project.org/CRAN/refmans/terra/html/varnames.html I was reading the documentation for varnames and longnames--and it seems there is some discussion of this issue:
It seems "expected" that these names are only read and not written, and are targeted to specific data source types.. However I do think it is a bit confusing that there are exported setters varnames<- and longnames<- if common ops like |
Could some of these attributes (longnames, varnames, time, etc.) that are not preserved when written be written to R objects within the zip file in tar_rast_write <- function(filetype, gdal, preserve_metadata) {
switch(preserve_metadata,
zip = function(object, path) {
# write the raster in a fresh local tempdir() that disappears when
# function is done
tmp <- withr::local_tempdir()
raster_tmp_file <- file.path(tmp, basename(path))
attr_tmp_file <- file.path(tmp, "attrs.RDS")
rast_attrs <- list(
time = terra::time(object),
longnames = terra::longnames(object),
varnames = terra::varnames(object)
)
zip_tmp_file <- file.path(tmp, "object.zip")
terra::writeRaster(
object,
filename = raster_tmp_file,
filetype = filetype,
overwrite = TRUE,
gdal = gdal
)
saveRDS(rast_attrs, attr_tmp_file, compress = FALSE)
# package files into a zip file using `zip::zip()`
raster_files <- list.files(path = tmp, full.names = TRUE)
zip::zip(
zipfile = zip_tmp_file,
files = raster_files,
compression_level = 1,
mode = "cherry-pick",
root = tmp
)
# move the zip file to the expected place
file.copy(zip_tmp_file, path)
}
# ...
)
}
tar_rast_read <- function(preserve_metadata) {
switch(preserve_metadata,
zip = function(path) {
tmp <- tempdir()
# NOTE: cannot use withr::local_tempdir() because the unzipped files need
# to persist so that the resulting `SpatRaster` object doesn't have a
# broken file pointer
zip::unzip(zipfile = path, exdir = tmp)
rast_attrs <- readRDS("attrs.RDS")
rast_out <- terra::rast(file.path(tmp, basename(path)))
terra::longnames(rast_out) <- rast_attrs[["longnames"]]
terra::varnames(rast_out) <- rast_attrs[["varnames"]]
terra::time(rast_out) <- rast_attrs[["time"]]
return(rast_out)
},
drop = function(path) terra::rast(path)
)
} If this interests you at all, I am happy to put together a PR. |
I'm still not sure we should be trying to do anything that |
I requested this be fixed for Likewise, I am not sure we should be coming up with our own approach for this. The ability to store these attributes is driver-specific. Now in terra metadata are handled by the GDAL driver (after rspatial/terra#1714), so no terra-specific JSON metadata files anymore. Either attributes are stored internal to the file, or they are put in a Persistent Auxiliary Metadata file (PAM). Some GDAL drivers support user-defined metadata tags--see It might be reasonable to request storing varnames and longnames in user tags as a feature of terra, but I have not had a chance to investigate all the details of the current implementation and how it would interact with e.g. HDF and netCDF drivers that these attributes were originally intended for. |
The unfortunate thing about this being supported by |
Hold up—I just read rspatial/terra#1714. Does this mean if we require this version of |
This, I think, is an argument for using something like I am not sure it is a good general assumption that all data required to reproduce an R object can be stored in a regular spatial format--even with (auxiliary) metadata support--without also making some efforts to serialize the actual (Packed)SpatRaster/Vector object that was created.
Depends on the driver and version of GDAL. For GeoTIFF at least it all can be stored internally, as well as for several other drivers. |
See
Created on 2025-01-16 with reprex v2.1.1
Session info
The text was updated successfully, but these errors were encountered: