njtierney / geotargets

Targets extensions for geospatial data
https://njtierney.github.io/geotargets/
Other
49 stars 4 forks source link

`tar_terra_rast()` doesn't save units or varnames #58

Open Aariq opened 2 months ago

Aariq commented 2 months ago

I suspect this is because those metadata are stored in a "sidecar file" when filetype = "GeoTIFF", but I haven't investigated further.

library(targets)
tar_script({
    make_rast <- function() {
        x <- terra::rast(system.file("ex/elev.tif", package = "terra"))
        terra::units(x) <- "m"
        terra::varnames(x) <- "elev"
        x
    }
    list(
        geotargets::tar_terra_rast(
            rast,
            make_rast()
        )
    )
})
tar_make()
#> ▶ dispatched target rast
#> ● completed target rast [0.017 seconds]
#> ▶ ended pipeline [0.148 seconds]
tar_load(rast)
terra::units(rast)
#> [1] ""
terra::varnames(rast)
#> [1] "rast"
terra::names(rast)
#> [1] "elevation"

Created on 2024-04-25 with reprex v2.1.0

Aariq commented 2 months ago

Yeah, looks like units are store in an aux.json file. varnames() aren't read back in by rast() so I guess that one isn't our problem. Related to #37

Aariq commented 2 months ago

I've tried a half dozen or so differente GDAL drivers and they all create an aux.json file when a SpatRaster has units. Ideas: 1) always zip files after writing and use vsizip to read back in (cons to this approach in comments on #37) 2) have tar_terra_rast(my_map) create two targets: my_map_rast and my_map_meta. Not sure how reading this back in would work though 3) don't support units???

Aariq commented 2 months ago

Found this: https://github.com/rspatial/terra/blob/master/NEWS.md#new-18 aux.json file is written for all drivers except when using writeCDF()

njtierney commented 2 months ago

Thanks for this! I'll take a look later this week on my geotargets day.