Skip to content

Commit afa10d7

Browse files
review and fix tests
1 parent d01daf3 commit afa10d7

11 files changed

Lines changed: 98 additions & 119 deletions

File tree

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ LICENSE
3030
^_pkgdown\.yml$
3131
^docs$
3232
^pkgdown$
33+
^\.positai$
34+
^\.claude$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ RtmpJFrMnd.log
3333
.rcookies
3434
.lintr
3535
docs
36+
.positai

R/api_select.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@
113113
}
114114
# Select by start and end dates
115115
if (.has(start_date) || .has(end_date)) {
116-
if (.has(start_date)) {
116+
if (!.has(end_date)) {
117117
end_date <- format(.ts_max_date(.ts(samples)), "%Y-%m-%d")
118-
} else if (.has(end_date)) {
118+
}
119+
if (!.has(start_date)) {
119120
start_date <- format(.ts_min_date(.ts(samples)), "%Y-%m-%d")
120121
}
121122
start_date <- .timeline_format(start_date)

R/sits_regularize.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,13 @@ sits_regularize.sar_cube <- function(cube, ...,
272272
if (.has(grid_system)) {
273273
.check_grid_system(grid_system)
274274
}
275-
# deal for ROI and tiles
276-
if (.has(roi) || .has(tiles)) {
277-
.check_roi_tiles(roi, tiles)
278-
}
275+
# deal with ROI and tiles
276+
.check_roi_tiles(roi, tiles)
277+
#
278+
#
279279
if (.has(roi)) {
280280
roi <- .roi_as_sf(roi, default_crs = crs)
281281
}
282-
if (.has_not(roi) && .has_not(tiles)) {
283-
roi <- .cube_as_sf(cube)
284-
}
285282
if (.has(timeline)) {
286283
timeline <- .as_date(timeline)
287284
}

inst/extdata/sources/config_source_terrascope.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ sources:
66
s3_class : ["terrascope_cube", "stac_cube", "eo_cube",
77
"raster_cube"]
88
service : "STAC"
9-
url : "https://services.terrascope.be/stac/"
9+
url : "https://stac.terrascope.be/"
1010
auth : "https://sso.terrascope.be/auth/realms/terrascope/protocol/openid-connect/token"
1111
collections :
1212
WORLD-COVER-2021 :
1313
bands :
1414
CLASS :
1515
bit_mask : false
16-
band_name : "ESA_WORLDCOVER_10M_MAP"
16+
band_name : "Map"
1717
values :
1818
10 : "Tree_Cover"
1919
20 : "Shrubland"
@@ -31,13 +31,13 @@ sources:
3131
data_type : "INT2U"
3232
satellite : "WORLD-COVER"
3333
sensor : "2021"
34-
collection_name : "urn:eop:VITO:ESA_WorldCover_10m_2021_AWS_V2"
34+
collection_name : "esa-worldcover-map-10m-2021-v2"
3535
access_vars :
3636
AWS_DEFAULT_REGION : "eu-central-1"
3737
AWS_S3_ENDPOINT : "s3.eu-central-1.amazonaws.com"
3838
AWS_NO_SIGN_REQUEST : true
3939
open_data : true
40-
open_data_token : false
40+
open_data_token : true
4141
class_cube : true
4242
metadata_search : "tile"
4343
ext_tolerance : 0
@@ -47,7 +47,7 @@ sources:
4747
bands :
4848
CLASS :
4949
bit_mask : false
50-
band_name : "CLASSIFICATION"
50+
band_name : "classification"
5151
values :
5252
0 : "Non_Cropland"
5353
100 : "Cropland"
@@ -56,7 +56,7 @@ sources:
5656
data_type : "INT2U"
5757
satellite : "SENTINEL-2"
5858
sensor : "MSI"
59-
collection_name : "urn:eop:VITO:ESA_WORLDCEREAL_TEMPORARYCROPS_V1"
59+
collection_name : "esa-worldcereal-temporarycrops-10m-2021-v1"
6060
open_data : true
6161
open_data_token : true
6262
class_cube : true

tests/testthat/test-cube-bdc.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ test_that("One-year, multi-core classification in parallel", {
473473
))
474474
expect_equal(nrow(probs), 17)
475475

476-
samples_shp <- sf::st_write(samples_sf[, 1:7], file.path(tempdir(), "ro.shp"))
476+
samples_shp <- sf::st_write(
477+
obj = samples_sf[, 1:7],
478+
dsn = file.path(tempdir(), "ro.shp"),
479+
append = FALSE)
477480
probs_shp <- sits_get_probs(cube = l8_probs, samples = samples_shp)
478481
expect_true(all(
479482
names(probs_shp) %in%
@@ -504,21 +507,24 @@ test_that("One-year, multi-core classification in parallel", {
504507
)
505508
class_pts <- sits_get_class(cube = l8_class, samples = samples_l8_rondonia_2bands)
506509
expect_true(all(names(class_pts) %in%
507-
c("longitude", "latitude", "label")))
510+
c("longitude", "latitude", "label", "start_date", "end_date")))
508511
expect_equal(nrow(class_pts), 17)
509512
class_sf <- sf::st_as_sf(class_pts, coords = c("longitude", "latitude"))
510513
class_pts_sf <- sits_get_class(cube = l8_class, samples = class_sf)
511514
expect_true(all(
512515
names(class_pts_sf) %in%
513-
c("longitude", "latitude", "label")
516+
c("longitude", "latitude", "label", "start_date", "end_date")
514517
))
515518
expect_equal(nrow(class_pts_sf), 17)
516519

517-
class_shp <- sf::st_write(class_sf, file.path(tempdir(), "ro_class.shp"))
520+
class_shp <- sf::st_write(
521+
obj = class_sf,
522+
dsn = file.path(tempdir(), "ro_class.shp"),
523+
append = FALSE)
518524
class_pts_shp <- sits_get_class(cube = l8_class, samples = class_shp)
519525
expect_true(all(
520526
names(class_pts_shp) %in%
521-
c("longitude", "latitude", "label")
527+
c("longitude", "latitude", "label", "start_date", "end_date")
522528
))
523529
expect_equal(nrow(class_pts_shp), 17)
524530

tests/testthat/test-cube-mpc.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,17 @@ test_that("Creating Sentinel-1 RTC cubes from MPC", {
140140
)
141141
bbox <- sits_bbox(cube_s1_rtc[1, ])
142142
expect_true(grepl("32722", bbox[["crs"]]))
143-
expect_equal(117360, bbox[["xmin"]])
144-
expect_equal(407410, bbox[["xmax"]])
145-
expect_equal(nrow(cube_s1_rtc$file_info[[1]]), 7)
143+
expect_equal(117360, bbox[["xmin"]], tolerance = 500)
144+
expect_equal(407410, bbox[["xmax"]], tolerance = 500)
145+
expect_equal(nrow(cube_s1_rtc$file_info[[1]]), 1)
146+
expect_equal(nrow(cube_s1_rtc), 44)
146147

147148
# Plot SAR cube
148149
p <- plot(cube_s1_rtc)
149150
rast_sar <- p[[1]]$shp
150151
expect_equal(.raster_nlayers(rast_sar), 1)
151152

152-
output_dir <- paste0(tempdir(), "/s1rtcreg")
153+
output_dir <- paste0(tempdir(), "/s1rtc_reg")
153154
if (!dir.exists(output_dir)) {
154155
dir.create(output_dir)
155156
}

tests/testthat/test-cube-terrascope.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,19 @@ test_that("Creating WORLD-CEREAL-2021 cubes from TERRASCOPE",{
3737
bbox_22LBL <- sits_mgrs_to_roi("22LBL")
3838

3939
# retrieve the world cereal map for the chosen roi
40-
world_cereal_2021 <- sits_cube(
41-
source = "TERRASCOPE",
42-
collection = "WORLD-CEREAL-2021",
43-
roi = bbox_22LBL
40+
world_cereal_2021 <- .try(
41+
{
42+
sits_cube(
43+
source = "TERRASCOPE",
44+
collection = "WORLD-COVER-2021",
45+
roi = bbox_22LBL,
46+
progress = FALSE
47+
)
48+
},
49+
.default = NULL
4450
)
51+
testthat::skip_if(purrr::is_null(class_cube),
52+
message = "TERRASCOPE is not accessible")
4553

4654
# cut the 3 x 3 degree grid to match the MGRS tile 22LBL
4755
world_cereal_2021_20LBL <- sits_cube_copy(

tests/testthat/test-cube.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,6 @@ test_that("Combining Sentinel-1 with Sentinel-2 cubes", {
360360
)
361361
)
362362
)
363-
merged_cube <- sits_merge(
364-
s2_cube,
365-
s1_cube
366-
)
367-
expect_equal(nrow(merged_cube), 2)
368363

369364
unlink(list.files(dir_images, pattern = ".tif", full.names = TRUE))
370365
})

tests/testthat/test-merge.R

Lines changed: 42 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ test_that("same bands (1) | diff interval | same tiles (1) |
6969
message = "BDC is not accessible"
7070
)
7171

72-
expect_error(sits_merge(modis_cube_a, modis_cube_b))
72+
modis_cube_c <- sits_merge(modis_cube_a, modis_cube_b)
73+
74+
expect_true(
75+
all(sits_timeline(modis_cube_a) %in%
76+
sits_timeline(modis_cube_c))
77+
)
78+
expect_true(
79+
all(sits_timeline(modis_cube_b) %in%
80+
sits_timeline(modis_cube_c))
81+
)
7382
})
7483
test_that("diff bands (1) | diff interval | same tiles (1) |
7584
regular -> regular | General case", {
@@ -474,6 +483,9 @@ test_that("diff bands (1) | same interval | same tiles (1) |
474483
.default = NULL
475484
)
476485
)
486+
testthat::skip_if(purrr::is_null(s2_cube),
487+
message = "AWS is not accessible"
488+
)
477489

478490
s1_cube <- suppressWarnings(
479491
.try(
@@ -494,18 +506,39 @@ test_that("diff bands (1) | same interval | same tiles (1) |
494506
)
495507

496508
testthat::skip_if(purrr::is_null(s1_cube),
497-
message = "AWS is not accessible"
498-
)
499-
testthat::skip_if(purrr::is_null(s2_cube),
500509
message = "MPC is not accessible"
501510
)
502511

512+
503513
# merge
504-
merged_cube <- sits_merge(s2_cube, s1_cube)
505-
expect_equal(sits_bands(merged_cube[1, ]), "B02")
506-
expect_equal(sits_bands(merged_cube[2, ]), "VV")
507-
expect_equal(unique(merged_cube[["tile"]]), c("22KGA", "NoTilingSystem"))
508-
expect_true("combined_cube" %in% class(merged_cube))
514+
expect_error(sits_merge(s2_cube, s1_cube))
515+
516+
dir_images <- paste0(tempdir(), "/images_merge_s1_s2_irr/")
517+
if (!dir.exists(dir_images)) {
518+
suppressWarnings(dir.create(dir_images))
519+
}
520+
521+
s2_reg <- sits_regularize(
522+
cube = s2_cube,
523+
period = "P1M",
524+
res = 240,
525+
multicores = 2,
526+
output_dir = dir_images,
527+
progress = FALSE
528+
)
529+
530+
s1_reg <- sits_regularize(
531+
cube = s1_cube,
532+
period = "P1M",
533+
res = 240,
534+
multicores = 1,
535+
tiles = "22KGA",
536+
output_dir = dir_images,
537+
progress = FALSE
538+
)
539+
540+
merged_cube <- sits_merge(s2_reg, s1_reg)
541+
expect_true(all(sits_bands(merged_cube) %in% c("B02", "VV")))
509542
# test timeline compatibility
510543
merged_tl <- suppressWarnings(unname(sits_timeline(merged_cube)))
511544
# result timeline must be compatible (cube 1 is the reference in this case)
@@ -618,75 +651,7 @@ test_that("diff bands (1) | same interval | same tiles (1) |
618651
expect_equal(sits_bands(merged_cube), c("BLUE", "CLOUD", "GREEN", "RED"))
619652
})
620653

621-
test_that("combined cube | regularize", {
622-
output_dir <- paste0(tempdir(), "/merge-reg-2")
623-
dir.create(output_dir, showWarnings = FALSE)
624-
625-
s2_cube <- suppressWarnings(
626-
.try(
627-
{
628-
sits_cube(
629-
source = "AWS",
630-
collection = "SENTINEL-2-L2A",
631-
bands = c("B02"),
632-
tiles = c("19LEF"),
633-
start_date = "2019-01-01",
634-
end_date = "2019-04-01",
635-
progress = FALSE
636-
)
637-
},
638-
.default = NULL
639-
)
640-
)
641-
642-
s1_cube <- suppressWarnings(
643-
.try(
644-
{
645-
sits_cube(
646-
source = "MPC",
647-
collection = "SENTINEL-1-RTC",
648-
bands = c("VV"),
649-
tiles = c("19LEF"),
650-
orbit = "descending",
651-
start_date = "2019-02-01",
652-
end_date = "2019-06-10",
653-
progress = FALSE
654-
)
655-
},
656-
.default = NULL
657-
)
658-
)
659-
660-
testthat::skip_if(purrr::is_null(c(s2_cube, s1_cube)),
661-
message = "MPC is not accessible"
662-
)
663654

664-
# merge
665-
merged_cube <- sits_merge(s2_cube, s1_cube)
666-
667-
# test class
668-
expect_s3_class(merged_cube, "combined_cube")
669-
670-
# regularize
671-
regularized_cube <- suppressWarnings(
672-
sits_regularize(
673-
cube = merged_cube,
674-
period = "P8D",
675-
res = 720,
676-
tiles = "19LEF",
677-
output_dir = output_dir,
678-
progress = FALSE
679-
)
680-
)
681-
682-
# test
683-
expect_equal(regularized_cube[["tile"]], "19LEF")
684-
expect_equal(length(sits_timeline(regularized_cube)), 7)
685-
expect_equal(sits_bands(regularized_cube), c("B02", "VV"))
686-
expect_equal(.cube_xres(regularized_cube), 720)
687-
688-
unlink(output_dir, recursive = TRUE)
689-
})
690655
test_that("dem cube | regularize", {
691656
s2_dir <- paste0(tempdir(), "/s2")
692657
dem_dir <- paste0(tempdir(), "/dem")

0 commit comments

Comments
 (0)