Skip to content

Commit

Permalink
fix already existing file detection in sits_cube_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
M3nin0 committed Nov 18, 2024
1 parent 826bf51 commit 4f47c8e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
12 changes: 11 additions & 1 deletion R/api_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
)
# Try to download
while (n_tries > 0) {
# Check if the output file already exists
if (.raster_is_valid(output_file)) {
local_asset <- .tile_from_file(
file = output_file, base_tile = asset,
band = .tile_bands(asset), update_bbox = TRUE,
labels = .tile_labels(asset)
)

return(local_asset)
}
# Update token (for big tiffs and slow networks)
asset <- .cube_token_generator(asset)
# Crop and download
Expand All @@ -50,7 +60,7 @@
output_file = output_file,
gdal_params = gdal_params
),
default = NULL
.default = NULL
)
# Check if the downloaded file is valid
if (.has(local_asset) && .raster_is_valid(output_file)) {
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-cube_copy.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,41 @@ test_that("Copy remote cube works (specific region with resampling)", {

unlink(data_dir, recursive = TRUE)
})

test_that("Copy invalid files", {
data_dir <- system.file("extdata/raster/mod13q1", package = "sits")

cube <- sits_cube(
source = "BDC",
collection = "MOD13Q1-6.1",
data_dir = data_dir,
multicores = 2,
progress = FALSE
)

# Editing cube with invalid files
# (skipping the first line to bypass the cube check and simulate a
# cube containing invalid files)
.fi(cube) <- .fi(cube) |>
dplyr::mutate(
path = ifelse(
dplyr::row_number() > 1,
paste0(path, "_invalid-file"),
path
)
)


cube_local <- sits_cube_copy(
cube = cube,
output_dir = tempdir(),
progress = FALSE
)

expect_equal(nrow(cube_local), 1)
expect_equal(length(sits_timeline(cube_local)), 1)

# Clean
files <- cube_local[["file_info"]][[1]][["path"]]
unlink(files)
})

0 comments on commit 4f47c8e

Please sign in to comment.