From 703b1b0adbc214237de6ff7932206fea88d7c3e2 Mon Sep 17 00:00:00 2001 From: gilbertocamara Date: Mon, 11 Nov 2024 19:26:28 -0300 Subject: [PATCH] allow ROI when plotting raster cubes --- R/api_check.R | 20 ++++ R/api_gdal.R | 1 + R/api_plot_raster.R | 136 +++++++++++++++++--------- R/api_sf.R | 19 ++++ R/api_tmap.R | 19 ++-- R/api_tmap_v3.R | 6 +- R/api_tmap_v4.R | 21 +++- R/sits_plot.R | 161 +++++++++++++++++++++---------- R/zzz.R | 2 +- inst/extdata/config_messages.yml | 1 + man/plot.class_cube.Rd | 5 + man/plot.dem_cube.Rd | 5 + man/plot.probs_cube.Rd | 5 + man/plot.raster_cube.Rd | 7 +- man/plot.sar_cube.Rd | 7 +- man/plot.uncertainty_cube.Rd | 5 + man/plot.variance_cube.Rd | 9 +- 17 files changed, 309 insertions(+), 120 deletions(-) diff --git a/R/api_check.R b/R/api_check.R index 6e9c5bb2e..1b335315d 100644 --- a/R/api_check.R +++ b/R/api_check.R @@ -1872,6 +1872,26 @@ .check_that(setequal(names(x), c(.bbox_cols, "crs"))) return(invisible(x)) } +#' @title Check if roi is specified correcty +#' @name .check_roi +#' @param roi Region of interest +#' @return Called for side effects. +#' @keywords internal +#' @noRd +.check_roi <- function(roi) { + # set caller to show in errors + .check_set_caller(".check_roi") + # check vector is named + .check_names(roi) + # check that names are correct + roi_names <- names(roi) + names_ll <- c("lon_min", "lon_max", "lat_min", "lat_max") + names_x <- c("xmin", "xmax", "ymin", "ymax") + .check_that(all(names_ll %in% roi_names) || + all(names_x %in% roi_names) + ) + return(invisible(roi)) +} #' @title Check if roi or tiles are provided #' @name .check_roi_tiles #' @param roi Region of interest diff --git a/R/api_gdal.R b/R/api_gdal.R index bc745a5cf..981aa1e66 100644 --- a/R/api_gdal.R +++ b/R/api_gdal.R @@ -252,6 +252,7 @@ #' @noRd #' @param file Input file (with path) #' @param out_file Output files (with path) +#' @param roi_file File containing ROI in a GDAL readable format #' @param as_crs Output CRS (if different from input) #' @param miss_value Missing value #' @param data_type GDAL data type diff --git a/R/api_plot_raster.R b/R/api_plot_raster.R index ecc900a2d..1d2f08c92 100644 --- a/R/api_plot_raster.R +++ b/R/api_plot_raster.R @@ -4,16 +4,19 @@ #' @description plots a set of false color image #' @keywords internal #' @noRd -#' @param tile Tile to be plotted. -#' @param band Band to be plotted. -#' @param date Date to be plotted. -#' @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 -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param max_cog_size Maximum size of COG overviews (lines or columns) +#' @param tile Tile to be plotted. +#' @param band Band to be plotted. +#' @param date Date to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) +#' @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 +#' @param palette A sequential RColorBrewer palette +#' @param rev Reverse the color palette? +#' @param scale Scale to plot map (0.4 to 1.0) +#' @param max_cog_size Maximum size of COG overviews (lines or columns) #' @param first_quantile First quantile for stretching images #' @param last_quantile Last quantile for stretching images #' @param tmap_params List with tmap params for detailed plot control @@ -21,6 +24,7 @@ .plot_false_color <- function(tile, band, date, + roi, sf_seg, seg_color, line_width, @@ -31,6 +35,17 @@ first_quantile, last_quantile, tmap_params) { + + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .tile_filter_bands(bands = band) |> + .tile_filter_dates(dates = date) |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } + # select the file to be plotted bw_file <- .tile_path(tile, band, date) # size of data to be read @@ -44,12 +59,13 @@ bw_file <- .gdal_warp_file(bw_file, sizes) # read spatial raster file - probs_rast <- terra::rast(bw_file) + rast <- terra::rast(bw_file) + # scale the data - probs_rast <- probs_rast * band_scale + band_offset + rast <- rast * band_scale + band_offset # extract the values - vals <- terra::values(probs_rast) + vals <- terra::values(rast) # obtain the quantiles quantiles <- stats::quantile( vals, @@ -63,10 +79,10 @@ vals <- ifelse(vals > minq, vals, minq) vals <- ifelse(vals < maxq, vals, maxq) - terra::values(probs_rast) <- vals + terra::values(rast) <- vals p <- .tmap_false_color( - probs_rast = probs_rast, + rast = rast, band = band, sf_seg = sf_seg, seg_color = seg_color, @@ -89,12 +105,13 @@ #' @param tile Tile to be plotted. #' @param band Band to be plotted. #' @param dates Dates to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param palette A sequential RColorBrewer palette #' @param rev Reverse the color palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @param max_cog_size Maximum size of COG overviews (lines or columns) -#' @param first_quantile First quantile for stretching images -#' @param last_quantile Last quantile for stretching images #' @param tmap_params List with tmap params for detailed plot control #' #' @return A list of plot objects @@ -102,13 +119,21 @@ .plot_band_multidate <- function(tile, band, dates, + roi, palette, rev, scale, max_cog_size, - first_quantile, - last_quantile, tmap_params) { + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .tile_filter_bands(bands = band) |> + .tile_filter_dates(dates = dates) |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } # select the files to be plotted red_file <- .tile_path(tile, band, dates[[1]]) green_file <- .tile_path(tile, band, dates[[2]]) @@ -134,8 +159,6 @@ seg_color = NULL, line_width = NULL, scale = scale, - first_quantile = first_quantile, - last_quantile = last_quantile, tmap_params = tmap_params ) return(p) @@ -155,8 +178,6 @@ #' @param line_width Line width to plot the segments boundary #' @param scale Scale to plot map (0.4 to 1.0) #' @param max_cog_size Maximum size of COG overviews (lines or columns) -#' @param first_quantile First quantile for stretching images -#' @param last_quantile Last quantile for stretching images #' @param tmap_params List with tmap params for detailed plot control #' @return A plot object #' @@ -165,19 +186,28 @@ green, blue, date, + roi, sf_seg, seg_color, line_width, scale, max_cog_size, - first_quantile, - last_quantile, tmap_params) { + + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .tile_filter_bands(bands = c(red, green, blue)) |> + .tile_filter_dates(dates = date) |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } + # 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) @@ -199,8 +229,6 @@ seg_color = seg_color, line_width = line_width, scale = scale, - first_quantile = first_quantile, - last_quantile = last_quantile, tmap_params = tmap_params ) return(p) @@ -219,8 +247,6 @@ #' @param seg_color Color to use for segment borders #' @param line_width Line width to plot the segments boundary #' @param scale Scale to plot map (0.4 to 1.0) -#' @param first_quantile First quantile for stretching images -#' @param last_quantile Last quantile for stretching images #' @param tmap_params List with tmap params for detailed plot control #' @return A plot object #' @@ -233,8 +259,6 @@ seg_color, line_width, scale, - first_quantile, - last_quantile, tmap_params) { # read raster data as a stars object with separate RGB bands @@ -247,16 +271,6 @@ ), proxy = FALSE ) - # open RGB stars - rgb_st <- stars::st_rgb(rgb_st[, , , 1:3], - dimension = "band", - maxColorValue = max_value, - use_alpha = FALSE, - probs = c(first_quantile, - last_quantile), - stretch = TRUE - ) - p <- .tmap_rgb_color( rgb_st = rgb_st, scale = scale, @@ -274,6 +288,9 @@ #' @keywords internal #' @noRd #' @param tile Tile to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param legend Legend for the classes #' @param palette A sequential RColorBrewer palette #' @param scale Scale to plot the map @@ -281,8 +298,13 @@ #' @param tmap_params List with tmap params for detailed plot control #' @return A plot object #' -.plot_class_image <- function(tile, legend, palette, - scale, max_cog_size, tmap_params) { +.plot_class_image <- function(tile, + roi, + legend, + palette, + scale, + max_cog_size, + tmap_params) { # verifies if stars package is installed .check_require_packages("stars") # verifies if tmap package is installed @@ -304,6 +326,13 @@ label = unname(labels), color = unname(colors) ) + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } # size of data to be read sizes <- .tile_overview_size(tile = tile, max_cog_size) # select the image to be plotted @@ -338,24 +367,29 @@ #' @keywords internal #' @noRd #' @param tile Probs cube to be plotted +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param title Legend title #' @param labels_plot Labels to be plotted #' @param palette A sequential RColorBrewer palette #' @param rev Reverse the color palette? #' @param quantile Minimum quantile to plot #' @param scale Global scale for plot -#' @param tmap_params Parameters for tmap #' @param max_cog_size Maximum size of COG overviews (lines or columns) +#' @param window Spatial extent to plot in WGS 84 +#' (xmin, xmax, ymin, ymax) #' @return A plot object #' .plot_probs <- function(tile, + roi, labels_plot, palette, rev, scale, quantile, - tmap_params, - max_cog_size) { + max_cog_size, + tmap_params) { # set caller to show in errors .check_set_caller(".plot_probs") # verifies if stars package is installed @@ -374,8 +408,14 @@ } else { .check_that(all(labels_plot %in% labels)) } + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } # size of data to be read - max_size <- .conf("plot", "max_size") sizes <- .tile_overview_size(tile = tile, max_cog_size) # get the path probs_file <- .tile_path(tile) diff --git a/R/api_sf.R b/R/api_sf.R index 7174cb7b8..92b7ab254 100644 --- a/R/api_sf.R +++ b/R/api_sf.R @@ -270,3 +270,22 @@ # return only valid geometries sf_object[is_geometry_valid,] } +#' @title Create an sf polygon from a window +#' @name .sf_from_window +#' @keywords internal +#' @noRd +#' @param window named window in WGS 84 coordinates with +#' names (xmin, xmax, ymin, xmax) +#' @return sf polygon +#' +.sf_from_window <- function(window) { + df <- data.frame( + lon = c(window[["xmin"]], window[["xmin"]], window[["xmax"]], window[["xmax"]]), + lat = c(window[["ymin"]], window[["ymax"]], window[["ymax"]], window[["ymin"]]) + ) + polygon <- df |> + sf::st_as_sf(coords = c("lon", "lat"), crs = 4326) |> + dplyr::summarise(geometry = sf::st_combine(geometry)) |> + sf::st_cast("POLYGON") + polygon +} diff --git a/R/api_tmap.R b/R/api_tmap.R index d28193bc6..ef95b6619 100644 --- a/R/api_tmap.R +++ b/R/api_tmap.R @@ -4,7 +4,7 @@ #' @description plots a set of false color image #' @keywords internal #' @noRd -#' @param probs_rast terra spRast object. +#' @param rast terra spRast object. #' @param band Band to be plotted. #' @param sf_seg Segments (sf object) #' @param seg_color Color to use for segment borders @@ -14,7 +14,7 @@ #' @param scale Scale to plot map (0.4 to 1.0) #' @param tmap_params List with tmap params for detailed plot control #' @return A list of plot objects -.tmap_false_color <- function(probs_rast, +.tmap_false_color <- function(rast, band, sf_seg, seg_color, @@ -25,10 +25,10 @@ tmap_params){ if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(probs_rast) <- "tmap_v3" + class(rast) <- "tmap_v3" else - class(probs_rast) <- "tmap_v4" - UseMethod(".tmap_false_color", probs_rast) + class(rast) <- "tmap_v4" + UseMethod(".tmap_false_color", rast) } #' @title Plot a DEM #' @name .tmap_dem_map @@ -59,7 +59,7 @@ #' @description plots a RGB color image #' @keywords internal #' @noRd -#' @param st Stars object. +#' @param rgb_st RGB stars object. #' @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 @@ -67,8 +67,11 @@ #' @param tmap_params List with tmap params for detailed plot control #' @return A list of plot objects .tmap_rgb_color <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { + sf_seg, + seg_color, + line_width, + scale, + tmap_params) { if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") class(rgb_st) <- "tmap_v3" diff --git a/R/api_tmap_v3.R b/R/api_tmap_v3.R index f14665875..e0d96e93f 100644 --- a/R/api_tmap_v3.R +++ b/R/api_tmap_v3.R @@ -1,5 +1,5 @@ #' @export -.tmap_false_color.tmap_v3 <- function(probs_rast, +.tmap_false_color.tmap_v3 <- function(rast, band, sf_seg, seg_color, @@ -12,7 +12,7 @@ cols4all_name <- paste0("-", palette) # generate plot - p <- tmap::tm_shape(probs_rast) + + p <- tmap::tm_shape(rast) + tmap::tm_raster( palette = palette, title = band, @@ -69,7 +69,7 @@ return(p) } #' @export -.tmap_rgb_color.tmap_v3 <- function(rgb_st, +.tmap_rgb_color.tmap_v3 <- function(rgb_st, ..., sf_seg, seg_color, line_width, scale, tmap_params) { diff --git a/R/api_tmap_v4.R b/R/api_tmap_v4.R index fc498f578..e1f62c62c 100644 --- a/R/api_tmap_v4.R +++ b/R/api_tmap_v4.R @@ -1,5 +1,5 @@ #' @export -.tmap_false_color.tmap_v4 <- function(probs_rast, +.tmap_false_color.tmap_v4 <- function(rast, band, sf_seg, seg_color, @@ -20,7 +20,7 @@ else position <- tmap::tm_pos_in("left", "bottom") - p <- tmap::tm_shape(probs_rast) + + p <- tmap::tm_shape(rast) + tmap::tm_raster( col.scale = tmap::tm_scale_continuous( values = cols4all_name, @@ -93,11 +93,22 @@ } #' @export .tmap_rgb_color.tmap_v4 <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { + sf_seg, + seg_color, + line_width, + scale, + tmap_params) { p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) + - tmap::tm_raster() + + tmap::tm_rgb( + col = tmap::tm_vars(n = 3, multivariate = TRUE), + col.scale = tmap::tm_scale_rgb( + value.na = NA, + stretch = TRUE, + probs = c(0.05, 0.95), + maxColorValue = 1.0 + ) + ) + tmap::tm_graticules( labels_size = tmap_params[["graticules_labels_size"]] ) + diff --git a/R/sits_plot.R b/R/sits_plot.R index db93a5dfa..6b6454e3b 100644 --- a/R/sits_plot.R +++ b/R/sits_plot.R @@ -319,20 +319,23 @@ plot.predicted <- function(x, y, ..., #' #' @description Plot RGB raster cube #' -#' @param x Object of class "raster_cube". -#' @param ... Further specifications for \link{plot}. -#' @param band Band for plotting grey images. -#' @param red Band for red color. -#' @param green Band for green color. -#' @param blue Band for blue color. -#' @param tile Tile to be plotted. -#' @param dates Dates to be plotted. -#' @param palette An RColorBrewer palette -#' @param rev Reverse the color order in the palette? -#' @param scale Scale to plot map (0.4 to 1.0) +#' @param x Object of class "raster_cube". +#' @param ... Further specifications for \link{plot}. +#' @param band Band for plotting grey images. +#' @param red Band for red color. +#' @param green Band for green color. +#' @param blue Band for blue color. +#' @param tile Tile to be plotted. +#' @param dates Dates to be plotted +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) +#' @param palette An RColorBrewer palette +#' @param rev Reverse the color order in the palette? +#' @param scale Scale to plot map (0.4 to 1.0) #' @param first_quantile First quantile for stretching images #' @param last_quantile Last quantile for stretching images -#' @param max_cog_size Maximum size of COG overviews (lines or columns) +#' @param max_cog_size Maximum size of COG overviews (lines or columns) #' @param legend_position Where to place the legend (default = "outside") #' #' @return A plot object with an RGB image @@ -376,6 +379,7 @@ plot.raster_cube <- function(x, ..., blue = NULL, tile = x[["tile"]][[1]], dates = NULL, + roi = NULL, palette = "RdYlGn", rev = FALSE, scale = 1.0, @@ -385,6 +389,9 @@ plot.raster_cube <- function(x, ..., legend_position = "inside") { # check caller .check_set_caller(".plot_raster_cube") + # check roi + if (.has(roi)) + .check_roi(roi) # retrieve dots dots <- list(...) # deal with wrong parameter "date" @@ -439,8 +446,7 @@ plot.raster_cube <- function(x, ..., rev = rev, scale = scale, max_cog_size = max_cog_size, - first_quantile = first_quantile, - last_quantile = last_quantile, + roi = roi, tmap_params = tmap_params ) return(p) @@ -453,6 +459,7 @@ plot.raster_cube <- function(x, ..., tile = tile, band = band, date = dates[[1]], + roi = roi, sf_seg = NULL, seg_color = NULL, line_width = NULL, @@ -472,13 +479,12 @@ plot.raster_cube <- function(x, ..., green = green, blue = blue, date = dates[[1]], + roi = roi, sf_seg = NULL, seg_color = NULL, line_width = NULL, scale = scale, max_cog_size = max_cog_size, - first_quantile = first_quantile, - last_quantile = last_quantile, tmap_params = tmap_params ) } @@ -489,19 +495,22 @@ plot.raster_cube <- function(x, ..., #' @name plot.sar_cube #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} #' -#' @description Plot RGB raster cube +#' @description Plot SAR raster cube #' -#' @param x Object of class "raster_cube". -#' @param ... Further specifications for \link{plot}. -#' @param band Band for plotting grey images. -#' @param red Band for red color. -#' @param green Band for green color. -#' @param blue Band for blue color. -#' @param tile Tile to be plotted. -#' @param dates Dates to be plotted. -#' @param palette An RColorBrewer palette -#' @param rev Reverse the color order in the palette? -#' @param scale Scale to plot map (0.4 to 1.0) +#' @param x Object of class "raster_cube". +#' @param ... Further specifications for \link{plot}. +#' @param band Band for plotting grey images. +#' @param red Band for red color. +#' @param green Band for green color. +#' @param blue Band for blue color. +#' @param tile Tile to be plotted. +#' @param dates Dates to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) +#' @param palette An RColorBrewer palette +#' @param rev Reverse the color order in the palette? +#' @param scale Scale to plot map (0.4 to 1.0) #' @param first_quantile First quantile for stretching images #' @param last_quantile Last quantile for stretching images #' @param max_cog_size Maximum size of COG overviews (lines or columns) @@ -551,6 +560,7 @@ plot.sar_cube <- function(x, ..., blue = NULL, tile = x[["tile"]][[1]], dates = NULL, + roi = NULL, palette = "Greys", rev = FALSE, scale = 1.0, @@ -567,6 +577,7 @@ plot.sar_cube <- function(x, ..., blue = blue, tile = tile, dates = dates, + roi = roi, palette = palette, rev = rev, scale = scale, @@ -588,6 +599,9 @@ plot.sar_cube <- function(x, ..., #' @param ... Further specifications for \link{plot}. #' @param band Band for plotting grey images. #' @param tile Tile to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param palette An RColorBrewer palette #' @param rev Reverse the color order in the palette? #' @param scale Scale to plot map (0.4 to 1.0) @@ -625,6 +639,7 @@ plot.sar_cube <- function(x, ..., plot.dem_cube <- function(x, ..., band = "ELEVATION", tile = x[["tile"]][[1]], + roi = NULL, palette = "Spectral", rev = TRUE, scale = 1.0, @@ -632,6 +647,9 @@ plot.dem_cube <- function(x, ..., legend_position = "inside") { # check caller .check_set_caller(".plot_dem_cube") + # check roi + if (.has(roi)) + .check_roi(roi) # retrieve dots dots <- list(...) # get tmap params from dots @@ -653,6 +671,14 @@ plot.dem_cube <- function(x, ..., tile <- .cube_filter_tiles(cube = x, tiles = tile) # check band .check_that(band %in% .cube_bands(x)) + # crop using ROI + if (.has(roi)) { + tile <- tile |> + .tile_filter_bands(bands = band) |> + .crop(roi = roi, + output_dir = tempdir(), + progress = FALSE) + } # select the file to be plotted dem_file <- .tile_path(tile, band) # size of data to be read @@ -660,9 +686,9 @@ plot.dem_cube <- function(x, ..., # retrieve the overview if COG dem_file <- .gdal_warp_file(dem_file, sizes) # read SpatialRaster file - r <- terra::rast(dem_file) + rast <- terra::rast(dem_file) # plot the DEM - p <- .tmap_dem_map(r = r, + p <- .tmap_dem_map(r = rast, band = band, palette = palette, rev = rev, @@ -782,6 +808,7 @@ plot.vector_cube <- function(x, ..., tile = tile, band = band, date = dates[[1]], + roi = NULL, sf_seg = sf_seg, seg_color = seg_color, line_width = line_width, @@ -801,13 +828,12 @@ plot.vector_cube <- function(x, ..., green = green, blue = blue, date = dates[[1]], + roi = NULL, sf_seg = sf_seg, seg_color = seg_color, line_width = line_width, scale = scale, max_cog_size = max_cog_size, - first_quantile = first_quantile, - last_quantile = last_quantile, tmap_params = tmap_params ) } @@ -821,6 +847,9 @@ plot.vector_cube <- function(x, ..., #' @param x Object of class "probs_cube". #' @param ... Further specifications for \link{plot}. #' @param tile Tile to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param labels Labels to plot. #' @param palette RColorBrewer palette #' @param rev Reverse order of colors in palette? @@ -856,6 +885,7 @@ plot.vector_cube <- function(x, ..., #' plot.probs_cube <- function(x, ..., tile = x[["tile"]][[1]], + roi = NULL, labels = NULL, palette = "YlGn", rev = FALSE, @@ -874,6 +904,9 @@ plot.probs_cube <- function(x, ..., can_repeat = FALSE, msg = .conf("messages", ".plot_raster_cube_tile") ) + # check roi + if (.has(roi)) + .check_roi(roi) # get tmap params from dots dots <- list(...) tmap_params <- .tmap_params_set(dots, legend_position, legend_title) @@ -882,6 +915,7 @@ plot.probs_cube <- function(x, ..., # plot the probs cube p <- .plot_probs(tile = tile, + roi = roi, labels_plot = labels, palette = palette, rev = rev, @@ -987,6 +1021,9 @@ plot.probs_vector_cube <- function(x, ..., #' @param x Object of class "variance_cube". #' @param ... Further specifications for \link{plot}. #' @param tile Tile to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param labels Labels to plot. #' @param palette RColorBrewer palette #' @param rev Reverse order of colors in palette? @@ -994,10 +1031,11 @@ plot.probs_vector_cube <- function(x, ..., #' @param scale Scale to plot map (0.4 to 1.0) #' @param quantile Minimum quantile to plot #' @param max_cog_size Maximum size of COG overviews (lines or columns) + #' @param legend_position Where to place the legend (default = "inside") #' @param legend_title Title of legend (default = "probs") -#' @return A plot containing probabilities associated -#' to each class for each pixel. +#' @return A plot containing local variances associated to the +#' logit probability for each pixel and each class. #' #' #' @examples @@ -1025,6 +1063,7 @@ plot.probs_vector_cube <- function(x, ..., #' plot.variance_cube <- function(x, ..., tile = x[["tile"]][[1]], + roi = NULL, labels = NULL, palette = "YlGnBu", rev = FALSE, @@ -1044,6 +1083,9 @@ plot.variance_cube <- function(x, ..., can_repeat = FALSE, msg = .conf("messages", ".plot_raster_cube_tile") ) + # check roi + if (.has(roi)) + .check_roi(roi) # retrieve dots dots <- list(...) # get tmap params from dots @@ -1055,6 +1097,7 @@ plot.variance_cube <- function(x, ..., # plot the variance cube if (type == "map") { p <- .plot_probs(tile = tile, + roi = roi, labels_plot = labels, palette = palette, rev = rev, @@ -1074,15 +1117,18 @@ plot.variance_cube <- function(x, ..., #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} #' @description plots a probability cube using stars #' -#' @param x Object of class "probs_image". -#' @param ... Further specifications for \link{plot}. -#' @param tile Tiles to be plotted. -#' @param palette An RColorBrewer palette -#' @param rev Reverse the color order in the palette? +#' @param x Object of class "probs_image". +#' @param ... Further specifications for \link{plot}. +#' @param tile Tiles to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) +#' @param palette An RColorBrewer palette +#' @param rev Reverse the color order in the palette? #' @param scale Scale to plot map (0.4 to 1.0) #' @param first_quantile First quantile for stretching images #' @param last_quantile Last quantile for stretching images -#' @param max_cog_size Maximum size of COG overviews (lines or columns) +#' @param max_cog_size Maximum size of COG overviews (lines or columns) #' @param legend_position Where to place the legend (default = "inside") #' #' @return A plot object produced by the stars package @@ -1121,6 +1167,7 @@ plot.variance_cube <- function(x, ..., #' plot.uncertainty_cube <- function(x, ..., tile = x[["tile"]][[1]], + roi = NULL, palette = "RdYlGn", rev = TRUE, scale = 1.0, @@ -1129,6 +1176,9 @@ plot.uncertainty_cube <- function(x, ..., max_cog_size = 1024, legend_position = "inside") { .check_set_caller(".plot_uncertainty_cube") + # check roi + if (.has(roi)) + .check_roi(roi) # get tmap params from dots dots <- list(...) tmap_params <- .tmap_params_set(dots, legend_position) @@ -1150,6 +1200,7 @@ plot.uncertainty_cube <- function(x, ..., tile = tile, band = band, date = NULL, + roi = roi, sf_seg = NULL, seg_color = NULL, line_width = NULL, @@ -1263,12 +1314,15 @@ plot.uncertainty_vector_cube <- function(x, ..., #' @param y Ignored. #' @param ... Further specifications for \link{plot}. #' @param tile Tile to be plotted. +#' @param roi Spatial extent to plot in WGS 84 - named vector +#' with either (lon_min, lon_max, lat_min, lat_max) or +#' (xmin, xmax, ymin, ymax) #' @param title Title of the plot. #' @param legend Named vector that associates labels to colors. #' @param palette Alternative RColorBrewer palette #' @param scale Relative scale (0.4 to 1.0) of plot text -#' @param max_cog_size Maximum size of COG overviews (lines or columns) -#' @param legend_position Where to place the legend (default = "outside") +#' @param max_cog_size Maximum size of COG overviews (lines or columns) +#' @param legend_position Where to place the legend (default = "outside") #' #' @return A color map, where each pixel has the color #' associated to a label, as defined by the legend @@ -1309,6 +1363,7 @@ plot.uncertainty_vector_cube <- function(x, ..., #' plot.class_cube <- function(x, y, ..., tile = x[["tile"]][[1]], + roi = NULL, title = "Classified Image", legend = NULL, palette = "Spectral", @@ -1318,6 +1373,9 @@ plot.class_cube <- function(x, y, ..., stopifnot(missing(y)) # set caller to show in errors .check_set_caller(".plot_class_cube") + # check roi + if (.has(roi)) + .check_roi(roi) # check for color_palette parameter (sits 1.4.1) dots <- list(...) # get tmap params from dots @@ -1343,6 +1401,7 @@ plot.class_cube <- function(x, y, ..., # plot class cube .plot_class_image( tile = tile, + roi = roi, legend = legend, palette = palette, scale = scale, @@ -1356,15 +1415,15 @@ plot.class_cube <- function(x, y, ..., #' #' @description Plot vector classified cube #' -#' @param x Object of class "segments". -#' @param ... Further specifications for \link{plot}. -#' @param tile Tile to be plotted. -#' @param legend Named vector that associates labels to colors. -#' @param seg_color Segment color. -#' @param line_width Segment line width. -#' @param palette Alternative RColorBrewer palette -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param legend_position Where to place the legend (default = "outside") +#' @param x Object of class "segments". +#' @param ... Further specifications for \link{plot}. +#' @param tile Tile to be plotted. +#' @param legend Named vector that associates labels to colors. +#' @param seg_color Segment color. +#' @param line_width Segment line width. +#' @param palette Alternative RColorBrewer palette +#' @param scale Scale to plot map (0.4 to 1.0) +#' @param legend_position Where to place the legend (default = "outside") #' #' @return A plot object with an RGB image #' or a B/W image on a color diff --git a/R/zzz.R b/R/zzz.R index 357a9c7aa..adfcc199f 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -23,7 +23,7 @@ sits_env[["model_formula"]] <- "log" utils::globalVariables(c( ".x", ".y", ":=", # dplyr "self", "ctx", "super", "private", # torch - "uniform", "choice", "randint", + "uniform", "choice", "randint", "geometry", "normal", "lognormal", "loguniform", # sits_tuning_random "sar:frequency_band", "sar:instrument_mode", "sat:orbit_state" # S1 stac )) diff --git a/inst/extdata/config_messages.yml b/inst/extdata/config_messages.yml index c1cc10465..92c4c7dfe 100644 --- a/inst/extdata/config_messages.yml +++ b/inst/extdata/config_messages.yml @@ -75,6 +75,7 @@ .check_progress: "progress must be either TRUE or FALSE" .check_raster_cube_files: "Invalid data cube - missing files" .check_recovery: "recovery mode: data already exists. To produce new data, change output_dir or version" +.check_roi: "invalid specification of ROI - check function documentation" .check_roi_tiles: "either roi or tiles must be provided, but both are not allowed" .check_samples: "invalid samples - missing columns and/or data rows" .check_samples_cluster: "cluster id missing in samples - run sits_cluster() first" diff --git a/man/plot.class_cube.Rd b/man/plot.class_cube.Rd index 9e3d6775c..7bfa50c20 100644 --- a/man/plot.class_cube.Rd +++ b/man/plot.class_cube.Rd @@ -9,6 +9,7 @@ y, ..., tile = x[["tile"]][[1]], + roi = NULL, title = "Classified Image", legend = NULL, palette = "Spectral", @@ -26,6 +27,10 @@ \item{tile}{Tile to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{title}{Title of the plot.} \item{legend}{Named vector that associates labels to colors.} diff --git a/man/plot.dem_cube.Rd b/man/plot.dem_cube.Rd index 96f941125..f7ee0ff94 100644 --- a/man/plot.dem_cube.Rd +++ b/man/plot.dem_cube.Rd @@ -9,6 +9,7 @@ ..., band = "ELEVATION", tile = x[["tile"]][[1]], + roi = NULL, palette = "Spectral", rev = TRUE, scale = 1, @@ -25,6 +26,10 @@ \item{tile}{Tile to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{palette}{An RColorBrewer palette} \item{rev}{Reverse the color order in the palette?} diff --git a/man/plot.probs_cube.Rd b/man/plot.probs_cube.Rd index 583116e58..18a66813a 100644 --- a/man/plot.probs_cube.Rd +++ b/man/plot.probs_cube.Rd @@ -8,6 +8,7 @@ x, ..., tile = x[["tile"]][[1]], + roi = NULL, labels = NULL, palette = "YlGn", rev = FALSE, @@ -25,6 +26,10 @@ \item{tile}{Tile to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{labels}{Labels to plot.} \item{palette}{RColorBrewer palette} diff --git a/man/plot.raster_cube.Rd b/man/plot.raster_cube.Rd index 7e0c8dbf5..287dade82 100644 --- a/man/plot.raster_cube.Rd +++ b/man/plot.raster_cube.Rd @@ -13,6 +13,7 @@ blue = NULL, tile = x[["tile"]][[1]], dates = NULL, + roi = NULL, palette = "RdYlGn", rev = FALSE, scale = 1, @@ -37,7 +38,11 @@ \item{tile}{Tile to be plotted.} -\item{dates}{Dates to be plotted.} +\item{dates}{Dates to be plotted} + +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} \item{palette}{An RColorBrewer palette} diff --git a/man/plot.sar_cube.Rd b/man/plot.sar_cube.Rd index 05e7456c6..8046ba036 100644 --- a/man/plot.sar_cube.Rd +++ b/man/plot.sar_cube.Rd @@ -13,6 +13,7 @@ blue = NULL, tile = x[["tile"]][[1]], dates = NULL, + roi = NULL, palette = "Greys", rev = FALSE, scale = 1, @@ -39,6 +40,10 @@ \item{dates}{Dates to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{palette}{An RColorBrewer palette} \item{rev}{Reverse the color order in the palette?} @@ -58,7 +63,7 @@ A plot object with an RGB image or a B/W image on a color scale for SAR cubes } \description{ -Plot RGB raster cube +Plot SAR raster cube } \note{ Use \code{scale} parameter for general output control. diff --git a/man/plot.uncertainty_cube.Rd b/man/plot.uncertainty_cube.Rd index 94876ff4f..b90f4c06e 100644 --- a/man/plot.uncertainty_cube.Rd +++ b/man/plot.uncertainty_cube.Rd @@ -8,6 +8,7 @@ x, ..., tile = x[["tile"]][[1]], + roi = NULL, palette = "RdYlGn", rev = TRUE, scale = 1, @@ -24,6 +25,10 @@ \item{tile}{Tiles to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{palette}{An RColorBrewer palette} \item{rev}{Reverse the color order in the palette?} diff --git a/man/plot.variance_cube.Rd b/man/plot.variance_cube.Rd index 578ca838f..122d104d1 100644 --- a/man/plot.variance_cube.Rd +++ b/man/plot.variance_cube.Rd @@ -8,6 +8,7 @@ x, ..., tile = x[["tile"]][[1]], + roi = NULL, labels = NULL, palette = "YlGnBu", rev = FALSE, @@ -26,6 +27,10 @@ \item{tile}{Tile to be plotted.} +\item{roi}{Spatial extent to plot in WGS 84 - named vector +with either (lon_min, lon_max, lat_min, lat_max) or +(xmin, xmax, ymin, ymax)} + \item{labels}{Labels to plot.} \item{palette}{RColorBrewer palette} @@ -45,8 +50,8 @@ \item{legend_title}{Title of legend (default = "probs")} } \value{ -A plot containing probabilities associated - to each class for each pixel. +A plot containing local variances associated to the + logit probability for each pixel and each class. } \description{ plots a probability cube using stars