Skip to content

Commit

Permalink
Merge pull request #131 from njtierney/zip-tempdir
Browse files Browse the repository at this point in the history
Fix missing data when `preserve_metadata = "zip"`
  • Loading branch information
Aariq authored Jan 7, 2025
2 parents fe0ba8a + f6c59a8 commit 044536b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ tar_terra_rast <- function(name,
tar_rast_read <- function(preserve_metadata) {
switch(preserve_metadata,
zip = function(path) {
tmp <- withr::local_tempdir()
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)
terra::rast(file.path(tmp, basename(path)))
},
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ tar_test("metadata is maintained", {
r
}
list(
tar_terra_rast(r, make_rast())
tar_terra_rast(r, make_rast()),
tar_terra_rast(r2, make_rast(), preserve_metadata = "drop")
)
})
tar_make()
x <- tar_read(r)
expect_equal(terra::units(x), rep("m", 3))
expect_equal(terra::time(x), as.Date("2024-10-01") + c(0, 1, 2))
expect_equal(head(terra::values(x)), head(terra::values(tar_read(r2))))
})

0 comments on commit 044536b

Please sign in to comment.