Skip to content

Commit 7cbc5e8

Browse files
Merge pull request #1257 from OldLipe/feat/dev-sits
Add support to multiple tiles in summary
2 parents 3e80043 + 803aed0 commit 7cbc5e8

File tree

3 files changed

+124
-124
lines changed

3 files changed

+124
-124
lines changed

R/sits_summary.R

+119-118
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ summary.raster_cube <- function(object, ..., tile = NULL, date = NULL) {
195195
#' @title Summary of a derived cube
196196
#' @author Felipe Souza, \email{felipe.souza@@inpe.br}
197197
#' @noRd
198-
#' @param object data cube
198+
#' @param object data cube
199199
#' @param ... Further specifications for \link{summary}.
200-
#' @param tile A \code{tile}.
200+
#' @param sample_size The size of samples will be extracted from the variance
201+
#' cube.
201202
#' @return Summary of a derived cube
202203
#'
203204
#' @examples
@@ -225,53 +226,48 @@ summary.raster_cube <- function(object, ..., tile = NULL, date = NULL) {
225226
#' }
226227
#'
227228
#' @export
228-
summary.derived_cube <- function(object, ..., tile = NULL) {
229+
summary.derived_cube <- function(object, ..., sample_size = 10000) {
229230
.check_set_caller("summary_derived_cube")
230-
# Pre-conditional check
231-
.check_chr_parameter(tile, allow_null = TRUE)
232-
# Extract the chosen tile
233-
if (!is.null(tile)) {
234-
object <- .summary_check_tile(object, tile)
235-
}
236-
# get sample size
237-
sample_size <- .conf("summary_sample_size")
238-
# Get tile name
239-
tile <- .default(tile, .cube_tiles(object)[[1]])
240-
tile <- .cube_filter_tiles(object, tile)
241-
# get the bands
242-
band <- .tile_bands(tile)
243-
.check_num(
244-
x = length(band),
245-
min = 1,
246-
max = 1,
247-
is_integer = TRUE
248-
)
249-
# extract the file paths
250-
files <- .tile_paths(tile)
251-
# read the files with terra
252-
r <- .raster_open_rast(files)
253-
# get the a sample of the values
254-
values <- r |>
255-
.raster_sample(size = sample_size, na.rm = TRUE)
256-
# scale the values
257-
band_conf <- .tile_band_conf(tile, band)
258-
scale <- .scale(band_conf)
259-
offset <- .offset(band_conf)
260-
sum <- summary(values * scale + offset)
261-
colnames(sum) <- .tile_labels(tile)
262-
return(sum)
231+
# Get cube labels
232+
labels <- unname(.cube_labels(object))
233+
# Extract variance values for each tiles using a sample size
234+
var_values <- slider::slide(object, function(tile) {
235+
# get the bands
236+
band <- .tile_bands(tile)
237+
# extract the file path
238+
file <- .tile_paths(tile)
239+
# read the files with terra
240+
r <- .raster_open_rast(file)
241+
# get the a sample of the values
242+
values <- r |>
243+
.raster_sample(size = sample_size, na.rm = TRUE)
244+
# scale the values
245+
band_conf <- .tile_band_conf(tile, band)
246+
scale <- .scale(band_conf)
247+
offset <- .offset(band_conf)
248+
values <- values * scale + offset
249+
values
250+
})
251+
# Combine variance values
252+
var_values <- dplyr::bind_rows(var_values)
253+
var_values <- summary(var_values)
254+
# Update columns name
255+
colnames(var_values) <- labels
256+
# Return summary values
257+
return(var_values)
263258
}
264259
#' @title Summarise variance cubes
265260
#' @method summary variance_cube
266261
#' @name summary.variance_cube
267262
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
268263
#' @description This is a generic function. Parameters depend on the specific
269264
#' type of input.
270-
#' @param object Object of class "class_cube"
271-
#' @param ... Further specifications for \link{summary}.
272-
#' @param tile Tile to be summarized
273-
#' @param intervals Intervals to calculate the quantiles
274-
#' @param quantiles Quantiles to be shown
265+
#' @param object Object of class "class_cube"
266+
#' @param ... Further specifications for \link{summary}.
267+
#' @param sample_size The size of samples will be extracted from the variance
268+
#' cube.
269+
#' @param intervals Intervals to calculate the quantiles
270+
#' @param quantiles Quantiles to be shown
275271
#'
276272
#' @return A summary of a variance cube
277273
#'
@@ -299,45 +295,47 @@ summary.derived_cube <- function(object, ..., tile = NULL) {
299295
#' @export
300296
summary.variance_cube <- function(
301297
object, ...,
302-
tile = NULL,
303298
intervals = 0.05,
304-
quantiles = c ("75%", "80%", "85%", "90%", "95%", "100%")) {
299+
sample_size = 10000,
300+
quantiles = c("75%", "80%", "85%", "90%", "95%", "100%")) {
305301
.check_set_caller("summary_variance_cube")
306-
# Pre-conditional check
307-
.check_chr_parameter(tile, allow_null = TRUE)
308-
# Extract the chosen tile
309-
if (!is.null(tile)) {
310-
object <- .summary_check_tile(object, tile)
311-
}
312-
# get sample size
313-
sample_size <- .conf("summary_sample_size")
314-
# Get tile name
315-
tile <- .default(tile, .cube_tiles(object)[[1]])
316-
tile <- .cube_filter_tiles(object, tile)
317-
# get the bands
318-
band <- .tile_bands(tile)
319-
# extract the file paths
320-
files <- .tile_paths(tile)
321-
# read the files with terra
322-
r <- .raster_open_rast(files)
323-
# get the a sample of the values
324-
values <- r |>
325-
.raster_sample(size = sample_size, na.rm = TRUE)
326-
# scale the values
327-
band_conf <- .tile_band_conf(tile, band)
328-
scale <- .scale(band_conf)
329-
offset <- .offset(band_conf)
330-
values <- values * scale + offset
331-
# calculate the quantiles
332-
mat <- apply(values, 2, function(x){
333-
stats::quantile(x, probs = seq(0, 1, intervals))
302+
# Get cube labels
303+
labels <- unname(.cube_labels(object))
304+
# Extract variance values for each tiles using a sample size
305+
var_values <- slider::slide(object, function(tile) {
306+
# get the bands
307+
band <- .tile_bands(tile)
308+
# extract the file path
309+
file <- .tile_paths(tile)
310+
# read the files with terra
311+
r <- .raster_open_rast(file)
312+
# get the a sample of the values
313+
values <- r |>
314+
.raster_sample(size = sample_size, na.rm = TRUE)
315+
# scale the values
316+
band_conf <- .tile_band_conf(tile, band)
317+
scale <- .scale(band_conf)
318+
offset <- .offset(band_conf)
319+
values <- values * scale + offset
320+
values
334321
})
335-
colnames(mat) <- .tile_labels(tile)
336-
337-
return(mat[quantiles, ])
322+
# Combine variance values
323+
var_values <- dplyr::bind_rows(var_values)
324+
# Update columns name
325+
colnames(var_values) <- labels
326+
# Extract quantile for each column
327+
var_values <- dplyr::reframe(
328+
var_values,
329+
dplyr::across(.cols = dplyr::all_of(labels), function(x) {
330+
stats::quantile(x, probs = seq(0, 1, intervals))
331+
})
332+
)
333+
# Update row names
334+
percent_intervals <- paste0(seq(from = 0, to = 1, by = intervals)*100, "%")
335+
rownames(var_values) <- percent_intervals
336+
# Return variance values filtered by quantiles
337+
return(var_values[quantiles, ])
338338
}
339-
#'
340-
#'
341339
#' @title Summarize data cubes
342340
#' @method summary class_cube
343341
#' @name summary.class_cube
@@ -346,7 +344,6 @@ summary.variance_cube <- function(
346344
#' type of input.
347345
#' @param object Object of class "class_cube"
348346
#' @param ... Further specifications for \link{summary}.
349-
#' @param tile Tile to be summarized
350347
#'
351348
#' @return A summary of a classified cube
352349
#'
@@ -373,46 +370,50 @@ summary.variance_cube <- function(
373370
#' summary(label_cube)
374371
#' }
375372
#' @export
376-
#'
377-
summary.class_cube <- function(object, ..., tile = NULL) {
373+
summary.class_cube <- function(object, ...) {
378374
.check_set_caller("summary_class_cube")
379-
# Pre-conditional check
380-
.check_chr_parameter(tile, allow_null = TRUE)
381-
# Extract the chosen tile
382-
if (!is.null(tile)) {
383-
object <- .summary_check_tile(object, tile)
384-
}
385-
# Get tile name
386-
tile <- .default(tile, .cube_tiles(object)[[1]])
387-
tile <- .cube_filter_tiles(object, tile)
388-
# get the bands
389-
bands <- .tile_bands(tile)
390-
.check_chr_parameter(bands, len_min = 1, len_max = 1)
391-
# extract the file paths
392-
files <- .tile_paths(tile)
393-
# read raster files
394-
r <- .raster_open_rast(files)
395-
# get a frequency of values
396-
class_areas <- .raster_freq(r)
397-
# transform to km^2
398-
cell_size <- .tile_xres(tile) * .tile_yres(tile)
399-
class_areas[["area"]] <- (class_areas[["count"]] * cell_size) / 10^6
400-
# change value to character
401-
class_areas <- dplyr::mutate(class_areas,
402-
value = as.character(.data[["value"]])
403-
)
404-
# create a data.frame with the labels
405-
labels <- .tile_labels(tile)
406-
df1 <- tibble::tibble(value = names(labels), class = unname(labels))
407-
# join the labels with the areas
408-
sum <- dplyr::full_join(df1, class_areas, by = "value")
409-
sum <- dplyr::mutate(sum,
410-
area_km2 = signif(.data[["area"]], 2),
411-
.keep = "unused"
412-
)
413-
# remove layer information
414-
sum_clean <- sum[, -3] |>
415-
tidyr::replace_na(list(layer = 1, count = 0, area_km2 = 0))
416-
# show the result
417-
return(sum_clean)
375+
# Get cube labels
376+
labels <- unname(.cube_labels(object))
377+
# Extract classes values for each tiles using a sample size
378+
classes_areas <- slider::slide(object, function(tile) {
379+
# get the bands
380+
band <- .tile_bands(tile)
381+
# extract the file path
382+
file <- .tile_paths(tile)
383+
# read the files with terra
384+
r <- .raster_open_rast(file)
385+
# get a frequency of values
386+
class_areas <- .raster_freq(r)
387+
# transform to km^2
388+
cell_size <- .tile_xres(tile) * .tile_yres(tile)
389+
class_areas[["area"]] <- (class_areas[["count"]] * cell_size) / 10^6
390+
# change value to character
391+
class_areas <- dplyr::mutate(
392+
class_areas, value = as.character(.data[["value"]])
393+
)
394+
# create a data.frame with the labels
395+
labels <- .tile_labels(tile)
396+
df1 <- tibble::tibble(value = names(labels), class = unname(labels))
397+
# join the labels with the areas
398+
sum <- dplyr::full_join(df1, class_areas, by = "value")
399+
sum <- dplyr::mutate(sum,
400+
area_km2 = signif(.data[["area"]], 2),
401+
.keep = "unused"
402+
)
403+
# remove layer information
404+
sum_clean <- sum[, -3] |>
405+
tidyr::replace_na(list(layer = 1, count = 0, area_km2 = 0))
406+
407+
sum_clean
408+
})
409+
# Combine tiles areas
410+
classes_areas <- dplyr::bind_rows(classes_areas) |>
411+
dplyr::group_by(.data[["value"]], .data[["class"]]) |>
412+
dplyr::summarise(
413+
count = sum(.data[["count"]]),
414+
area_km2 = sum(.data[["area_km2"]]),
415+
.groups = "keep") |>
416+
dplyr::ungroup()
417+
# Return classes areas
418+
return(classes_areas)
418419
}

man/summary.class_cube.Rd

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/summary.variance_cube.Rd

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)