Skip to content

Commit 657e154

Browse files
Merge pull request #1177 from M3nin0/fix/base-cube-getdata
fix returned class from sits_get_data with base_cube
2 parents d26910a + 0410cb8 commit 657e154

File tree

3 files changed

+143
-8
lines changed

3 files changed

+143
-8
lines changed

R/api_data.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
tidyr::drop_na()
114114
# checking samples consistency
115115
.data_check(ts_tbl_size, nrow(ts_tbl))
116-
# add base class
117-
class(ts_tbl) <- c("sits_base", class(ts_tbl))
116+
# add base class (`sits` is added as it is removed in the join above)
117+
class(ts_tbl) <- unique(c("sits_base", "sits", class(ts_tbl)))
118118
}
119119
return(ts_tbl)
120120
}

R/api_request_httr2.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@
106106
#'
107107
#' @return A httr2 request object.
108108
#' @export
109-
.request_headers.httr2 <- function(req_obj, header) {
109+
.request_headers.httr2 <- function(req_obj, header, ...) {
110110
default_value <- list(
111111
"User-Agent" = "SITS-R-PACKAGE (github.com/e-sensing/sits)",
112112
"Accept" = "*/*",
113113
"Connection" = "keep-alive"
114114
)
115115

116-
header_values <- modifyList(
116+
header_values <- utils::modifyList(
117117
x = header,
118118
val = default_value
119119
)

tests/testthat/test-data.R

+139-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ test_that("Reading a LAT/LONG from RASTER", {
1414
expect_equal(names(point_ndvi)[1], "longitude")
1515
expect_true(ncol(.tibble_time_series(point_ndvi)) == 2)
1616
expect_true(length(sits_timeline(point_ndvi)) == 12)
17+
expect_true(
18+
all(
19+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(point_ndvi)
20+
)
21+
)
1722

1823
samples2 <- tibble::tibble(longitude = -55.66738, latitude = 11.76990)
1924
expect_warning(
@@ -49,6 +54,11 @@ test_that("Reading a CSV file from RASTER", {
4954
expect_equal(length(names(points_poly)), 7)
5055
expect_true(ncol(.tibble_time_series(points_poly)) == 2)
5156
expect_true(length(sits_timeline(points_poly)) == 12)
57+
expect_true(
58+
all(
59+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_poly)
60+
)
61+
)
5262

5363
Sys.setenv("SITS_SAMPLES_CACHE_DIR" = tempdir())
5464

@@ -63,6 +73,12 @@ test_that("Reading a CSV file from RASTER", {
6373
expect_equal(length(names(points_df)), 7)
6474
expect_true(ncol(.tibble_time_series(points_df)) == 2)
6575
expect_true(length(sits_timeline(points_df)) == 12)
76+
expect_true(
77+
all(
78+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_df)
79+
)
80+
)
81+
6682
Sys.unsetenv("SITS_SAMPLES_CACHE_DIR")
6783
})
6884

@@ -113,6 +129,11 @@ test_that("Retrieving points from BDC using POLYGON shapefiles", {
113129
object = unique(points_shp[["end_date"]]),
114130
expected = as.Date(cube_timeline[length(cube_timeline)])
115131
)
132+
expect_true(
133+
all(
134+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_shp)
135+
)
136+
)
116137

117138
# test bounding box
118139
polygons_bbox <- .bbox(sf_mt)
@@ -181,7 +202,7 @@ test_that("Retrieving points from BDC using POLYGON shapefiles", {
181202
)
182203
})
183204

184-
test_that("Retrieving points from MPC using POINT shapefiles", {
205+
test_that("Retrieving points from BDC using POINT shapefiles", {
185206
shp_file <- system.file(
186207
"extdata/shapefiles/cerrado/cerrado_forested.shp",
187208
package = "sits"
@@ -226,6 +247,12 @@ test_that("Retrieving points from MPC using POINT shapefiles", {
226247
object = unique(points_cf[["end_date"]]),
227248
expected = as.Date(cube_timeline[length(cube_timeline)])
228249
)
250+
expect_true(
251+
all(
252+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_cf)
253+
)
254+
)
255+
229256
points_bbox <- .bbox(sf_cf)
230257

231258
points_in_bbox <- dplyr::filter(
@@ -237,7 +264,7 @@ test_that("Retrieving points from MPC using POINT shapefiles", {
237264
)
238265
})
239266

240-
test_that("Retrieving points from MPC using sits tibble", {
267+
test_that("Retrieving points from BDC using sits tibble", {
241268
cube_bbox <- sits_bbox(cerrado_2classes)
242269
# create a raster cube file based on the bbox of the sits tibble
243270
modis_cube <- .try(
@@ -282,6 +309,11 @@ test_that("Retrieving points from MPC using sits tibble", {
282309
object = unique(points_tb[["end_date"]]),
283310
expected = as.Date(cube_timeline[length(cube_timeline)])
284311
)
312+
expect_true(
313+
all(
314+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_tb)
315+
)
316+
)
285317
})
286318

287319
test_that("Retrieving points from BDC using sf objects", {
@@ -329,6 +361,11 @@ test_that("Retrieving points from BDC using sf objects", {
329361
object = unique(points_cf[["end_date"]]),
330362
expected = as.Date(cube_timeline[length(cube_timeline)])
331363
)
364+
expect_true(
365+
all(
366+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_cf)
367+
)
368+
)
332369

333370
points_bbox <- .bbox(sf_cf)
334371

@@ -385,10 +422,14 @@ test_that("Retrieving points from BDC using sf objects", {
385422
object = unique(points_poly[["end_date"]]),
386423
expected = as.Date(cube_timeline[length(cube_timeline)])
387424
)
425+
expect_true(
426+
all(
427+
c("sits", "tbl_df", "tbl", "data.frame") %in% class(points_poly)
428+
)
429+
)
388430

389431
# test bounding box
390432
polygons_bbox <- .bbox(sf_mt)
391-
392433
points_poly_in_bbox <- dplyr::filter(
393434
points_poly,
394435
.data[["longitude"]] >= polygons_bbox[["xmin"]],
@@ -400,6 +441,89 @@ test_that("Retrieving points from BDC using sf objects", {
400441
expect_true(nrow(points_poly_in_bbox) == nrow(points_poly))
401442
})
402443

444+
test_that("Retrieving points from MPC Base Cube", {
445+
regdir <- paste0(tempdir(), "/base_cube_reg/")
446+
if (!dir.exists(regdir)) {
447+
suppressWarnings(dir.create(regdir))
448+
}
449+
# define roi
450+
roi <- list(
451+
lon_min = -55.69004,
452+
lon_max = -55.62223,
453+
lat_min = -11.78788,
454+
lat_max = -11.73343
455+
)
456+
# load sentinel-2 cube
457+
s2_cube <- sits_cube(
458+
source = "MPC",
459+
collection = "SENTINEL-2-L2A",
460+
start_date = "2019-01-01",
461+
end_date = "2019-01-20",
462+
bands = c("B05"),
463+
tiles = "21LXH",
464+
progress = FALSE
465+
)
466+
s2_cube <- suppressWarnings(sits_regularize(
467+
cube = s2_cube,
468+
period = "P16D",
469+
res = 320,
470+
multicores = 1,
471+
tiles = "21LXH",
472+
output_dir = regdir,
473+
progress = FALSE
474+
))
475+
# load dem cube
476+
dem_cube <- sits_cube(
477+
source = "MPC",
478+
collection = "COP-DEM-GLO-30",
479+
tiles = "21LXH"
480+
)
481+
dem_cube <- sits_regularize(
482+
cube = dem_cube,
483+
multicores = 1,
484+
res = 232,
485+
tiles = "21LXH",
486+
output_dir = regdir
487+
)
488+
# create base cube
489+
base_cube <- sits_add_base_cube(s2_cube, dem_cube)
490+
# load samples
491+
samples <- read.csv(
492+
system.file("extdata/samples/samples_sinop_crop.csv", package = "sits")
493+
)
494+
# edit samples to work with the cube (test purposes only)
495+
samples[["start_date"]] <- "2019-01-02"
496+
samples[["end_date"]] <- "2019-01-02"
497+
# extract data
498+
samples_ts <- sits_get_data(
499+
base_cube,
500+
samples = samples,
501+
crs = 32721,
502+
multicores = 1
503+
)
504+
# validations
505+
cube_timeline <- sits_timeline(base_cube)
506+
expect_equal(object = nrow(samples_ts), expected = 18)
507+
expect_equal(
508+
object = unique(samples_ts[["start_date"]]),
509+
expected = as.Date(cube_timeline[1])
510+
)
511+
expect_equal(
512+
object = unique(samples_ts[["end_date"]]),
513+
expected = as.Date(cube_timeline[length(cube_timeline)])
514+
)
515+
expect_true(
516+
all(
517+
c("sits_base", "sits", "tbl_df", "tbl", "data.frame") %in%
518+
class(samples_ts)
519+
)
520+
)
521+
522+
unlink(s2_cube[["file_info"]][[1]]$path)
523+
unlink(dem_cube[["file_info"]][[1]]$path)
524+
unlink(base_cube[["file_info"]][[1]]$path)
525+
})
526+
403527
test_that("Reading metadata from CSV file", {
404528
csv_file <- paste0(tempdir(), "/cerrado_2classes.csv")
405529
sits_to_csv(cerrado_2classes, file = csv_file)
@@ -476,7 +600,12 @@ test_that("Reading data from Classified data", {
476600
expect_equal(
477601
nrow(points_poly), nrow(read.csv(csv_raster_file))
478602
)
479-
603+
expect_true(
604+
all(
605+
c("predicted", "sits", "tbl_df", "tbl", "data.frame") %in%
606+
class(points_poly)
607+
)
608+
)
480609
expect_equal(
481610
colnames(points_poly), c(
482611
"longitude", "latitude",
@@ -545,4 +674,10 @@ test_that("Reading data from Classified data from STAC", {
545674
"label", "cube", "predicted"
546675
)
547676
)
677+
expect_true(
678+
all(
679+
c("predicted", "sits", "tbl_df", "tbl", "data.frame") %in%
680+
class(points_poly)
681+
)
682+
)
548683
})

0 commit comments

Comments
 (0)