diff --git a/DESCRIPTION b/DESCRIPTION index bd5c0d9b0..b1d7616c0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: sits Type: Package -Version: 1.5.0 +Version: 1.5.1 Title: Satellite Image Time Series Analysis for Earth Observation Data Cubes Authors@R: c(person('Rolf', 'Simoes', role = c('aut'), email = 'rolf.simoes@inpe.br'), person('Gilberto', 'Camara', role = c('aut', 'cre'), email = 'gilberto.camara.inpe@gmail.com'), diff --git a/R/api_gdal.R b/R/api_gdal.R index 44ae413e2..73f70b232 100644 --- a/R/api_gdal.R +++ b/R/api_gdal.R @@ -156,7 +156,7 @@ ), params = list( "-ot" = data_type, - "-of" = .conf("gdal_presets", "block", "of"), + "-of" = .conf("gdal_presets", "image", "of"), "-b" = rep(1, nlayers), "-outsize" = list(.ncols(block), .nrows(block)), "-scale" = list(0, 1, miss_value, miss_value), @@ -165,7 +165,7 @@ .xmin(bbox), .ymax(bbox), .xmax(bbox), .ymin(bbox) ), "-a_nodata" = miss_value, - "-co" = .conf("gdal_presets", "block", "co") + "-co" = .conf("gdal_presets", "image", "co") ), quiet = TRUE ) diff --git a/R/api_plot_raster.R b/R/api_plot_raster.R index 783aae059..4e84476b8 100644 --- a/R/api_plot_raster.R +++ b/R/api_plot_raster.R @@ -1,5 +1,5 @@ #' @title Plot a false color image -#' @name .plot_raster.false_color +#' @name .plot_false_color #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} #' @description plots a set of false color image #' @keywords internal @@ -11,14 +11,11 @@ #' @param seg_color Color to use for segment borders #' @param line_width Line width to plot the segments boundary #' @param palette A sequential RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be plotted +#' @param main_title Main title for the plot #' @param rev Reverse the color palette? #' @param scale Scale to plot map (0.4 to 1.0) #' -#' @return A plot object +#' @return A list of plot objects .plot_false_color <- function(tile, band, date, @@ -26,60 +23,38 @@ seg_color, line_width, palette, - style, - n_colors, + main_title, rev, scale) { - # verifies if stars package is installed - .check_require_packages("stars") - # verifies if tmap package is installed - .check_require_packages("tmap") - # check palette - .check_palette(palette) - # check style - .check_chr_within( - style, - within = .conf("tmap_continuous_style"), - discriminator = "any_of" - ) - # check number of colors - .check_int_parameter(n_colors, min = 4) - # check rev - .check_lgl_parameter(rev) - # check scale parameter - .check_num_parameter(scale, min = 0.2) - # check SAR Cube - palette <- .view_adjust_palette(tile, palette) - # reverse the color palette? - if (rev) - palette <- paste0("-", palette) # select the file to be plotted bw_file <- .tile_path(tile, band, date) # size of data to be read - max_size <- .conf("plot_max_size") + max_size <- .conf("view_max_size") sizes <- .tile_overview_size(tile = tile, max_size) - # used for SAR images without tiling system - if (tile[["tile"]] == "NoTilingSystem") { - bw_file <- .gdal_warp_grd(bw_file, sizes) - } - # read file - stars_obj <- stars::read_stars( - bw_file, - RasterIO = list( - nBufXSize = sizes[["xsize"]], - nBufYSize = sizes[["ysize"]] - ), - proxy = FALSE - ) - - # rescale the stars object - band_conf <- .tile_band_conf(tile = tile, band = band) + # scale and offset + band_conf <- .tile_band_conf(tile, band) band_scale <- .scale(band_conf) band_offset <- .offset(band_conf) - stars_obj <- stars_obj * band_scale + band_offset - stars_obj <- stars_obj[stars_obj <= 1.0] - - + max_value <- .max_value(band_conf) + # used for SAR images without tiling system + #if (tile[["tile"]] == "NoTilingSystem") { + bw_file <- .gdal_warp_grd(bw_file, sizes) + #} + # open the file in terra + rast <- terra::rast(bw_file) + # apply scale and offset + rast <- rast * band_scale + band_offset + # get the quantiles + vals <- terra::values(rast) + quantiles <- stats::quantile(vals, probs = c(0, 0.05, 0.95, 1)) + maxq <- quantiles[[3]] + minq <- quantiles[[2]] + maxv <- quantiles[[4]] + minv <- quantiles[[1]] + range <- maxv - minv + rangeq <- maxq - minq + stretch <- rangeq / range + rast <- stretch * (rast - minv) + minq # tmap params labels_size <- as.numeric(.conf("tmap", "graticules_labels_size")) legend_bg_color <- .conf("tmap", "legend_bg_color") @@ -88,111 +63,87 @@ legend_text_size <- as.numeric(.conf("tmap", "legend_text_size")) # generate plot - p <- suppressMessages( - tmap::tm_shape(stars_obj, raster.downsample = FALSE) + - tmap::tm_raster( - style = style, - n = n_colors, - palette = palette, - title = band, - midpoint = NA, - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = labels_size - ) + - tmap::tm_compass() + - tmap::tm_layout( - scale = scale, - legend.bg.color = legend_bg_color, - legend.bg.alpha = legend_bg_alpha, - legend.title.size = legend_title_size, - legend.text.size = legend_text_size - ) - ) + p <- tmap::tm_shape(rast, raster.downsample = FALSE) + + tmap::tm_raster( + palette = palette, + title = band, + style = "cont", + style.args = list(na.rm = TRUE) + + ) + + tmap::tm_graticules( + labels.size = labels_size + ) + + tmap::tm_compass() + + tmap::tm_layout( + main.title = main_title, + main.title.size = 1, + main.title.position = "center", + legend.bg.color = legend_bg_color, + legend.bg.alpha = legend_bg_alpha, + legend.title.size = legend_title_size, + legend.text.size = legend_text_size, + scale = scale + ) # include segments if (.has(sf_seg)) { p <- p + tmap::tm_shape(sf_seg) + tmap::tm_borders(col = seg_color, lwd = line_width) } return(p) + } -#' @title Plot a classified image -#' @name .plot_class_image + +#' @title Plot a multi-date band as RGB +#' @name .plot_band_multidate #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a classified image +#' @description plots a set of false color image #' @keywords internal #' @noRd #' @param tile Tile to be plotted. -#' @param legend Legend for the classes +#' @param band Band to be plotted. +#' @param dates Dates to be plotted. #' @param palette A sequential RColorBrewer palette -#' @param scale Scale to plot the map +#' @param main_title Main title for the plot +#' @param rev Reverse the color palette? +#' @param scale Scale to plot map (0.4 to 1.0) #' -#' @return A plot object +#' @return A list of plot objects #' -.plot_class_image <- function(tile, legend, palette, scale) { - # verifies if stars package is installed - .check_require_packages("stars") - # verifies if tmap package is installed - .check_require_packages("tmap") - - # deal with color palette - .check_palette(palette) - # get the labels - labels <- unlist(.cube_labels(tile, dissolve = FALSE)) - # obtain the colors - colors <- .colors_get( - labels = labels, - legend = legend, - palette = palette, - rev = TRUE - ) - names(colors) <- names(labels) +.plot_band_multidate <- function(tile, + band, + dates, + palette, + main_title, + rev, + scale) { + # select the files to be plotted + red_file <- .tile_path(tile, band, dates[[1]]) + 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") sizes <- .tile_overview_size(tile = tile, max_size) - # select the image to be plotted - class_file <- .tile_path(tile) - - # read file - stars_obj <- stars::read_stars( - class_file, - RasterIO = list( - nBufXSize = sizes[["xsize"]], - nBufYSize = sizes[["ysize"]] - ), - proxy = FALSE - ) - - # rename stars object - 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")) - - # plot using tmap - p <- suppressMessages( - tmap::tm_shape(stars_obj, raster.downsample = FALSE) + - tmap::tm_raster( - style = "cat", - palette = colors, - labels = labels - ) + - tmap::tm_graticules( - labels.size = labels_size - ) + - tmap::tm_compass() + - tmap::tm_layout( - scale = scale, - legend.bg.color = legend_bg_color, - legend.bg.alpha = legend_bg_alpha, - legend.title.size = legend_title_size, - legend.text.size = legend_text_size - ) + # get the max values + band_params <- .tile_band_conf(tile, band) + max_value <- .max_value(band_params) + # used for SAR images without tiling system + if (tile[["tile"]] == "NoTilingSystem") { + red_file <- .gdal_warp_grd(red_file, sizes) + green_file <- .gdal_warp_grd(green_file, sizes) + blue_file <- .gdal_warp_grd(blue_file, sizes) + } + # plot multitemporal band as RGB + p <- .plot_rgb_stars( + red_file = red_file, + green_file = green_file, + blue_file = blue_file, + sizes = sizes, + max_value = max_value, + main_title = main_title, + sf_seg = NULL, + seg_color = NULL, + line_width = NULL ) return(p) } @@ -206,6 +157,7 @@ #' @param green Band to be plotted in green #' @param blue Band to be plotted in blue #' @param date Date to be plotted +#' @param main_title Main title for the plot #' @param sf_seg Segments (sf object) #' @param seg_color Color to use for segment borders #' @param line_width Line width to plot the segments boundary @@ -216,19 +168,18 @@ green, blue, date, - sf_seg = NULL, - seg_color = NULL, - line_width = 0.2) { - # verifies if stars package is installed - .check_require_packages("stars") - # verifies if tmap package is installed - .check_require_packages("tmap") - + main_title, + sf_seg, + seg_color, + line_width) { # get RGB files for the requested timeline red_file <- .tile_path(tile, red, date) green_file <- .tile_path(tile, green, date) blue_file <- .tile_path(tile, blue, date) + # get the max values + 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") sizes <- .tile_overview_size(tile = tile, max_size) @@ -238,6 +189,45 @@ green_file <- .gdal_warp_grd(green_file, sizes) blue_file <- .gdal_warp_grd(blue_file, sizes) } + p <- .plot_rgb_stars( + red_file = red_file, + green_file = green_file, + blue_file = blue_file, + sizes = sizes, + max_value = max_value, + main_title = main_title, + sf_seg = sf_seg, + seg_color = seg_color, + line_width = line_width + ) + return(p) +} +#' @title Plot a RGB image using stars and tmap +#' @name .plot_rgb_stars +#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} +#' @keywords internal +#' @noRd +#' @param red_file File to be plotted in red +#' @param green_file File to be plotted in green +#' @param blue_file File to be plotted in blue +#' @param sizes Image sizes for overview +#' @param max_value Maximum value +#' @param main_title Main title +#' @param sf_seg Segments (sf object) +#' @param seg_color Color to use for segment borders +#' @param line_width Line width to plot the segments boundary +#' @return A plot object +#' +.plot_rgb_stars <- function(red_file, + green_file, + blue_file, + sizes, + max_value, + main_title, + sf_seg, + seg_color, + line_width){ + # read raster data as a stars object with separate RGB bands rgb_st <- stars::read_stars( c(red_file, green_file, blue_file), @@ -248,10 +238,7 @@ ), proxy = FALSE ) - # get the max values - band_params <- .tile_band_conf(tile, red) - max_value <- .max_value(band_params) - + # open RGB stars rgb_st <- stars::st_rgb(rgb_st[, , , 1:3], dimension = "band", maxColorValue = max_value, @@ -267,6 +254,11 @@ tmap::tm_graticules( labels.size = labels_size ) + + tmap::tm_layout( + main.title = main_title, + main.title.size = 1, + main.title.position = "center" + ) + tmap::tm_compass() # include segments @@ -277,6 +269,85 @@ return(p) } +#' @title Plot a classified image +#' @name .plot_class_image +#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} +#' @description plots a classified image +#' @keywords internal +#' @noRd +#' @param tile Tile to be plotted. +#' @param legend Legend for the classes +#' @param palette A sequential RColorBrewer palette +#' @param scale Scale to plot the map +#' +#' @return A plot object +#' +.plot_class_image <- function(tile, legend, palette, scale) { + # verifies if stars package is installed + .check_require_packages("stars") + # verifies if tmap package is installed + .check_require_packages("tmap") + + # deal with color palette + .check_palette(palette) + # get the labels + labels <- unlist(.cube_labels(tile, dissolve = FALSE)) + # obtain the colors + colors <- .colors_get( + labels = labels, + legend = legend, + palette = palette, + rev = TRUE + ) + names(colors) <- names(labels) + # size of data to be read + 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) + + # read file + stars_obj <- stars::read_stars( + class_file, + RasterIO = list( + nBufXSize = sizes[["xsize"]], + nBufYSize = sizes[["ysize"]] + ), + proxy = FALSE + ) + + # rename stars object + 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")) + + # plot using tmap + p <- suppressMessages( + tmap::tm_shape(stars_obj, raster.downsample = FALSE) + + tmap::tm_raster( + style = "cat", + palette = colors, + labels = labels + ) + + tmap::tm_graticules( + labels.size = labels_size + ) + + tmap::tm_compass() + + tmap::tm_layout( + scale = scale, + legend.bg.color = legend_bg_color, + legend.bg.alpha = legend_bg_alpha, + legend.title.size = legend_title_size, + legend.text.size = legend_text_size + ) + ) + return(p) +} #' @title Plot probs #' @name .plot_probs #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -285,10 +356,6 @@ #' @param tile Probs cube to be plotted. #' @param labels_plot Labels to be plotted #' @param palette A sequential RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse the color palette? #' @param scale Global scale for plot #' @return A plot object @@ -296,8 +363,6 @@ .plot_probs <- function(tile, labels_plot, palette, - style, - n_colors, rev, scale) { # set caller to show in errors @@ -355,9 +420,8 @@ p <- tmap::tm_shape(probs_st[, , , bds]) + tmap::tm_raster( - style = style, + style = "cont", palette = palette, - n = n_colors, midpoint = NA, title = labels[labels %in% labels_plot] ) + diff --git a/R/api_plot_time_series.R b/R/api_plot_time_series.R index f013e5b6c..e44841df8 100644 --- a/R/api_plot_time_series.R +++ b/R/api_plot_time_series.R @@ -161,8 +161,7 @@ group = .data[["variable"]] )) + ggplot2::geom_line(ggplot2::aes(color = .data[["variable"]])) + - ggplot2::labs(title = plot_title) + - ggplot2::scale_fill_manual(palette = colors) + ggplot2::labs(title = plot_title) return(g) } #' @title Plot one time series with NAs using ggplot diff --git a/R/api_plot_vector.R b/R/api_plot_vector.R index 39efc4498..509005639 100644 --- a/R/api_plot_vector.R +++ b/R/api_plot_vector.R @@ -136,9 +136,7 @@ #' @noRd #' @param tile Tile to be plotted. #' @param palette A sequential RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") +#' @param main_title Main title for the cube #' @param rev Revert the color of the palette? #' @param scale Global map scale #' @@ -146,7 +144,7 @@ #' .plot_uncertainty_vector <- function(tile, palette, - style, + main_title, rev, scale) { # verifies if stars package is installed @@ -167,12 +165,15 @@ p <- tmap::tm_shape(sf_seg) + tmap::tm_polygons(uncert_type, palette = palette, - style = style) + + style = "cont") + tmap::tm_graticules( labels.size = as.numeric(.conf("tmap", "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")) diff --git a/R/sits_apply.R b/R/sits_apply.R index d86d2cb58..6665b98ed 100644 --- a/R/sits_apply.R +++ b/R/sits_apply.R @@ -128,7 +128,7 @@ sits_apply.raster_cube <- function(data, ..., .check_is_raster_cube(data) .check_that(.cube_is_regular(data)) # Check window size - .check_int_parameter(window_size, min = 3, is_odd = TRUE) + .check_int_parameter(window_size, min = 1, is_odd = TRUE) # Check normalized index .check_lgl_parameter(normalized) # Check memsize @@ -158,11 +158,10 @@ sits_apply.raster_cube <- function(data, ..., bands = bands, expr = expr ) - # Check memory and multicores - # Get block size - block <- .raster_file_blocksize(.raster_open_rast(.tile_path(data))) # Overlapping pixels overlap <- ceiling(window_size / 2) - 1 + # Get block size + block <- .raster_file_blocksize(.raster_open_rast(.tile_path(data))) # Check minimum memory needed to process one block job_memsize <- .jobs_memsize( job_size = .block_size(block = block, overlap = overlap), @@ -170,6 +169,16 @@ sits_apply.raster_cube <- function(data, ..., nbytes = 8, proc_bloat = .conf("processing_bloat_cpu") ) + # Update block parameter + block <- .jobs_optimal_block( + job_memsize = job_memsize, + block = block, + image_size = .tile_size(.tile(data)), + memsize = memsize, + multicores = multicores + ) + # adjust for blocks of size 1 + block <- .block_regulate_size(block) # Update multicores parameter multicores <- .jobs_max_multicores( job_memsize = job_memsize, diff --git a/R/sits_plot.R b/R/sits_plot.R index e1a9902c7..ced095292 100644 --- a/R/sits_plot.R +++ b/R/sits_plot.R @@ -321,22 +321,21 @@ plot.predicted <- function(x, y, ..., #' @param green Band for green color. #' @param blue Band for blue color. #' @param tile Tile to be plotted. -#' @param date Date to be plotted. +#' @param dates Dates to be plotted. #' @param palette An RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse the color order in the palette? #' @param scale Scale to plot map (0.4 to 1.0) #' #' @return A plot object with an RGB image -#' or a B/W image on a color -#' scale using the pallete +#' or a B/W image on a color scale #' -#' @note To see which colors are supported, please run \code{sits_colors()} +#' @note #' Use \code{scale} parameter for general output control. -#' If required, then set the other params individually +#' The \code{dates} parameter indicates the date allows plotting of different dates when +#' a single band and three dates are provided, `sits` will plot a +#' multi-temporal RGB image for a single band (useful in the case of +#' SAR data). For RGB bands with multi-dates, multiple plots will be +#' produced. #' @examples #' if (sits_run_examples()) { #' # create a data cube from local files @@ -347,7 +346,8 @@ plot.predicted <- function(x, y, ..., #' data_dir = data_dir #' ) #' # plot NDVI band of the second date date of the data cube -#' plot(cube, band = "NDVI", date = sits_timeline(cube)[1]) +#' plot(cube, band = "NDVI", dates = sits_timeline(cube)[1]) +#' # plot NDVI band as an RGB composite for the three bands #' } #' @export plot.raster_cube <- function(x, ..., @@ -356,13 +356,18 @@ plot.raster_cube <- function(x, ..., green = NULL, blue = NULL, tile = x[["tile"]][[1]], - date = NULL, + dates = NULL, palette = "RdYlGn", - style = "cont", - n_colors = 10, rev = FALSE, - scale = 1) { + scale = 1.2) { + # check caller .check_set_caller(".plot_raster_cube") + # retrieve dots + dots <- list(...) + # deal with wrong parameter "date" + if ("date" %in% names(dots) && missing(dates)) { + dates <- as.Date(dots[["date"]]) + } # is tile inside the cube? .check_chr_contains( x = x[["tile"]], @@ -372,48 +377,93 @@ plot.raster_cube <- function(x, ..., can_repeat = FALSE, msg = .conf("messages", ".plot_raster_cube_tile") ) + # verifies if stars package is installed + .check_require_packages("stars") + # verifies if tmap package is installed + .check_require_packages("tmap") + .check_require_packages("tmaptools") + if (.has(band)) { + # check palette + .check_palette(palette) + # check rev + .check_lgl_parameter(rev) + } + # check scale parameter + .check_num_parameter(scale, min = 0.2) + # reverse the color palette? + if (rev) + palette <- paste0("-", palette) # filter the tile to be processed tile <- .cube_filter_tiles(cube = x, tiles = tile) - if (.has(date)) { + if (.has(dates)) { # is this a valid date? - date <- as.Date(date) - .check_that(date %in% .tile_timeline(tile), + dates <- as.Date(dates) + .check_that(all(dates %in% .tile_timeline(tile)), msg = .conf("messages", ".plot_raster_cube_date") ) } else { - date <- .tile_timeline(tile)[[1]] + dates <- .tile_timeline(tile)[[1]] } - # only one date at a time - .check_that(length(date) == 1, - msg = .conf("messages", ".plot_raster_cube_single_date")) # BW or color? .check_bw_rgb_bands(band, red, green, blue) .check_available_bands(x, band, red, green, blue) - if (.has(band)) + + if (.has(band) && length(dates) == 3) { + main_title <- paste0(.tile_collection(tile), " ", band, " ", + as.Date(dates[[1]]), "(R) ", + as.Date(dates[[2]]), "(G) ", + as.Date(dates[[3]]), "(B) " + ) + p <- .plot_band_multidate( + tile = tile, + band = band, + dates = dates, + palette = palette, + main_title = main_title, + rev = rev, + scale = scale + ) + return(p) + } + if (length(dates) > 1) { + warning(.conf("messages", ".plot_raster_cube_single_date")) + } + if (.has(band)) { + main_title <- paste0(.tile_collection(tile), " ", band, + " ", as.Date(dates[[1]])) p <- .plot_false_color( tile = tile, band = band, - date = date, + date = dates[[1]], sf_seg = NULL, seg_color = NULL, line_width = NULL, palette = palette, - style = style, - n_colors = n_colors, + main_title = main_title, rev = rev, scale = scale ) - else + } else { # plot RGB + main_title <- paste0(.tile_collection(tile)," ", + red, "(R) ", + green, "(G) ", + blue, "(B) ", + as.Date(dates[[1]]) + ) p <- .plot_rgb( tile = tile, red = red, green = green, blue = blue, - date = date, + date = dates[[1]], + main_title = main_title, sf_seg = NULL, - seg_color = NULL + seg_color = NULL, + line_width = NULL ) + } + return(p) } #' @title Plot RGB vector data cubes @@ -429,14 +479,10 @@ plot.raster_cube <- function(x, ..., #' @param green Band for green color. #' @param blue Band for blue color. #' @param tile Tile to be plotted. -#' @param date Date to be plotted. +#' @param dates Dates to be plotted. #' @param seg_color Color to show the segment boundaries #' @param line_width Line width to plot the segments boundary (in pixels) #' @param palette An RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse the color order in the palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @@ -471,15 +517,19 @@ plot.vector_cube <- function(x, ..., green = NULL, blue = NULL, tile = x[["tile"]][[1]], - date = NULL, + dates = NULL, seg_color = "black", line_width = 1, palette = "RdYlGn", - style = "cont", - n_colors = 10, rev = FALSE, - scale = 0.8) { + scale = 1.0) { .check_set_caller(".plot_vector_cube") + # retrieve dots + dots <- list(...) + # deal with wrong parameter "date" + if ("date" %in% names(dots) && missing(dates)) { + dates <- as.Date(dots[["date"]]) + } # is tile inside the cube? .check_chr_contains( x = x[["tile"]], @@ -491,24 +541,24 @@ plot.vector_cube <- function(x, ..., ) # filter the tile to be processed tile <- .cube_filter_tiles(cube = x, tiles = tile) - if (!.has(date)) { - date <- .tile_timeline(tile)[[1]] + if (.has(dates)) { + # is this a valid date? + dates <- as.Date(dates)[[1]] + .check_that(all(dates %in% .tile_timeline(tile)), + msg = .conf("messages", ".plot_raster_cube_date") + ) + } else { + dates <- .tile_timeline(tile)[[1]] } - # only one date at a time - .check_that(length(date) == 1, - msg = .conf("messages", ".plot_raster_cube_single_date") - ) - # is this a valid date? - date <- as.Date(date) - .check_that(date %in% .tile_timeline(tile), - msg = .conf("messages", ".plot_raster_cube_date") - ) # retrieve the segments for this tile sf_seg <- .segments_read_vec(tile) # BW or color? .check_bw_rgb_bands(band, red, green, blue) .check_available_bands(x, band, red, green, blue) if (.has(band)) { + main_title <- paste0( + .tile_collection(tile), " ", band, " ", as.Date(date) + ) # plot the band as false color p <- .plot_false_color( tile = tile, @@ -518,12 +568,17 @@ plot.vector_cube <- function(x, ..., seg_color = seg_color, line_width = line_width, palette = palette, - style = style, - n_colors = n_colors, + main_title = main_title, rev = rev, scale = scale ) } else { + main_title <- paste0(.tile_collection(tile)," ", + red, "(R) ", + green, "(G) ", + blue, "(B) ", + as.Date(date) + ) # plot RGB p <- .plot_rgb( tile = tile, @@ -531,6 +586,7 @@ plot.vector_cube <- function(x, ..., green = green, blue = blue, date = date, + main_title = main_title, sf_seg = sf_seg, seg_color = seg_color, line_width = line_width @@ -548,10 +604,6 @@ plot.vector_cube <- function(x, ..., #' @param tile Tile to be plotted. #' @param labels Labels to plot (optional). #' @param palette RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse order of colors in palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @return A plot containing probabilities associated @@ -583,8 +635,6 @@ plot.probs_cube <- function(x, ..., tile = x[["tile"]][[1]], labels = NULL, palette = "YlGn", - style = "cont", - n_colors = 10, rev = FALSE, scale = 0.8) { .check_set_caller(".plot_probs_cube") @@ -611,8 +661,6 @@ plot.probs_cube <- function(x, ..., p <- .plot_probs(tile = tile, labels_plot = labels, palette = palette, - style = style, - n_colors = n_colors, rev = rev, scale = scale) @@ -628,9 +676,6 @@ plot.probs_cube <- function(x, ..., #' @param tile Tile to be plotted. #' @param labels Labels to plot (optional). #' @param palette RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") #' @param rev Reverse order of colors in palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @return A plot containing probabilities associated @@ -676,7 +721,6 @@ plot.probs_vector_cube <- function(x, ..., tile = x[["tile"]][[1]], labels = NULL, palette = "YlGn", - style = "cont", rev = FALSE, scale = 0.8) { .check_set_caller(".plot_probs_vector") @@ -703,7 +747,6 @@ plot.probs_vector_cube <- function(x, ..., p <- .plot_probs_vector(tile = tile, labels_plot = labels, palette = palette, - style = style, rev = rev, scale = scale) @@ -719,10 +762,6 @@ plot.probs_vector_cube <- function(x, ..., #' @param tile Tile to be plotted. #' @param labels Labels to plot (optional). #' @param palette RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse order of colors in palette? #' @param type Type of plot ("map" or "hist") #' @param scale Scale to plot map (0.4 to 1.0) @@ -757,8 +796,6 @@ plot.variance_cube <- function(x, ..., tile = x[["tile"]][[1]], labels = NULL, palette = "YlGnBu", - style = "cont", - n_colors = 10, rev = FALSE, type = "map", scale = 0.8) { @@ -788,8 +825,6 @@ plot.variance_cube <- function(x, ..., p <- .plot_probs(tile = tile, labels_plot = labels, palette = palette, - style = style, - n_colors = n_colors, rev = rev, scale = scale) } else { @@ -808,10 +843,6 @@ plot.variance_cube <- function(x, ..., #' @param ... Further specifications for \link{plot}. #' @param tile Tiles to be plotted. #' @param palette An RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") -#' @param n_colors Number of colors to be shown #' @param rev Reverse the color order in the palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @@ -844,10 +875,8 @@ plot.variance_cube <- function(x, ..., plot.uncertainty_cube <- function(x, ..., tile = x[["tile"]][[1]], palette = "RdYlGn", - style = "cont", rev = TRUE, - n_colors = 10, - scale = 0.8) { + scale = 1.0) { .check_set_caller(".plot_uncertainty_cube") # check for color_palette parameter (sits 1.4.1) dots <- list(...) @@ -868,6 +897,7 @@ plot.uncertainty_cube <- function(x, ..., # filter the cube tile <- .cube_filter_tiles(cube = x, tiles = tile[[1]]) band <- sits_bands(tile) + main_title <- paste0(.tile_collection(tile), " uncertainty ", band) # plot the data using tmap p <- .plot_false_color( tile = tile, @@ -877,8 +907,7 @@ plot.uncertainty_cube <- function(x, ..., seg_color = NULL, line_width = NULL, palette = palette, - style = style, - n_colors = n_colors, + main_title = main_title, rev = rev, scale = scale ) @@ -894,9 +923,6 @@ plot.uncertainty_cube <- function(x, ..., #' @param ... Further specifications for \link{plot}. #' @param tile Tile to be plotted. #' @param palette RColorBrewer palette -#' @param style Method to process the color scale -#' ("cont", "order", "quantile", "fisher", -#' "jenks", "log10") #' @param rev Reverse order of colors in palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @return A plot containing probabilities associated @@ -947,7 +973,6 @@ plot.uncertainty_cube <- function(x, ..., plot.uncertainty_vector_cube <- function(x, ..., tile = x[["tile"]][[1]], palette = "RdYlGn", - style = "cont", rev = TRUE, scale = 0.8) { .check_set_caller(".plot_uncertainty_vector_cube") @@ -969,13 +994,15 @@ plot.uncertainty_vector_cube <- function(x, ..., # filter the cube tile <- .cube_filter_tiles(cube = x, tiles = tile) - + # set the title + band <- sits_bands(tile) + main_title <- paste0(.tile_collection(tile), " uncertainty ", band) # plot the probs vector cube p <- .plot_uncertainty_vector(tile = tile, - palette = palette, - style = style, - rev = rev, - scale = scale) + palette = palette, + main_title = main_title, + rev = rev, + scale = scale) return(p) } diff --git a/inst/extdata/config_internals.yml b/inst/extdata/config_internals.yml index 2d58c2902..2ce5e84ec 100644 --- a/inst/extdata/config_internals.yml +++ b/inst/extdata/config_internals.yml @@ -215,9 +215,6 @@ gdal_presets : cog : overviews : [2, 4, 8, 16] method : "NEAREST" - block : - of : "GTiff" - co : ["COMPRESS=NONE", "BIGTIFF=NO"] # cannot be tiled! image : of : "GTiff" co : ["COMPRESS=LZW", "PREDICTOR=2", "BIGTIFF=YES", @@ -270,7 +267,7 @@ sar_cube_grey_colors: 128 # tmap configurations tmap: max_cells: 1e+06 - graticules_labels_size: 0.7 + graticules_labels_size: 0.8 legend_outside: False legend_outside_position: "right" legend_title_size: 1.0 diff --git a/inst/extdata/config_messages.yml b/inst/extdata/config_messages.yml index fe4cc178e..b2784898d 100644 --- a/inst/extdata/config_messages.yml +++ b/inst/extdata/config_messages.yml @@ -193,7 +193,7 @@ .plot_probs_vector: "some requested labels are not present in cube" .plot_raster_cube: "wrong input parameters - see example in documentation" .plot_raster_cube_tile: "tile is not included in the cube" -.plot_raster_cube_single_date: "plot handles one date at a time" +.plot_raster_cube_single_date: "plotting only the first requested date" .plot_raster_cube_date: "date is not part of the cube timeline" .plot_raster_false_color: "plotting false color image" .plot_sits: "wrong input parameters - see example in documentation" diff --git a/inst/extdata/sources/config_source_mpc.yml b/inst/extdata/sources/config_source_mpc.yml index 320b3abbc..74a7fad0f 100644 --- a/inst/extdata/sources/config_source_mpc.yml +++ b/inst/extdata/sources/config_source_mpc.yml @@ -193,7 +193,7 @@ sources: VV : &mspc_rtc_10m missing_value : -32768.0 minimum_value : 0 - maximum_value : 65534 + maximum_value : 65534.0 scale_factor : 1 offset_value : 0 resolution : 10 diff --git a/man/plot.probs_cube.Rd b/man/plot.probs_cube.Rd index 1d298d909..c0b3fa4fe 100644 --- a/man/plot.probs_cube.Rd +++ b/man/plot.probs_cube.Rd @@ -10,8 +10,6 @@ tile = x[["tile"]][[1]], labels = NULL, palette = "YlGn", - style = "cont", - n_colors = 10, rev = FALSE, scale = 0.8 ) @@ -27,12 +25,6 @@ \item{palette}{RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - -\item{n_colors}{Number of colors to be shown} - \item{rev}{Reverse order of colors in palette?} \item{scale}{Scale to plot map (0.4 to 1.0)} diff --git a/man/plot.probs_vector_cube.Rd b/man/plot.probs_vector_cube.Rd index 34184ee89..68db13e81 100644 --- a/man/plot.probs_vector_cube.Rd +++ b/man/plot.probs_vector_cube.Rd @@ -10,7 +10,6 @@ tile = x[["tile"]][[1]], labels = NULL, palette = "YlGn", - style = "cont", rev = FALSE, scale = 0.8 ) @@ -26,10 +25,6 @@ \item{palette}{RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - \item{rev}{Reverse order of colors in palette?} \item{scale}{Scale to plot map (0.4 to 1.0)} diff --git a/man/plot.raster_cube.Rd b/man/plot.raster_cube.Rd index 4baa98d40..dd96cece6 100644 --- a/man/plot.raster_cube.Rd +++ b/man/plot.raster_cube.Rd @@ -12,12 +12,10 @@ green = NULL, blue = NULL, tile = x[["tile"]][[1]], - date = NULL, + dates = NULL, palette = "RdYlGn", - style = "cont", - n_colors = 10, rev = FALSE, - scale = 1 + scale = 1.2 ) } \arguments{ @@ -35,32 +33,28 @@ \item{tile}{Tile to be plotted.} -\item{date}{Date to be plotted.} +\item{dates}{Dates to be plotted.} \item{palette}{An RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - -\item{n_colors}{Number of colors to be shown} - \item{rev}{Reverse the color order in the palette?} \item{scale}{Scale to plot map (0.4 to 1.0)} } \value{ A plot object with an RGB image - or a B/W image on a color - scale using the pallete + or a B/W image on a color scale } \description{ Plot RGB raster cube } \note{ -To see which colors are supported, please run \code{sits_colors()} - Use \code{scale} parameter for general output control. - If required, then set the other params individually +Use \code{scale} parameter for general output control. + The \code{dates} parameter indicates the date allows plotting of different dates when + a single band and three dates are provided, `sits` will plot a + multi-temporal RGB image for a single band (useful in the case of + SAR data). For RGB bands with multi-dates, multiple plots will be + produced. } \examples{ if (sits_run_examples()) { @@ -72,7 +66,8 @@ if (sits_run_examples()) { data_dir = data_dir ) # plot NDVI band of the second date date of the data cube - plot(cube, band = "NDVI", date = sits_timeline(cube)[1]) + plot(cube, band = "NDVI", dates = sits_timeline(cube)[1]) + # plot NDVI band as an RGB composite for the three bands } } \author{ diff --git a/man/plot.uncertainty_cube.Rd b/man/plot.uncertainty_cube.Rd index e46db4e6a..60235cb68 100644 --- a/man/plot.uncertainty_cube.Rd +++ b/man/plot.uncertainty_cube.Rd @@ -9,10 +9,8 @@ ..., tile = x[["tile"]][[1]], palette = "RdYlGn", - style = "cont", rev = TRUE, - n_colors = 10, - scale = 0.8 + scale = 1 ) } \arguments{ @@ -24,14 +22,8 @@ \item{palette}{An RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - \item{rev}{Reverse the color order in the palette?} -\item{n_colors}{Number of colors to be shown} - \item{scale}{Scale to plot map (0.4 to 1.0)} } \value{ diff --git a/man/plot.uncertainty_vector_cube.Rd b/man/plot.uncertainty_vector_cube.Rd index 381ad8a18..eb88749b6 100644 --- a/man/plot.uncertainty_vector_cube.Rd +++ b/man/plot.uncertainty_vector_cube.Rd @@ -9,7 +9,6 @@ ..., tile = x[["tile"]][[1]], palette = "RdYlGn", - style = "cont", rev = TRUE, scale = 0.8 ) @@ -23,10 +22,6 @@ \item{palette}{RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - \item{rev}{Reverse order of colors in palette?} \item{scale}{Scale to plot map (0.4 to 1.0)} diff --git a/man/plot.variance_cube.Rd b/man/plot.variance_cube.Rd index 1259f3111..7a0c5bc68 100644 --- a/man/plot.variance_cube.Rd +++ b/man/plot.variance_cube.Rd @@ -10,8 +10,6 @@ tile = x[["tile"]][[1]], labels = NULL, palette = "YlGnBu", - style = "cont", - n_colors = 10, rev = FALSE, type = "map", scale = 0.8 @@ -28,12 +26,6 @@ \item{palette}{RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - -\item{n_colors}{Number of colors to be shown} - \item{rev}{Reverse order of colors in palette?} \item{type}{Type of plot ("map" or "hist")} diff --git a/man/plot.vector_cube.Rd b/man/plot.vector_cube.Rd index e964c7ca4..15d117193 100644 --- a/man/plot.vector_cube.Rd +++ b/man/plot.vector_cube.Rd @@ -12,14 +12,12 @@ green = NULL, blue = NULL, tile = x[["tile"]][[1]], - date = NULL, + dates = NULL, seg_color = "black", line_width = 1, palette = "RdYlGn", - style = "cont", - n_colors = 10, rev = FALSE, - scale = 0.8 + scale = 1 ) } \arguments{ @@ -37,7 +35,7 @@ \item{tile}{Tile to be plotted.} -\item{date}{Date to be plotted.} +\item{dates}{Dates to be plotted.} \item{seg_color}{Color to show the segment boundaries} @@ -45,12 +43,6 @@ \item{palette}{An RColorBrewer palette} -\item{style}{Method to process the color scale -("cont", "order", "quantile", "fisher", - "jenks", "log10")} - -\item{n_colors}{Number of colors to be shown} - \item{rev}{Reverse the color order in the palette?} \item{scale}{Scale to plot map (0.4 to 1.0)} diff --git a/tests/testthat/test-cube-mpc.R b/tests/testthat/test-cube-mpc.R index 7085006cd..604869195 100644 --- a/tests/testthat/test-cube-mpc.R +++ b/tests/testthat/test-cube-mpc.R @@ -151,7 +151,7 @@ test_that("Creating Sentinel-1 RTC cubes from MPC", { cube_s1_rtc_reg <- sits_regularize( cube = cube_s1_rtc, - period = "P1M", + period = "P16D", res = 240, tiles = c("21LXJ", "21LYJ"), multicores = 1, diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 8932d2a62..d9775ce81 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -12,7 +12,6 @@ test_that("Plot Time Series and Images", { ) p2 <- plot(sits_patterns(cerrado_2classes)) - expect_equal(p2$guides$colour$title, "Bands") expect_equal(p2$theme$legend.position, "bottom") p3 <- cerrado_2classes |> @@ -22,7 +21,6 @@ test_that("Plot Time Series and Images", { expect_equal(as.Date(p3$data$Time[1]), as.Date("2000-09-13")) expect_equal(p3$data$Pattern[1], "Cerrado") expect_equal(p3$data$name[1], "EVI") - expect_equal(p3$guides$colour$title, "Bands") p4 <- cerrado_2classes |> sits_patterns() |> @@ -30,7 +28,6 @@ test_that("Plot Time Series and Images", { expect_equal(as.Date(p4$data$Time[1]), as.Date("2000-09-13")) expect_equal(p4$data$Pattern[1], "Cerrado") expect_equal(p4$data$name[1], "NDVI") - expect_equal(p4$guides$colour$title, "Bands") point_ndvi <- sits_select(point_mt_6bands, bands = "NDVI") set.seed(290356) @@ -49,7 +46,7 @@ test_that("Plot Time Series and Images", { progress = FALSE ) p <- plot(sinop, band = "NDVI", palette = "RdYlGn", rev = TRUE) - expect_equal(p$tm_shape$shp_name, "stars_obj") + expect_equal(p$tm_shape$shp_name, "rast") expect_equal(p$tm_raster$palette, "-RdYlGn") expect_equal(p$tm_grid$grid.projection, 4326)