Skip to content

Commit

Permalink
merge cubes with one cube with a single timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed May 28, 2024
1 parent a68212c commit 691a70d
Show file tree
Hide file tree
Showing 14 changed files with 327 additions and 238 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ export(sits_combine_predictions)
export(sits_confidence_sampling)
export(sits_config)
export(sits_config_show)
export(sits_config_user_file)
export(sits_cube)
export(sits_cube_copy)
export(sits_detect_change)
Expand Down
98 changes: 96 additions & 2 deletions R/api_conf.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@
.check_that(file.exists(yml_file))
return(yml_file)
}
#' @title Return the user-relevant configuration file
#' @name .config_file
#' @keywords internal
#' @noRd
#' @return default user-relevant configuration file
.config_file <- function() {
.check_set_caller(".config_file")
# load the default configuration file
yml_file <- system.file("extdata", "config.yml", package = "sits")
# check that the file name is valid
.check_that(file.exists(yml_file))
return(yml_file)
}
#' @title Return the message configuration files (only for developers)
#' @name .conf_sources_files
#' @keywords internal
Expand Down Expand Up @@ -282,7 +295,7 @@
# pre condition - table contains name and hex code
.check_chr_contains(
x = colnames(color_tb),
contains = .conf("sits_color_table_cols"),
contains = .conf("color_table_cols"),
discriminator = "all_of"
)
# replace all duplicates
Expand Down Expand Up @@ -450,7 +463,8 @@
user_config[["gdalcubes_chunk_size"]],
sources = user_config[["sources"]],
colors = user_config[["colors"]],
tmap = user_config[["tmap"]]
view = user_config[["view"]],
plot = user_config[["plot"]]
)
}
}
Expand Down Expand Up @@ -484,6 +498,86 @@
)
return(invisible(bands))
}
#' @title List configuration parameters
#' @name .conf_list_params
#' @description List the contents of a source
#'
#' @keywords internal
#' @noRd
#' @param params parameter list
#'
#' @return Called for side effects.
.conf_list_params <- function(params) {
params <- lapply(params, function(x) {
if (is.atomic(x)) {
return(x)
}
list(names(x))
})
params_txt <- yaml::as.yaml(
params,
indent = 4,
handlers = list(
character = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
},
integer = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
},
numeric = function(x) {
res <- toString(x)
class(res) <- "verbatim"
res
}
)
)
cat(params_txt, sep = "\n")
}

#' @title List contents of a source
#' @name .conf_list_source
#' @description List the contents of a source
#'
#' @keywords internal
#' @noRd
#' @param source Data source
#'
#' @return Called for side effects.
.conf_list_source <- function(source){
cat(paste0(s, ":\n"))
collections <- .source_collections(source = s)
purrr::map(collections, function(c) {
cat(paste0("- ", c))
cat(paste0(
" (", .source_collection_satellite(s, c),
"/", .source_collection_sensor(s, c), ")\n",
"- grid system: ", .source_collection_grid_system(s, c), "\n"
))
cat("- bands: ")
cat(.source_bands(s, c))
cat("\n")
if (.source_collection_open_data(source = s, collection = c)) {
cat("- opendata collection ")
if (.source_collection_open_data(
source = s,
collection = c,
token = TRUE
)) {
cat("(requires access token)")
}
} else {
cat("- not opendata collection")
}
cat("\n")
cat("\n")
})
}


#' @title Get names associated to a configuration key
#' @name .conf_names
#' @param key key combination to access config information
Expand Down
54 changes: 29 additions & 25 deletions R/api_plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# select the file to be plotted
bw_file <- .tile_path(tile, band, date)
# size of data to be read
max_size <- .conf("view_max_size")
max_size <- .conf("view", "max_size")
sizes <- .tile_overview_size(tile = tile, max_size)
# scale and offset
band_conf <- .tile_band_conf(tile, band)
Expand All @@ -46,9 +46,11 @@
# extract the values
vals <- terra::values(rast)
# obtain the quantiles
fst_quant <- .as.numeric(.conf("plot", "first_quantile"))
lst_quant <- .as.numeric(.conf("plot", "last_quantile"))
quantiles <- stats::quantile(
vals,
probs = c(0, 0.02, 0.98, 1),
probs = c(0, fst_quant, lst_quant, 1),
na.rm = TRUE
)
minv <- quantiles[[1]]
Expand All @@ -70,11 +72,11 @@
}

# tmap params
labels_size <- as.numeric(.conf("tmap", "graticules_labels_size"))
legend_bg_color <- .conf("tmap", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("tmap", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("tmap", "legend_title_size"))
legend_text_size <- as.numeric(.conf("tmap", "legend_text_size"))
labels_size <- as.numeric(.conf("plot", "graticules_labels_size"))
legend_bg_color <- .conf("plot", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("plot", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("plot", "legend_title_size"))
legend_text_size <- as.numeric(.conf("plot", "legend_text_size"))

# generate plot
p <- tmap::tm_shape(rast, raster.downsample = FALSE) +
Expand Down Expand Up @@ -135,7 +137,7 @@
green_file <- .tile_path(tile, band, dates[[2]])
blue_file <- .tile_path(tile, band, dates[[3]])
# size of data to be read
max_size <- .conf("plot_max_size")
max_size <- .conf("plot", "max_size")
sizes <- .tile_overview_size(tile = tile, max_size)
# get the max values
band_params <- .tile_band_conf(tile, band)
Expand Down Expand Up @@ -197,7 +199,7 @@
band_params <- .tile_band_conf(tile, red)
max_value <- .max_value(band_params)
# size of data to be read
max_size <- .conf("plot_max_size")
max_size <- .conf("plot", "max_size")
sizes <- .tile_overview_size(tile = tile, max_size)
# used for SAR images
if (tile[["tile"]] == "NoTilingSystem") {
Expand Down Expand Up @@ -257,16 +259,18 @@
),
proxy = FALSE
)
fst_quant <- .as.numeric(.conf("plot", "first_quantile"))
lst_quant <- .as.numeric(.conf("plot", "last_quantile"))
# open RGB stars
rgb_st <- stars::st_rgb(rgb_st[, , , 1:3],
dimension = "band",
maxColorValue = max_value,
use_alpha = FALSE,
probs = c(0.05, 0.95),
probs = c(fst_quant, las_quant),
stretch = TRUE
)
# tmap params
labels_size <- as.numeric(.conf("tmap", "graticules_labels_size"))
labels_size <- as.numeric(.conf("plot", "graticules_labels_size"))

p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) +
tmap::tm_raster() +
Expand Down Expand Up @@ -306,7 +310,7 @@
# verifies if stars package is installed
.check_require_packages("stars")
# verifies if tmap package is installed
.check_require_packages("tmap")
.check_require_packages("plot")

# deal with color palette
.check_palette(palette)
Expand All @@ -321,7 +325,7 @@
)
names(colors) <- names(labels)
# size of data to be read
max_size <- .conf("plot_max_size")
max_size <- .conf("plot", "max_size")
sizes <- .tile_overview_size(tile = tile, max_size)
# select the image to be plotted
class_file <- .tile_path(tile)
Expand All @@ -340,11 +344,11 @@
stars_obj <- stats::setNames(stars_obj, "labels")

# tmap params
labels_size <- as.numeric(.conf("tmap", "graticules_labels_size"))
legend_bg_color <- .conf("tmap", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("tmap", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("tmap", "legend_title_size"))
legend_text_size <- as.numeric(.conf("tmap", "legend_text_size"))
labels_size <- as.numeric(.conf("plot", "graticules_labels_size"))
legend_bg_color <- .conf("plot", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("plot", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("plot", "legend_title_size"))
legend_text_size <- as.numeric(.conf("plot", "legend_text_size"))

# plot using tmap
p <- suppressMessages(
Expand Down Expand Up @@ -390,7 +394,7 @@
# verifies if stars package is installed
.check_require_packages("stars")
# verifies if tmap package is installed
.check_require_packages("tmap")
.check_require_packages("plot")
# precondition - check color palette
.check_palette(palette)
# revert the palette
Expand All @@ -408,7 +412,7 @@
.check_that(all(labels_plot %in% labels))
}
# size of data to be read
max_size <- .conf("plot_max_size")
max_size <- .conf("plot", "max_size")
sizes <- .tile_overview_size(tile = tile, max_size)
# get the path
probs_path <- .tile_path(tile)
Expand All @@ -432,11 +436,11 @@
# select stars bands to be plotted
bds <- as.numeric(names(labels[labels %in% labels_plot]))

labels_size <- as.numeric(.conf("tmap", "graticules_labels_size"))
legend_bg_color <- .conf("tmap", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("tmap", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("tmap", "legend_title_size"))
legend_text_size <- as.numeric(.conf("tmap", "legend_text_size"))
labels_size <- as.numeric(.conf("plot", "graticules_labels_size"))
legend_bg_color <- .conf("plot", "legend_bg_color")
legend_bg_alpha <- as.numeric(.conf("plot", "legend_bg_alpha"))
legend_title_size <- as.numeric(.conf("plot", "legend_title_size"))
legend_text_size <- as.numeric(.conf("plot", "legend_text_size"))

p <- tmap::tm_shape(probs_st[, , , bds]) +
tmap::tm_raster(
Expand Down
22 changes: 11 additions & 11 deletions R/api_plot_vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
palette = colors
) +
tmap::tm_graticules(
labels.size = as.numeric(.conf("tmap", "graticules_labels_size"))
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_compass() +
tmap::tm_layout(
scale = scale,
legend.bg.color = .conf("tmap", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("tmap", "legend_bg_alpha"))
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.2)
return(p)
Expand Down Expand Up @@ -87,7 +87,7 @@
# verifies if stars package is installed
.check_require_packages("stars")
# verifies if tmap package is installed
.check_require_packages("tmap")
.check_require_packages("plot")
# precondition - check color palette
.check_palette(palette)
# revert the palette
Expand Down Expand Up @@ -115,14 +115,14 @@
midpoint = 0.5,
title = labels[labels %in% labels_plot]) +
tmap::tm_graticules(
labels.size = as.numeric(.conf("tmap", "graticules_labels_size"))
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_facets() +
tmap::tm_compass() +
tmap::tm_layout(
scale = scale,
legend.bg.color = .conf("tmap", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("tmap", "legend_bg_alpha"))
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.1)

Expand Down Expand Up @@ -150,7 +150,7 @@
# verifies if stars package is installed
.check_require_packages("stars")
# verifies if tmap package is installed
.check_require_packages("tmap")
.check_require_packages("plot")
# precondition - check color palette
.check_palette(palette)
# revert the palette
Expand All @@ -167,16 +167,16 @@
palette = palette,
style = "cont") +
tmap::tm_graticules(
labels.size = as.numeric(.conf("tmap", "graticules_labels_size"))
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_compass() +
tmap::tm_layout(
main.title = main_title,
main.title.size = 1,
main.title.position = "center",
scale = scale,
legend.bg.color = .conf("tmap", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("tmap", "legend_bg_alpha"))
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.2)

Expand Down
Loading

0 comments on commit 691a70d

Please sign in to comment.