Skip to content

Commit

Permalink
Merge pull request #128 from njtierney/raster-zip-path
Browse files Browse the repository at this point in the history
Fix for #127—don't rely on target store path to construct temp files when `preserve_metadata = "zip"`
  • Loading branch information
njtierney authored Dec 19, 2024
2 parents 36ecb15 + cf512c2 commit 35ad984
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
16 changes: 8 additions & 8 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,31 @@ tar_rast_write <- function(filetype, gdal, preserve_metadata) {
zip = function(object, path) {
# write the raster in a fresh local tempdir() that disappears when function is done
tmp <- withr::local_tempdir()
dir.create(file.path(tmp, dirname(path)), recursive = TRUE)
raster_tmp_file <- file.path(tmp, basename(path))
zip_tmp_file <- file.path(tmp, "object.zip")
terra::writeRaster(
object,
file.path(tmp, path),
filename = raster_tmp_file,
filetype = filetype,
overwrite = TRUE,
gdal = gdal
)
# package files into a zip file using `zip::zip()`
raster_files <- list.files(file.path(tmp, dirname(path)), full.names = TRUE)
raster_files <- list.files(path = tmp, full.names = TRUE)
zip::zip(
file.path(tmp, basename(path)),
zipfile = zip_tmp_file,
files = raster_files,
compression_level = 1,
mode = "cherry-pick",
root = dirname(raster_files)[1]
root = tmp
)
# move the zip file to the expected place
file.copy(file.path(tmp, basename(path)), path)
unlink(file.path(tmp, basename(path)))
file.copy(zip_tmp_file, path)
},
drop = function(object, path) {
terra::writeRaster(
object,
path,
filename = path,
filetype = filetype,
overwrite = TRUE,
gdal = gdal
Expand Down
1 change: 1 addition & 0 deletions geotargets.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 06a88cdf-966c-4990-91d1-2938c034ca80

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/_snaps/tar-terra-vect.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
source : test_terra_vect
coord. ref. : lon/lat WGS 84 (EPSG:4326)
names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
type : <num> <chr> <num> <chr> <num> <int>
values : 1 Diekirch 1 Clervaux 312 18081
1 Diekirch 2 Diekirch 218 32543
1 Diekirch 3 Redange 259 18664
names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
type : <num> <chr> <num> <chr> <num> <num>
values : 1 Diekirch 1 Clervaux 312 1.808e+04
1 Diekirch 2 Diekirch 218 3.254e+04
1 Diekirch 3 Redange 259 1.866e+04

---

Expand All @@ -26,9 +26,9 @@
extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
source : test_terra_vect_shz} (test_terra_vect_shz)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
type : <num> <chr> <num> <chr> <num> <int>
values : 1 Diekirch 1 Clervaux 312 18081
1 Diekirch 2 Diekirch 218 32543
1 Diekirch 3 Redange 259 18664
names : ID_1 NAME_1 ID_2 NAME_2 AREA POP
type : <num> <chr> <num> <chr> <num> <num>
values : 1 Diekirch 1 Clervaux 312 1.808e+04
1 Diekirch 2 Diekirch 218 3.254e+04
1 Diekirch 3 Redange 259 1.866e+04

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/tile-funs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
[[2]]
xmin xmax ymin ymax
6.133333 6.533333 49.816667 50.191667
6.141667 6.533333 49.816667 50.191667
[[3]]
xmin xmax ymin ymax
5.741667 6.141667 49.441667 49.816667
[[4]]
xmin xmax ymin ymax
6.133333 6.533333 49.441667 49.816667
6.141667 6.533333 49.441667 49.816667

0 comments on commit 35ad984

Please sign in to comment.