Skip to content

Commit f7338eb

Browse files
committed
Add test for fault tolerant sits_get_data() (Fix #1470)
1 parent 2d7f73d commit f7338eb

1 file changed

Lines changed: 80 additions & 40 deletions

File tree

tests/testthat/test-data.R

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test_that("Reading a CSV file from RASTER", {
3131
)
3232

3333
csv_raster_file <- system.file("extdata/samples/samples_sinop_crop.csv",
34-
package = "sits"
34+
package = "sits"
3535
)
3636
points_poly <- sits_get_data(
3737
raster_cube,
@@ -104,7 +104,7 @@ test_that("Retrieving points from MPC using POLYGON shapefiles", {
104104
.default = NULL
105105
)
106106
testthat::skip_if(purrr::is_null(modis_cube),
107-
message = "MPC is not accessible"
107+
message = "MPC is not accessible"
108108
)
109109
# get the timeline
110110
cube_timeline <- sits_timeline(modis_cube)
@@ -145,11 +145,11 @@ test_that("Retrieving points from MPC using POLYGON shapefiles", {
145145

146146
# retrieve labelled points from MPC cube
147147
points_shp_avg <- suppressMessages(sits_get_data(modis_cube,
148-
samples = shp_file,
149-
n_sam_pol = 5,
150-
label_attr = "NM_ESTADO",
151-
pol_avg = TRUE,
152-
progress = FALSE
148+
samples = shp_file,
149+
n_sam_pol = 5,
150+
label_attr = "NM_ESTADO",
151+
pol_avg = TRUE,
152+
progress = FALSE
153153
))
154154

155155
expect_equal(object = nrow(points_shp_avg), expected = 1)
@@ -159,10 +159,10 @@ test_that("Retrieving points from MPC using POLYGON shapefiles", {
159159
)
160160
# retrieve points from MPC cube with no label
161161
points_shp_no_label <- suppressMessages(sits_get_data(modis_cube,
162-
samples = shp_file,
163-
n_sam_pol = 5,
164-
pol_avg = TRUE,
165-
progress = FALSE
162+
samples = shp_file,
163+
n_sam_pol = 5,
164+
pol_avg = TRUE,
165+
progress = FALSE
166166
))
167167

168168
expect_equal(object = nrow(points_shp_no_label), expected = 1)
@@ -173,9 +173,9 @@ test_that("Retrieving points from MPC using POLYGON shapefiles", {
173173
# test for errors in get_data syntax
174174
expect_error(
175175
sits_get_data(raster_cube,
176-
samples = temp_shp,
177-
label_attr = "labelddddsssaaa",
178-
progress = FALSE
176+
samples = temp_shp,
177+
label_attr = "labelddddsssaaa",
178+
progress = FALSE
179179
)
180180
)
181181
})
@@ -206,14 +206,14 @@ test_that("Retrieving points from MPC using POINT shapefiles", {
206206
.default = NULL
207207
)
208208
testthat::skip_if(purrr::is_null(modis_cube),
209-
message = "MPC is not accessible"
209+
message = "MPC is not accessible"
210210
)
211211
tf <- paste0(tempdir(), "/cerrado_forested.shp")
212212
sf::st_write(sf_cf[1:5, ], dsn = tf, quiet = TRUE, append = FALSE)
213213
points_cf <- suppressMessages(sits_get_data(modis_cube,
214-
samples = tf,
215-
label = "Woodland",
216-
progress = FALSE
214+
samples = tf,
215+
label = "Woodland",
216+
progress = FALSE
217217
))
218218
cube_timeline <- sits_timeline(modis_cube)
219219
expect_equal(object = nrow(points_cf), expected = 5)
@@ -260,7 +260,7 @@ test_that("Retrieving points from BDC using sits tibble", {
260260
.default = NULL
261261
)
262262
testthat::skip_if(purrr::is_null(modis_cube),
263-
message = "BDC is not accessible"
263+
message = "BDC is not accessible"
264264
)
265265
# create a sits_tibble to retrieve the data
266266
# first select unique locations
@@ -274,8 +274,8 @@ test_that("Retrieving points from BDC using sits tibble", {
274274
input_tb$start_date <- as.Date("2018-08-22")
275275
input_tb$end_date <- as.Date("2019-08-30")
276276
points_tb <- suppressMessages(sits_get_data(modis_cube,
277-
samples = input_tb,
278-
progress = FALSE
277+
samples = input_tb,
278+
progress = FALSE
279279
))
280280
cube_timeline <- sits_timeline(modis_cube)
281281
expect_equal(object = nrow(points_tb), expected = 5)
@@ -321,12 +321,12 @@ test_that("Retrieving points from MPC using sf objects", {
321321
)
322322

323323
testthat::skip_if(purrr::is_null(modis_cube),
324-
message = "MPC is not accessible"
324+
message = "MPC is not accessible"
325325
)
326326
points_cf <- suppressMessages(sits_get_data(modis_cube,
327-
samples = sf_cf[1:5, ],
328-
label = "Woodland",
329-
progress = FALSE
327+
samples = sf_cf[1:5, ],
328+
label = "Woodland",
329+
progress = FALSE
330330
))
331331

332332
cube_timeline <- sits_timeline(modis_cube)
@@ -381,13 +381,13 @@ test_that("Retrieving points from MPC using sf objects", {
381381
)
382382

383383
testthat::skip_if(purrr::is_null(modis_cube),
384-
message = "MPC is not accessible"
384+
message = "MPC is not accessible"
385385
)
386386
# obtain a set of points based on an SF POLYGOn geometry
387387
points_poly <- suppressMessages(sits_get_data(modis_cube,
388-
samples = sf_mt,
389-
n_sam_pol = 5,
390-
progress = FALSE
388+
samples = sf_mt,
389+
n_sam_pol = 5,
390+
progress = FALSE
391391
))
392392

393393
cube_timeline <- sits_timeline(modis_cube)
@@ -564,8 +564,8 @@ test_that("Reading data from Classified data", {
564564
)
565565
# smooth the probability cube using Bayesian statistics
566566
bayes_cube <- sits_smooth(probs_cube,
567-
output_dir = output_dir,
568-
progress = FALSE
567+
output_dir = output_dir,
568+
progress = FALSE
569569
)
570570
# label the probability cube
571571
label_cube <- sits_label_classification(
@@ -576,10 +576,10 @@ test_that("Reading data from Classified data", {
576576

577577
# Using CSV
578578
csv_raster_file <- system.file("extdata/samples/samples_sinop_crop.csv",
579-
package = "sits"
579+
package = "sits"
580580
)
581581
points_poly <- sits_get_class(label_cube,
582-
samples = csv_raster_file
582+
samples = csv_raster_file
583583
)
584584
expect_equal(
585585
nrow(points_poly), nrow(read.csv(csv_raster_file))
@@ -633,19 +633,19 @@ test_that("Reading data from Classified data from STAC", {
633633
)
634634

635635
testthat::skip_if(purrr::is_null(class_cube),
636-
message = "TERRASCOPE is not accessible"
636+
message = "TERRASCOPE is not accessible"
637637
)
638638

639639
# adapt date to work with the sinop samples
640640
class_cube[["file_info"]][[1]][["start_date"]] <- "2013-10-01"
641641
class_cube[["file_info"]][[1]][["end_date"]] <- "2013-10-01"
642642
# Using CSV
643643
csv_raster_file <- system.file("extdata/samples/samples_sinop_crop.csv",
644-
package = "sits"
644+
package = "sits"
645645
)
646646
points_poly <- suppressWarnings(
647647
sits_get_class(class_cube,
648-
samples = csv_raster_file
648+
samples = csv_raster_file
649649
)
650650
)
651651
expect_equal(nrow(points_poly), 18)
@@ -665,17 +665,57 @@ test_that("Reading data from Classified data from STAC", {
665665

666666
test_that("Impute points", {
667667
# Define samples
668-
samples <- samples_modis_ndvi[1:3,]
668+
samples <- samples_modis_ndvi[1:3, ]
669669
# Add NA values
670-
samples[1,][["time_series"]][[1]][["NDVI"]][1] <- NA
671-
samples[1,][["time_series"]][[1]][["NDVI"]][5] <- NA
670+
samples[1, ][["time_series"]][[1]][["NDVI"]][1] <- NA
671+
samples[1, ][["time_series"]][[1]][["NDVI"]][5] <- NA
672672
# Impute
673673
samples_impute <- suppressWarnings(sits_impute(samples))
674674
# Check result
675-
expect_true(!all(is.na(samples_impute[1,][["time_series"]][[1]][["NDVI"]])))
675+
expect_true(!all(is.na(samples_impute[1, ][["time_series"]][[1]][["NDVI"]])))
676676
# Check deprecation warning
677677
doc_mode <- Sys.getenv("SITS_DOCUMENTATION_MODE")
678678
Sys.setenv("SITS_DOCUMENTATION_MODE" = "FALSE")
679679
expect_warning(sits_impute(samples))
680680
Sys.setenv("SITS_DOCUMENTATION_MODE" = doc_mode)
681681
})
682+
683+
test_that("sits_get_data is fault tolerant for corrupted raster files", {
684+
src_dir <- system.file("extdata/raster/mod13q1", package = "sits")
685+
tmp_dir <- file.path(tempdir(), paste0("mod13q1_corrupt_"))
686+
unlink(tmp_dir, recursive = TRUE, force = TRUE, expand = TRUE)
687+
dir.create(tmp_dir, recursive = TRUE, showWarnings = FALSE)
688+
689+
files <- list.files(src_dir, full.names = TRUE)
690+
ok <- file.copy(files, tmp_dir, overwrite = TRUE)
691+
expect_true(all(ok))
692+
693+
raster_cube <- sits_cube(
694+
source = "BDC",
695+
collection = "MOD13Q1-6.1",
696+
data_dir = tmp_dir,
697+
progress = FALSE
698+
)
699+
700+
corrupted_file <- list.files(
701+
tmp_dir,
702+
pattern = "2014-05-25\\.jp2$",
703+
full.names = TRUE
704+
)
705+
expect_length(corrupted_file, 1)
706+
writeBin(as.raw(c(0x00, 0x01, 0x02, 0x03)), corrupted_file)
707+
708+
samples <- tibble::tibble(longitude = -55.66738, latitude = -11.76990)
709+
710+
expect_error(
711+
sits_get_data(
712+
raster_cube,
713+
samples,
714+
multicores = 2,
715+
progress = FALSE
716+
),
717+
regexp = "unable to extract time series from tile 012010, band NDVI"
718+
)
719+
720+
unlink(tmp_dir, recursive = TRUE, force = TRUE, expand = TRUE)
721+
})

0 commit comments

Comments
 (0)