Skip to content

Commit 9702634

Browse files
Merge pull request #1488 from OldLipe/feat/reclassify-tile
Restrict support to irregular cubes in `sits_merge` and update world cover STAC
2 parents f8f6ca1 + 696938e commit 9702634

9 files changed

Lines changed: 57 additions & 490 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ Config/testthat/start-first: cube, raster, regularize, data, ml
112112
LinkingTo:
113113
Rcpp,
114114
RcppArmadillo
115-
RoxygenNote: 7.3.3
116115
Collate:
117116
'api_accessors.R'
118117
'api_accuracy.R'
@@ -296,3 +295,4 @@ Collate:
296295
'sits_variance.R'
297296
'sits_xlsx.R'
298297
'zzz.R'
298+
Config/roxygen2/version: 8.0.0

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ S3method(sits_reclassify,probs_cube)
469469
S3method(sits_reclassify,probs_vector_cube)
470470
S3method(sits_reduce,raster_cube)
471471
S3method(sits_reduce,sits)
472-
S3method(sits_regularize,combined_cube)
473472
S3method(sits_regularize,default)
474473
S3method(sits_regularize,dem_cube)
475474
S3method(sits_regularize,derived_cube)

R/api_merge.R

Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -174,53 +174,22 @@
174174
.merge_cube_compactify <- function(data1, data2) {
175175
# extract tiles
176176
tiles <- .merge_get_common_tiles(data1, data2)
177-
if (.has(tiles)) {
178-
# align timeline tile by tile.
179-
merged_cube <- .map_dfr(tiles, function(tile) {
180-
# get tiles
181-
tile1 <- .cube_filter_tiles(data1, tile)
182-
tile2 <- .cube_filter_tiles(data2, tile)
183-
# get tile timelines
184-
ts1 <- .tile_timeline(tile1)
185-
ts2 <- .tile_timeline(tile2)
186-
# adjust timeline using zipper strategy
187-
ts_overlap <- .merge_zipper_strategy(ts1, ts2)
188-
# filter cubes in the overlapping dates
189-
tile1 <- .cube_filter_dates(tile1, ts_overlap)
190-
tile2 <- .cube_filter_dates(tile2, ts_overlap)
191-
# merge by file
192-
.merge_strategy_file(tile1, tile2)
193-
})
194-
} else {
195-
# It is not possible to merge non-common tiles with multiple bands using
196-
# the same sensor
197-
.check_that(
198-
.cube_sensor(data1) != .cube_sensor(data2),
199-
msg = .conf("messages", ".merge_irregular_bands")
200-
)
201-
# if no common tiles are available, use a global reference timeline.
202-
# in this case, this timeline is generated by the merge of all timelines
203-
# in the reference cube (cube 1)
204-
reference_timeline <- as.Date(unlist(.cube_timeline(data1)))
205-
# based on the global timeline, cut the timeline of all tiles in cube 2
206-
merged_cube <- .cube_foreach_tile(data2, function(row) {
207-
# get row timeline
208-
row_timeline <- .tile_timeline(row)
209-
# search overlaps between the reference timeline and row timeline
210-
t_overlap <- .merge_zipper_strategy(
211-
t1 = reference_timeline,
212-
t2 = row_timeline
213-
)
214-
# cut the timeline
215-
.cube_filter_dates(row, t_overlap)
216-
})
217-
# as there is no tile reference, merge using `bind` strategy (cube row)
218-
merged_cube <- .merge_strategy_bind(data1, merged_cube)
219-
# assign `combined cube` class, meaning the cube is a combination of
220-
# cubes that contains different timelines in different tiles
221-
class(merged_cube) <- c("combined_cube", class(data1))
222-
merged_cube
223-
}
177+
# align timeline tile by tile.
178+
merged_cube <- .map_dfr(tiles, function(tile) {
179+
# get tiles
180+
tile1 <- .cube_filter_tiles(data1, tile)
181+
tile2 <- .cube_filter_tiles(data2, tile)
182+
# get tile timelines
183+
ts1 <- .tile_timeline(tile1)
184+
ts2 <- .tile_timeline(tile2)
185+
# adjust timeline using zipper strategy
186+
ts_overlap <- .merge_zipper_strategy(ts1, ts2)
187+
# filter cubes in the overlapping dates
188+
tile1 <- .cube_filter_dates(tile1, ts_overlap)
189+
tile2 <- .cube_filter_dates(tile2, ts_overlap)
190+
# merge by file
191+
.merge_strategy_file(tile1, tile2)
192+
})
224193
}
225194
#' @title Define merge strategy based on intersecting the timeline
226195
#' @name .merge_strategy_intersects
@@ -396,28 +365,6 @@
396365
# Return merged cube
397366
merged_cube
398367
}
399-
#' @title Merge strategy for irregular cubes
400-
#' @name .merge.irregular_case
401-
#' @author Felipe Carvalho, \email{filipe.carvalho@@inpe.br}
402-
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
403-
#' @noRd
404-
#' @param data1 Data cube
405-
#' @param data2 Data cube
406-
#' @return Merged data cube
407-
.merge.irregular_case <- function(data1, data2) {
408-
# verify if cube has the same bands
409-
has_same_bands <- .merge_has_equal_bands(data1, data2)
410-
# rule 1: if the bands are the same, combine cubes (`densify`)
411-
if (has_same_bands) {
412-
# merge!
413-
.merge_cube_densify(data1, data2)
414-
} else {
415-
# rule 2: if the bands are different and their timelines are
416-
# compatible, the bands are joined. The resulting timeline is the one
417-
# from the first cube.
418-
.merge_cube_compactify(data1, data2)
419-
}
420-
}
421368

422369
#' @title Merges cubes based on adequate strategy
423370
#' @name .merge
@@ -434,40 +381,19 @@
434381
if (.merge_type_hls(data1, data2)) {
435382
return("hls_case")
436383
}
437-
if (.merge_type_deaustralia_s2(data1, data2)) {
438-
return("irregular_case")
439-
}
440384
if (.merge_type_regular(data1, data2)) {
441385
return("regular_case")
442386
}
443-
if (.merge_type_irregular(data1, data2)) {
444-
return("irregular_case")
445-
}
446387
# find no alternative? error messages
447388
stop(.conf("messages", ".merge_type"), toString(class(data1)))
448389
}
449390
.merge_type_regular <- function(data1, data2) {
450391
.cube_is_regular(data1) &&
451-
.cube_is_regular(data2) &&
452-
.cube_has_unique_period(data1) &&
453-
.cube_has_unique_period(data2)
392+
.cube_is_regular(data2)
454393
}
455394
.merge_type_dem <- function(data1, data2) {
456395
any(inherits(data1, "dem_cube"), inherits(data2, "dem_cube"))
457396
}
458397
.merge_type_hls <- function(data1, data2) {
459398
all(inherits(data1, "hls_cube"), inherits(data2, "hls_cube"))
460399
}
461-
.merge_type_deaustralia_s2 <- function(data1, data2) {
462-
all(
463-
inherits(data1, "deaustralia_cube_ga_s2am_ard_3"),
464-
inherits(data2, "deaustralia_cube_ga_s2am_ard_3")
465-
) ||
466-
all(
467-
inherits(data1, "deaustralia_cube_ga_s2bm_ard_3"),
468-
inherits(data2, "deaustralia_cube_ga_s2bm_ard_3")
469-
)
470-
}
471-
.merge_type_irregular <- function(data1, data2) {
472-
!(.merge_type_regular(data1, data2))
473-
}

R/api_source_terrascope.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
platform = NULL) {
2121
# set caller to show in errors
2222
.check_set_caller(".source_items_new_terrascope_cube")
23+
# force token generation
24+
.source_terrascope_persist_token()
2325
# convert roi to bbox
2426
roi <- .stac_intersects_as_bbox(stac_query)
2527
# update stac query with the new spatial reference
2628
stac_query[["params"]][["intersects"]] <- NULL
2729
stac_query[["params"]][["bbox"]] <- roi[["bbox"]]
2830
# world cover product has all data available for a single date. So, fix the
2931
# temporal interval from the query
30-
stac_query[["params"]][["datetime"]] <- "2022-06-01T00:00:00Z"
32+
stac_query[["params"]][["datetime"]] <- "2021-01-01T00:00:00Z"
3133
# making the request
3234
items_info <- rstac::post_request(q = stac_query, ...)
3335
.check_stac_items(items_info)

R/sits_regularize.R

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ sits_regularize.sar_cube <- function(cube, ...,
274274
}
275275
# deal with ROI and tiles
276276
.check_roi_tiles(roi, tiles)
277-
#
278-
#
279277
if (.has(roi)) {
280278
roi <- .roi_as_sf(roi, default_crs = crs)
281279
}
@@ -313,63 +311,6 @@ sits_regularize.sar_cube <- function(cube, ...,
313311
}
314312
#' @rdname sits_regularize
315313
#' @export
316-
sits_regularize.combined_cube <- function(cube, ...,
317-
period,
318-
res,
319-
output_dir,
320-
grid_system = NULL,
321-
roi = NULL,
322-
crs = NULL,
323-
tiles = NULL,
324-
multicores = 2L,
325-
progress = TRUE) {
326-
# Preconditions
327-
.check_raster_cube_files(cube)
328-
.check_period(period)
329-
.check_num_parameter(res, exclusive_min = 0.0)
330-
output_dir <- .file_path_expand(output_dir)
331-
.check_output_dir(output_dir)
332-
.check_num_parameter(multicores, min = 1L, max = 2048L)
333-
progress <- .message_progress(progress)
334-
# check for ROI and tiles
335-
.check_roi_tiles(roi, tiles)
336-
if (.has(grid_system)) {
337-
.check_grid_system(grid_system)
338-
} else if (any("NoTilingSystem" %in% .cube_tiles(cube))) {
339-
grid_system <- "MGRS"
340-
}
341-
# Get a global timeline
342-
timeline <- .gc_get_valid_timeline(
343-
cube = cube, period = period
344-
)
345-
# Grouping by unique values for each type of cube: sar, optical, etc..
346-
cubes <- dplyr::group_by(
347-
cube, .data[["source"]], .data[["collection"]], .data[["satellite"]]
348-
) |> dplyr::group_map(~ {
349-
class(.x) <- .cube_s3class(.x)
350-
.x
351-
}, .keep = TRUE)
352-
# Regularizing each cube
353-
reg_cubes <- purrr::map(cubes, function(cube) {
354-
sits_regularize(
355-
cube = cube,
356-
timeline = timeline,
357-
period = period,
358-
res = res,
359-
roi = roi,
360-
crs = crs,
361-
tiles = tiles,
362-
output_dir = output_dir,
363-
grid_system = grid_system,
364-
multicores = multicores,
365-
progress = progress
366-
)
367-
})
368-
# In case where more than two cubes need to be merged
369-
purrr::reduce(reg_cubes, sits_merge)
370-
}
371-
#' @rdname sits_regularize
372-
#' @export
373314
sits_regularize.rainfall_cube <- function(cube, ...,
374315
period,
375316
res,

man/sits-package.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sits_regularize.Rd

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-cube-terrascope.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ test_that("Creating WORLD-COVER-2021 cubes from TERRASCOPE", {
1010
lat_min = -33.85777,
1111
lat_max = -32.56690
1212
),
13-
progress = FALSE
13+
progress = FALSE,
14+
crs = "EPSG:4326"
1415
)
1516
},
1617
.default = NULL
@@ -41,9 +42,10 @@ test_that("Creating WORLD-CEREAL-2021 cubes from TERRASCOPE",{
4142
{
4243
sits_cube(
4344
source = "TERRASCOPE",
44-
collection = "WORLD-COVER-2021",
45+
collection = "WORLD-CEREAL-2021",
4546
roi = bbox_22LBL,
46-
progress = FALSE
47+
progress = FALSE,
48+
crs = "EPSG:4326"
4749
)
4850
},
4951
.default = NULL
@@ -56,12 +58,16 @@ test_that("Creating WORLD-CEREAL-2021 cubes from TERRASCOPE",{
5658
cube = world_cereal_2021,
5759
roi = bbox_22LBL,
5860
multicores = 6,
59-
output_dir = tempdir()
61+
output_dir = tempdir(),
62+
crs = "EPSG:4326"
6063
)
64+
6165
roi_wc <- sits_bbox(world_cereal_2021_20LBL)[,1:4]
62-
roi_20LBL <- .bbox(bbox_22LBL, as_crs = "EPSG:4326")[,1:4]
66+
roi_20LBL <- .bbox(bbox_22LBL, as_crs = "EPSG:4326", default_crs = "EPSG:4326")[,1:4]
67+
6368
expect_equal(roi_wc[["xmin"]], roi_20LBL[["xmin"]], tolerance = 0.001)
64-
sumwc <- summary(world_cereal_2021_20LBL)
69+
sumwc <- suppressWarnings(summary(world_cereal_2021_20LBL))
70+
6571
expect_true(all(sumwc[["class"]] %in% c("Non_Cropland", "Cropland")))
6672
expect_true(all(sumwc[["value"]] %in% c("0", "100")))
6773
})

0 commit comments

Comments
 (0)