diff --git a/DESCRIPTION b/DESCRIPTION index 90a05b4ab..613cbc1af 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -202,6 +202,7 @@ Collate: 'api_tile.R' 'api_timeline.R' 'api_tmap.R' + 'api_tmap_v3.R' 'api_torch.R' 'api_torch_psetae.R' 'api_ts.R' diff --git a/NAMESPACE b/NAMESPACE index 67061e53e..741cf6547 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,11 @@ S3method("sits_labels<-",class_cube) S3method("sits_labels<-",default) S3method("sits_labels<-",probs_cube) S3method("sits_labels<-",sits) +S3method(.accuracy_get_validation,csv) +S3method(.accuracy_get_validation,data.frame) +S3method(.accuracy_get_validation,gpkg) +S3method(.accuracy_get_validation,sf) +S3method(.accuracy_get_validation,shp) S3method(.band_rename,raster_cube) S3method(.band_rename,sits) S3method(.check_samples,default) diff --git a/R/api_accuracy.R b/R/api_accuracy.R index 0d873e994..e457d8ff7 100644 --- a/R/api_accuracy.R +++ b/R/api_accuracy.R @@ -119,3 +119,71 @@ ) ) } +#' @title Get validation samples +#' @name .accuracy_get_validation +#' @description Retrieves and checks validation data +#' @keywords internal +#' @noRd +#' @param validation validation (CSV file, SHP file, SF object, data.frame) +#' @return samples for validation +#' +.accuracy_get_validation <- function(validation){ + # handle validation data as files + if (is.character(validation)) { + val_class <- tolower(.file_ext(validation)) + class(validation) <- c(val_class, validation) + } + UseMethod(".accuracy_get_validation", validation) +} +#' @export +.accuracy_get_validation.csv <- function(validation){ + # Read sample information from CSV file and put it in a tibble + valid_samples <- .csv_get_validation_samples(validation) + return(valid_samples) +} +#' @export +.accuracy_get_validation.shp <- function(validation){ + validation_sf <- sf::st_read(validation) + .check_that(all(sf::st_geometry_type(validation_sf) == "POINT")) + valid_samples <- .accuracy_get_validation(validation_sf) + return(valid_samples) +} +#' @export +.accuracy_get_validation.gpkg <- function(validation){ + validation_sf <- sf::st_read(validation) + .check_that(all(sf::st_geometry_type(validation_sf) == "POINT")) + valid_samples <- .accuracy_get_validation(validation_sf) + return(valid_samples) +} +#' @export +.accuracy_get_validation.sf <- function(validation){ + # Pre-condition - check for the required columns + .check_chr_contains(colnames(validation), c("label")) + # transform the `sf` object in a valid + valid_samples <- validation |> + dplyr::mutate( + geom = sf::st_geometry(validation) + ) |> + dplyr::mutate( + geom = sf::st_centroid(.data[["geom"]]) + ) |> + dplyr::mutate( + coords = sf::st_coordinates(.data[["geom"]]) + ) |> + dplyr::mutate( + longitude = .data[["coords"]][, 1], + latitude = .data[["coords"]][, 2] + ) |> + dplyr::select( + "label", "longitude", "latitude" + ) + return(valid_samples) +} +#' @export +`.accuracy_get_validation.data.frame` <- function(validation){ + # handle data frames + .check_chr_contains(colnames(validation), + c("label", "longitude", "latitude") + ) + return(validation) +} diff --git a/R/api_csv.R b/R/api_csv.R index 410e87dd3..f531d5e15 100644 --- a/R/api_csv.R +++ b/R/api_csv.R @@ -30,3 +30,30 @@ class(samples) <- c("sits", class(samples)) return(samples) } + +#' @title Transform a CSV with labelled points for accuracy assessmentinto a samples file +#' @name .csv_get_validation_samples +#' @author Gilberto Camara +#' @keywords internal +#' @noRd +#' @param csv_file CSV that describes the data to be retrieved. +#' @return A tibble with information the samples to be retrieved +#' +.csv_get_validation_samples <- function(csv_file) { + # read sample information from CSV file and put it in a tibble + samples <- tibble::as_tibble( + utils::read.csv( + file = csv_file, + stringsAsFactors = FALSE + ) + ) + # pre-condition - check if CSV file is correct + .check_samples(samples) + # select valid columns + samples <- dplyr::select( + samples, + c("longitude", "latitude", "label") + ) + class(samples) <- c("sits", class(samples)) + return(samples) +} diff --git a/R/api_samples.R b/R/api_samples.R index 7588aaddc..53da2b89d 100644 --- a/R/api_samples.R +++ b/R/api_samples.R @@ -350,6 +350,8 @@ dplyr::filter(.data[["label"]] == lab) |> dplyr::slice_sample(n = samples_label) }) + # transform to sf object + samples <- sf::st_as_sf(samples) return(samples) } diff --git a/R/api_tmap.R b/R/api_tmap.R index b4461b946..cfdc86198 100644 --- a/R/api_tmap.R +++ b/R/api_tmap.R @@ -30,51 +30,6 @@ class(st) <- "tmap_v3" UseMethod(".tmap_false_color", st) } -#' @export -.tmap_false_color.tmap_v3 <- function(st, - band, - sf_seg, - seg_color, - line_width, - palette, - rev, - scale, - tmap_params){ - - # reverse the color palette? - if (rev || palette == "Greys") - palette <- paste0("-", palette) - - # generate plot - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - 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 DEM #' @name .tmap_dem_map #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -97,39 +52,7 @@ class(r) <- "tmap_v3" UseMethod(".tmap_dem_map", r) } -# -#' @export -.tmap_dem_map.tmap_v3 <- function(r, band, - palette, rev, - scale, tmap_params){ - # reverse order of colors? - if (rev) - palette <- paste0("-", palette) - # generate plot - p <- tmap::tm_shape(r, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} + #' @title Plot a RGB color image with tmap #' @name .tmap_rgb_color #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -153,33 +76,6 @@ class(rgb_st) <- "tmap_v3" UseMethod(".tmap_rgb_color", rgb_st) } -#' @export -.tmap_rgb_color.tmap_v3 <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { - - # tmap params - labels_size <- tmap_params[["graticules_labels_size"]] - - p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) + - tmap::tm_raster() + - tmap::tm_graticules( - labels.size = labels_size - ) + - tmap::tm_layout( - scale = scale - ) + - tmap::tm_compass() - - # 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 probs image #' @name .tmap_probs_map #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -208,45 +104,24 @@ UseMethod(".tmap_probs_map", probs_st) } # -#' @export -#' -.tmap_probs_map.tmap_v3 <- function(probs_st, - labels, - labels_plot, - palette, - rev, - scale, - tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # select stars bands to be plotted - bds <- as.numeric(names(labels[labels %in% labels_plot])) +#' @title Plot a color image with legend +#' @name .tmap_class_map +#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} +#' @description plots a RGB color image +#' @keywords internal +#' @noRd +#' @param st Stars object. +#' @param colors Named vector with colors to be displayed +#' @param scale Scale to plot map (0.4 to 1.0) +#' @param tmap_params List with tmap params for detailed plot control +#' @return A plot object +.tmap_class_map <- function(st, colors, scale, tmap_params) { - p <- tmap::tm_shape(probs_st[, , , bds]) + - tmap::tm_raster( - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot] - ) + - tmap::tm_facets(sync = FALSE) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.outside = FALSE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) + if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") + class(st) <- "tmap_v3" + else + class(st) <- "tmap_v3" + UseMethod(".tmap_class_map", st) } #' @title Plot a vector probs map @@ -272,84 +147,9 @@ class(sf_seg) <- "tmap_v3" UseMethod(".tmap_vector_probs", sf_seg) } -#' @export -.tmap_vector_probs.tmap_v3 <- function(sf_seg, palette, rev, - labels, labels_plot, - scale, tmap_params){ - # revert the palette? - if (rev) { - palette <- paste0("-", palette) - } - # plot the segments - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - labels_plot, - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot]) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.1) - return(p) -} -#' @title Plot a color image with legend -#' @name .tmap_class_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st Stars object. -#' @param colors Named vector with colors to be displayed -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_class_map <- function(st, colors, scale, tmap_params) { - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(st) <- "tmap_v3" - else - class(st) <- "tmap_v3" - UseMethod(".tmap_class_map", st) -} -#' @export -.tmap_class_map.tmap_v3 <- function(st, colors, scale, tmap_params) { - - # plot using tmap - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - style = "cat", - labels = colors[["label"]], - palette = colors[["color"]] - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]], - ndiscr = 50 - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} #' @title Plot a vector class map #' @name .tmap_vector_class #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -368,36 +168,7 @@ class(sf_seg) <- "tmap_v3" UseMethod(".tmap_vector_class", sf_seg) } -# -#' @export -.tmap_vector_class.tmap_v3 <- function(sf_seg, - colors, - scale, - tmap_params){ - # plot the data using tmap - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = "class", - palette = colors - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - return(p) -} #' @title Plot a vector uncertainty map #' @name .tmap_vector_uncert #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -420,36 +191,6 @@ UseMethod(".tmap_vector_uncert", sf_seg) } -.tmap_vector_uncert.tmap_v3 <- function(sf_seg, palette, rev, - type, scale, tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # plot - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = type, - palette = palette - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - - return(suppressWarnings(p)) -} #' @title Prepare tmap params for dots value #' @name .tmap_params_set #' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} @@ -466,9 +207,7 @@ #' \item \code{legend_text_size}: relative size of legend text (default = 1.0) #' \item \code{legend_bg_color}: color of legend background (default = "white") #' \item \code{legend_bg_alpha}: legend opacity (default = 0.5) -#' \item \code{legend_width}: relative width of legend (default = 1.0) #' \item \code{legend_position}: 2D position of legend (default = c("left", "bottom")) -#' \item \code{legend_height}: relative height of legend (default = 1.0) #' } .tmap_params_set <- function(dots){ @@ -478,8 +217,6 @@ 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")) - legend_height <- as.numeric(.conf("plot", "legend_height")) - legend_width <- as.numeric(.conf("plot", "legend_width")) legend_position <- .conf("plot", "legend_position") if ("graticules_labels_size" %in% names(dots)) @@ -492,10 +229,6 @@ legend_title_size <- dots[["legend_title_size"]] if ("legend_text_size" %in% names(dots)) legend_text_size <- dots[["legend_text_size"]] - if ("legend_height" %in% names(dots)) - legend_height <- dots[["legend_height"]] - if ("legend_width" %in% names(dots)) - legend_width <- dots[["legend_width"]] if ("legend_position" %in% names(dots)) legend_position <- dots[["legend_position"]] @@ -505,8 +238,6 @@ "legend_bg_alpha" = legend_bg_alpha, "legend_title_size" = legend_title_size, "legend_text_size" = legend_text_size, - "legend_height" = legend_height, - "legend_width" = legend_width, "legend_position" = legend_position ) return(tmap_params) diff --git a/R/api_tmap_v3.R b/R/api_tmap_v3.R new file mode 100644 index 000000000..d994dbde6 --- /dev/null +++ b/R/api_tmap_v3.R @@ -0,0 +1,251 @@ +#' @export +.tmap_false_color.tmap_v3 <- function(st, + band, + sf_seg, + seg_color, + line_width, + palette, + rev, + scale, + tmap_params){ + if (rev || palette == "Greys") + cols4all_name <- paste0("-", palette) + + # generate plot + p <- tmap::tm_shape(st, raster.downsample = FALSE) + + tmap::tm_raster( + palette = palette, + title = band, + midpoint = NA, + style = "cont", + style.args = list(na.rm = TRUE) + ) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + 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) +} +# +#' @export +.tmap_dem_map.tmap_v3 <- function(r, band, + palette, rev, + scale, tmap_params){ + # reverse the color palette? + if (rev || palette == "Greys") + cols4all_name <- paste0("-", palette) + # generate plot + p <- tmap::tm_shape(r, raster.downsample = FALSE) + + tmap::tm_raster( + palette = palette, + title = band, + midpoint = NA, + style = "cont", + style.args = list(na.rm = TRUE) + ) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + return(p) +} +#' @export +.tmap_rgb_color.tmap_v3 <- function(rgb_st, + sf_seg, seg_color, line_width, + scale, tmap_params) { + + # tmap params + labels_size <- tmap_params[["graticules_labels_size"]] + + p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) + + tmap::tm_raster() + + tmap::tm_graticules( + labels.size = labels_size + ) + + tmap::tm_layout( + scale = scale + ) + + tmap::tm_compass() + + # include segments + if (.has(sf_seg)) { + p <- p + tmap::tm_shape(sf_seg) + + tmap::tm_borders(col = seg_color, lwd = line_width) + } + + return(p) +} +#' @export +#' +.tmap_probs_map.tmap_v3 <- function(probs_st, + labels, + labels_plot, + palette, + rev, + scale, + tmap_params){ + # reverse the color palette? + if (rev || palette == "Greys") + cols4all_name <- paste0("-", palette) + + # select stars bands to be plotted + bds <- as.numeric(names(labels[labels %in% labels_plot])) + + p <- tmap::tm_shape(probs_st[, , , bds]) + + tmap::tm_raster( + style = "cont", + palette = palette, + midpoint = NA, + title = labels[labels %in% labels_plot] + ) + + tmap::tm_facets(sync = FALSE) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.show = TRUE, + legend.outside = FALSE, + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + return(p) +} +#' @export +.tmap_class_map.tmap_v3 <- function(st, colors, scale, tmap_params) { + + # plot using tmap + p <- tmap::tm_shape(st, raster.downsample = FALSE) + + tmap::tm_raster( + style = "cat", + labels = colors[["label"]], + palette = colors[["color"]] + ) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]], + ndiscr = 50 + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + return(p) +} +#' @export +.tmap_vector_probs.tmap_v3 <- function(sf_seg, palette, rev, + labels, labels_plot, + scale, tmap_params){ + if (rev || palette == "Greys") + cols4all_name <- paste0("-", palette) + + # plot the segments + p <- tmap::tm_shape(sf_seg) + + tmap::tm_fill( + labels_plot, + style = "cont", + palette = palette, + midpoint = NA, + title = labels[labels %in% labels_plot]) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.show = TRUE, + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + + tmap::tm_borders(lwd = 0.1) + + return(p) +} +# +#' @export +.tmap_vector_class.tmap_v3 <- function(sf_seg, + colors, + scale, + tmap_params){ + # plot the data using tmap + p <- tmap::tm_shape(sf_seg) + + tmap::tm_fill( + col = "class", + palette = colors + ) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + + tmap::tm_borders(lwd = 0.2) + + return(p) +} +.tmap_vector_uncert.tmap_v3 <- function(sf_seg, palette, rev, + type, scale, tmap_params){ + # revert the palette + if (rev) { + palette <- paste0("-", palette) + } + # plot + p <- tmap::tm_shape(sf_seg) + + tmap::tm_fill( + col = type, + palette = palette + ) + + tmap::tm_graticules( + labels.size = tmap_params[["graticules_labels_size"]] + ) + + tmap::tm_compass() + + tmap::tm_layout( + legend.bg.color = tmap_params[["legend_bg_color"]], + legend.bg.alpha = tmap_params[["legend_bg_alpha"]], + legend.title.size = tmap_params[["legend_title_size"]], + legend.text.size = tmap_params[["legend_text_size"]], + legend.position = tmap_params[["legend_position"]], + scale = scale + ) + + tmap::tm_borders(lwd = 0.2) + + return(suppressWarnings(p)) +} diff --git a/R/sits_accuracy.R b/R/sits_accuracy.R index 65b6ae1c5..29f77249f 100644 --- a/R/sits_accuracy.R +++ b/R/sits_accuracy.R @@ -136,45 +136,9 @@ sits_accuracy.sits <- function(data, ...) { #' @export sits_accuracy.class_cube <- function(data, ..., validation) { .check_set_caller("sits_accuracy_class_cube") - # handle sample files in CSV format - if (is.character(validation)) { - if (tolower(.file_ext(validation)) == "csv") { - # Read sample information from CSV file and put it in a tibble - validation <- .csv_get_samples(validation) - } else if (tolower(.file_ext(validation)) == "shp") { - validation <- sf::st_read(validation) - .check_that(all(sf::st_geometry_type(validation) == "POINT")) - } else { - stop(.conf("messages", "sits_accuracy_class_cube_validation")) - } - } - # handle `sf` objects - if (inherits(validation, "sf")) { - # Pre-condition - check for the required columns - .check_chr_contains(colnames(validation), c( - "label", "start_date", "end_date" - )) - # transform the `sf` object in a valid - validation <- validation |> - dplyr::mutate( - geom = sf::st_geometry(validation) - ) |> - dplyr::mutate( - geom = sf::st_centroid(.data[["geom"]]) - ) |> - dplyr::mutate( - coords = sf::st_coordinates(.data[["geom"]]) - ) |> - dplyr::mutate( - longitude = .data[["coords"]][, 1], - latitude = .data[["coords"]][, 2] - ) |> - dplyr::select( - "start_date", "end_date", "label", "longitude", "latitude" - ) - } - # Pre-condition - check if validation samples are OK - validation <- .check_samples(validation) + # get the validation samples + valid_samples <- .accuracy_get_validation(validation) + # Find the labels of the cube labels_cube <- .cube_labels(data) # Create a list of (predicted, reference) values @@ -186,12 +150,12 @@ sits_accuracy.class_cube <- function(data, ..., validation) { .check_that(length(labelled_band) == 1) # get xy in cube projection xy_tb <- .proj_from_latlong( - longitude = validation[["longitude"]], - latitude = validation[["latitude"]], + longitude = valid_samples[["longitude"]], + latitude = valid_samples[["latitude"]], crs = .crs(tile) ) # join samples with XY values in a single tibble - points <- dplyr::bind_cols(validation, xy_tb) + points <- dplyr::bind_cols(valid_samples, xy_tb) # are there points to be retrieved from the cube? .check_that(nrow(points) != 0) # Filter the points inside the tile @@ -245,12 +209,12 @@ sits_accuracy.class_cube <- function(data, ..., validation) { # Create the error matrix error_matrix <- table( factor(pred_ref[["predicted"]], - levels = labels_cube, - labels = labels_cube + levels = labels_cube, + labels = labels_cube ), factor(pred_ref[["reference"]], - levels = labels_cube, - labels = labels_cube + levels = labels_cube, + labels = labels_cube ) ) # Get area for each class of the cube diff --git a/R/sits_active_learning.R b/R/sits_active_learning.R index c7b514578..2cdac8106 100644 --- a/R/sits_active_learning.R +++ b/R/sits_active_learning.R @@ -237,7 +237,7 @@ sits_uncertainty_sampling <- function(uncert_cube, #' data_dir <- system.file("extdata/raster/mod13q1", package = "sits") #' cube <- sits_cube( #' source = "BDC", -#' collection = "MOD13Q1-6", +#' collection = "MOD13Q1-6.1", #' data_dir = data_dir #' ) #' # build a random forest model diff --git a/R/sits_combine_predictions.R b/R/sits_combine_predictions.R index c8186d88a..2eded9d5b 100644 --- a/R/sits_combine_predictions.R +++ b/R/sits_combine_predictions.R @@ -45,7 +45,7 @@ #' data = cube, ml_model = rfor_model, output_dir = tempdir(), #' version = "rfor" #' ) -#' # create an XGBoost model +#' # create an SVM model #' svm_model <- sits_train(samples_modis_ndvi, sits_svm()) #' # classify a data cube using SVM model #' probs_svm_cube <- sits_classify( diff --git a/R/sits_cube.R b/R/sits_cube.R index 633c38f2a..39bec127b 100755 --- a/R/sits_cube.R +++ b/R/sits_cube.R @@ -217,18 +217,19 @@ #' # --- Access to Digital Earth Australia #' cube_deaustralia <- sits_cube( #' source = "DEAUSTRALIA", -#' collection = "LS8-GEOMEDIAN", -#' bands = c("B05", "B07"), +#' collection = "GA_LS8C_NBART_GM_CYEAR_3", +#' bands = c("BLUE", "GREEN", "RED", "NIR", "SWIR1"), #' roi = c( #' lon_min = 137.15991, #' lon_max = 138.18467, #' lat_min = -33.85777, #' lat_max = -32.56690 #' ), -#' start_date = "2016-01-01", -#' end_date = "2017-01-01" +#' start_date = "2018-01-01", +#' end_date = "2018-12-31" #' ) #' # --- Access to CDSE open data Sentinel 2/2A level 2 collection +#' # --- remember to set the appropriate environmental variables #' # It is recommended that `multicores` be used to accelerate the process. #' s2_cube <- sits_cube( #' source = "CDSE", @@ -240,6 +241,7 @@ #' ) #' #' ## --- Sentinel-1 SAR from CDSE +#' # --- remember to set the appropriate environmental variables #' roi_sar <- c("lon_min" = 33.546, "lon_max" = 34.999, #' "lat_min" = 1.427, "lat_max" = 3.726) #' s1_cube_open <- sits_cube( diff --git a/R/sits_mlp.R b/R/sits_mlp.R index 51da192d9..c2241ba08 100644 --- a/R/sits_mlp.R +++ b/R/sits_mlp.R @@ -66,7 +66,7 @@ #' data_dir <- system.file("extdata/raster/mod13q1", package = "sits") #' cube <- sits_cube( #' source = "BDC", -#' collection = "MOD13Q1-6", +#' collection = "MOD13Q1-6.1", #' data_dir = data_dir #' ) #' # classify a data cube diff --git a/R/sits_sample_functions.R b/R/sits_sample_functions.R index 3441bcecc..b626701dd 100644 --- a/R/sits_sample_functions.R +++ b/R/sits_sample_functions.R @@ -240,6 +240,7 @@ sits_reduce_imbalance <- function(samples, #' #' @param cube Classified cube #' @param expected_ua Expected values of user's accuracy +#' @param alloc_options Fixed sample allocation for rare classes #' @param std_err Standard error we would like to achieve #' @param rare_class_prop Proportional area limit for rare classes #' @@ -288,6 +289,7 @@ sits_reduce_imbalance <- function(samples, #' @export sits_sampling_design <- function(cube, expected_ua = 0.75, + alloc_options = c(100, 75, 50), std_err = 0.01, rare_class_prop = 0.1) { .check_set_caller("sits_sampling_design") @@ -325,18 +327,16 @@ sits_sampling_design <- function(cube, # find out the classes which are rare rare_classes <- prop[prop <= rare_class_prop] # Determine allocation possibilities - # allocate a sample size of 50–100 for rare classes # Given each allocation for rare classes (e.g, 100 samples) # allocate the rest of the sample size proportionally # to the other more frequent classes - alloc_three <- c(100, 75, 50) - alloc_options_lst <- purrr::map(alloc_three, function(al) { + alloc_options_lst <- purrr::map(alloc_options, function(al) { # determine the number of samples to be allocated # to more frequent classes samples_rare_classes <- al * length(rare_classes) remaining_samples <- sample_size - samples_rare_classes # allocate samples per class - # rare classes are given a fixed value (100, 75, 50) + # rare classes are given a fixed value (e.g., 100, 75, 50) # other classes are allocated proportionally to area alloc_class_lst <- purrr::map(prop, function(p) { if (p <= rare_class_prop) { @@ -462,7 +462,7 @@ sits_stratified_sampling <- function(cube, y = labels, by = "labels" ) |> - dplyr::select("labels", "label_id", alloc) |> + dplyr::select("labels", "label_id", dplyr::all_of(alloc)) |> dplyr::rename("label" = "labels") # include overhead samples_class[alloc] <- ceiling(unlist(samples_class[[alloc]]) * overhead) diff --git a/demo/00Index b/demo/00Index index 4974010e6..6145bbf1b 100644 --- a/demo/00Index +++ b/demo/00Index @@ -1,7 +1,4 @@ classify_cbers_bdc Classify a set of CBERS AWFI images in the Brazil Data Cube classify_deeplearning Classify MODIS image using deep learning -classify_raster_rfor Classify MODIS image using SVM -classify_ts Classify a time series using SVM -evaluate_samples_Kohonen Clustering and evaluation of samples using self-organizing maps ml_comparison Comparison of machine learning methods dl_comparison Comparison of deep learning methods diff --git a/demo/classify_cbers_bdc.R b/demo/classify_cbers_bdc.R index 70395cd3e..554db27e1 100644 --- a/demo/classify_cbers_bdc.R +++ b/demo/classify_cbers_bdc.R @@ -36,7 +36,7 @@ end_date <- timeline_samples[length(timeline_samples)] # define a CBERS data cube using the Brazil Data Cube cbers_cube <- sits_cube( source = "BDC", - collection = "CB4-16D-2", + collection = "CBERS-WFI-16D", bands = bands, tiles = "007004", start_date = start_date, diff --git a/demo/classify_deeplearning.R b/demo/classify_deeplearning.R index 273dd674a..eec2810e7 100644 --- a/demo/classify_deeplearning.R +++ b/demo/classify_deeplearning.R @@ -30,7 +30,7 @@ dl_model <- sits_train( data_dir <- system.file("extdata/sinop", package = "sitsdata") sinop <- sits_cube( source = "BDC", - collection = "MOD13Q1-6", + collection = "MOD13Q1-6.1", data_dir = data_dir ) @@ -55,8 +55,5 @@ sinop_label <- sits_label_classification( output_dir = tempdir() ) -# plot the smoothed image -plot(sinop_bayes) - # plot the classified image plot(sinop_label) diff --git a/demo/classify_raster_rfor.R b/demo/classify_raster_rfor.R deleted file mode 100644 index 49bc7962c..000000000 --- a/demo/classify_raster_rfor.R +++ /dev/null @@ -1,90 +0,0 @@ -# This is a demonstration of classification of a raster area -# The raster image is a MODIS data set covering the municipality of Sinop -# with two bands (NDVI and EVI) using MODIS collection 5 data - -# load the sitsdata library -if (!requireNamespace("sitsdata", quietly = TRUE)) { - stop("Please install package sitsdata\n", - "Please call devtools::install_github('e-sensing/sitsdata')", - call. = FALSE - ) -} - -# load the sitsdata library -library(sitsdata) - -data("samples_matogrosso_mod13q1") - -samples_ndvi_evi <- sits_select( - data = samples_matogrosso_mod13q1, - bands = c("NDVI", "EVI") -) - -# build the classification model -rfor_model <- sits_train( - samples_ndvi_evi, - ml_method = sits_rfor() -) - -# Read ndvi and evi data from the sitsdata package -# create a data cube to be classified -# Cube MOD13Q1 images from the Sinop region in Mato Grosso (Brazil) -data_dir <- system.file("extdata/sinop", package = "sitsdata") -sinop <- sits_cube( - source = "BDC", - collection = "MOD13Q1-6", - data_dir = data_dir, - delim = "_", - parse_info = c("X1", "tile", "band", "date") -) - -# classify the raster image -sinop_probs <- sits_classify( - data = sinop, - ml_model = rfor_model, - memsize = 8, - multicores = 2, - output_dir = tempdir() -) - -# smoothen with bayesian filter -sinop_bayes <- sits_smooth( - cube = sinop_probs, - memsize = 8, - multicores = 2, - output_dir = tempdir() -) -# calculate uncertainty -sinop_uncert <- sits_uncertainty( - cube = sinop_bayes, - type = "entropy", - memsize = 8, - multicores = 2, - output_dir = tempdir() -) -# calculate uncertainty -sinop_uncert_m <- sits_uncertainty( - cube = sinop_bayes, - type = "least", - memsize = 8, - multicores = 2, - output_dir = tempdir() -) -# calculate uncertainty -sinop_uncert_margin <- sits_uncertainty( - cube = sinop_bayes, - type = "margin", - memsize = 8, - multicores = 2, - output_dir = tempdir() -) -# label the classified image -sinop_label <- sits_label_classification( - cube = sinop_bayes, - memsize = 8, - multicores = 2, - output_dir = tempdir() -) - -# plot the smoothened image -plot(sinop_label) diff --git a/demo/classify_ts.R b/demo/classify_ts.R deleted file mode 100644 index 10ef52288..000000000 --- a/demo/classify_ts.R +++ /dev/null @@ -1,31 +0,0 @@ -# satellite image time series package (SITS) -# example of the classification of a time series -library(sits) - -# In this example, we are going to train a ML model -# and then will classify a point - -# use a sample with the bands "ndvi", "evi", "nir", and "mir" -# select a random forest model -rfor_model <- sits_train( - samples = samples_modis_ndvi, - ml_method = sits_rfor() -) - -# Retrieve a time series -data("point_mt_6bands") - -# select the bands "ndvi", "evi", "nir", and "mir" -point_tb <- sits_select( - data = point_mt_6bands, - bands = "NDVI" -) - -# classify the point -class_tb <- sits_classify( - data = point_tb, - ml_model = rfor_model -) - -# plot the classification -plot(class_tb, bands = "NDVI") diff --git a/demo/dl_comparison.R b/demo/dl_comparison.R index 6dd4e654f..9b8dda58b 100644 --- a/demo/dl_comparison.R +++ b/demo/dl_comparison.R @@ -39,15 +39,4 @@ acc_tc[["name"]] <- "TempCNN" results[[length(results) + 1]] <- acc_tc -# Deep Learning - ResNet -print("== Accuracy Assessment = ResNet =======================") -acc_rn <- sits_kfold_validate( - samples_matogrosso_mod13q1, - folds = 5, - ml_method = sits_resnet() -) -acc_rn[["name"]] <- "ResNet" - -results[[length(results) + 1]] <- acc_rn - sits_to_xlsx(results, file = file.path(tempdir(), "/accuracy_mato_grosso_dl.xlsx")) diff --git a/demo/evaluate_samples_Kohonen.R b/demo/evaluate_samples_Kohonen.R deleted file mode 100644 index 0b4c47266..000000000 --- a/demo/evaluate_samples_Kohonen.R +++ /dev/null @@ -1,31 +0,0 @@ -# satellite image time series package (SITS) -# example of clustering using self-organizin maps -library(sits) - -# load the sitsdata library -if (!requireNamespace("sitsdata", quietly = TRUE)) { - stop("Please install package sitsdata\n", - "Please call devtools::install_github('e-sensing/sitsdata')", - call. = FALSE - ) -} -library(sitsdata) -data("samples_cerrado_mod13q1") - -# Clustering time series samples using self-organizing maps -som_map <- - sits_som_map( - samples_cerrado_mod13q1, - grid_xdim = 12, - grid_ydim = 12, - alpha = 1, - distance = "euclidean" - ) - -plot(som_map) - -# Remove samples that have bad quality -new_samples <- sits_som_clean_samples(som_map) - -cluster_purity <- sits_som_evaluate_cluster(som_map) -plot(cluster_purity) diff --git a/inst/extdata/tmap/api_tmap_v3.R b/inst/extdata/tmap/api_tmap_v3.R deleted file mode 100644 index b4461b946..000000000 --- a/inst/extdata/tmap/api_tmap_v3.R +++ /dev/null @@ -1,514 +0,0 @@ -#' @title Plot a false color image with tmap -#' @name .tmap_false_color -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a set of false color image -#' @keywords internal -#' @noRd -#' @param st stars object. -#' @param band Band 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 tmap_params List with tmap params for detailed plot control -#' @return A list of plot objects -.tmap_false_color <- function(st, - band, - sf_seg, - seg_color, - line_width, - palette, - rev, - scale, - tmap_params){ - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(st) <- "tmap_v3" - else - class(st) <- "tmap_v3" - UseMethod(".tmap_false_color", st) -} -#' @export -.tmap_false_color.tmap_v3 <- function(st, - band, - sf_seg, - seg_color, - line_width, - palette, - rev, - scale, - tmap_params){ - - # reverse the color palette? - if (rev || palette == "Greys") - palette <- paste0("-", palette) - - # generate plot - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - 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 DEM -#' @name .tmap_dem_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param r Raster object. -#' @param band Band of DEM cube -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_dem_map <- function(r, band, - palette, rev, - scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(r) <- "tmap_v3" - else - class(r) <- "tmap_v3" - UseMethod(".tmap_dem_map", r) -} -# -#' @export -.tmap_dem_map.tmap_v3 <- function(r, band, - palette, rev, - scale, tmap_params){ - # reverse order of colors? - if (rev) - palette <- paste0("-", palette) - # generate plot - p <- tmap::tm_shape(r, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} -#' @title Plot a RGB color image with tmap -#' @name .tmap_rgb_color -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st 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 -#' @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_rgb_color <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(rgb_st) <- "tmap_v3" - else - class(rgb_st) <- "tmap_v3" - UseMethod(".tmap_rgb_color", rgb_st) -} -#' @export -.tmap_rgb_color.tmap_v3 <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { - - # tmap params - labels_size <- tmap_params[["graticules_labels_size"]] - - p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) + - tmap::tm_raster() + - tmap::tm_graticules( - labels.size = labels_size - ) + - tmap::tm_layout( - scale = scale - ) + - tmap::tm_compass() - - # 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 probs image -#' @name .tmap_probs_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st Stars object. -#' @param labels Class labels -#' @param labels_plot Class labels to be plotted -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_probs_map <- function(probs_st, - labels, - labels_plot, - palette, - rev, - scale, - tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(probs_st) <- "tmap_v3" - else - class(probs_st) <- "tmap_v3" - UseMethod(".tmap_probs_map", probs_st) -} -# -#' @export -#' -.tmap_probs_map.tmap_v3 <- function(probs_st, - labels, - labels_plot, - palette, - rev, - scale, - tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # select stars bands to be plotted - bds <- as.numeric(names(labels[labels %in% labels_plot])) - - p <- tmap::tm_shape(probs_st[, , , bds]) + - tmap::tm_raster( - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot] - ) + - tmap::tm_facets(sync = FALSE) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.outside = FALSE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} - -#' @title Plot a vector probs map -#' @name .tmap_vector_probs -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param labels Class labels -#' @param labels_plot Class labels to be plotted -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Tmap parameters -#' @return A plot object -.tmap_vector_probs <- function(sf_seg, palette, rev, - labels, labels_plot, - scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v3" - UseMethod(".tmap_vector_probs", sf_seg) -} -#' @export -.tmap_vector_probs.tmap_v3 <- function(sf_seg, palette, rev, - labels, labels_plot, - scale, tmap_params){ - # revert the palette? - if (rev) { - palette <- paste0("-", palette) - } - - # plot the segments - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - labels_plot, - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot]) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.1) - - return(p) -} -#' @title Plot a color image with legend -#' @name .tmap_class_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st Stars object. -#' @param colors Named vector with colors to be displayed -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_class_map <- function(st, colors, scale, tmap_params) { - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(st) <- "tmap_v3" - else - class(st) <- "tmap_v3" - UseMethod(".tmap_class_map", st) -} -#' @export -.tmap_class_map.tmap_v3 <- function(st, colors, scale, tmap_params) { - - # plot using tmap - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - style = "cat", - labels = colors[["label"]], - palette = colors[["color"]] - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]], - ndiscr = 50 - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} -#' @title Plot a vector class map -#' @name .tmap_vector_class -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf object. -#' @param colors Named vector with colors to be displayed -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Parameters to control tmap output -#' @return A plot object -.tmap_vector_class <- function(sf_seg, colors, scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v3" - UseMethod(".tmap_vector_class", sf_seg) -} -# -#' @export -.tmap_vector_class.tmap_v3 <- function(sf_seg, - colors, - scale, - tmap_params){ - # plot the data using tmap - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = "class", - palette = colors - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - - return(p) -} -#' @title Plot a vector uncertainty map -#' @name .tmap_vector_uncert -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param type Uncertainty type -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Tmap parameters -#' @return A plot object -.tmap_vector_uncert <- function(sf_seg, palette, rev, - type, scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v3" - UseMethod(".tmap_vector_uncert", sf_seg) - -} -.tmap_vector_uncert.tmap_v3 <- function(sf_seg, palette, rev, - type, scale, tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # plot - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = type, - palette = palette - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - - return(suppressWarnings(p)) -} -#' @title Prepare tmap params for dots value -#' @name .tmap_params_set -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @noRd -#' @keywords internal -#' @param dots params passed on dots -#' @description The following optional parameters are available to allow for detailed -#' control over the plot output: -#' \itemize{ -#' \item \code{first_quantile}: 1st quantile for stretching images (default = 0.05) -#' \item \code{last_quantile}: last quantile for stretching images (default = 0.95) -#' \item \code{graticules_labels_size}: size of coordinates labels (default = 0.8) -#' \item \code{legend_title_size}: relative size of legend title (default = 1.0) -#' \item \code{legend_text_size}: relative size of legend text (default = 1.0) -#' \item \code{legend_bg_color}: color of legend background (default = "white") -#' \item \code{legend_bg_alpha}: legend opacity (default = 0.5) -#' \item \code{legend_width}: relative width of legend (default = 1.0) -#' \item \code{legend_position}: 2D position of legend (default = c("left", "bottom")) -#' \item \code{legend_height}: relative height of legend (default = 1.0) -#' } -.tmap_params_set <- function(dots){ - - # tmap params - graticules_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")) - legend_height <- as.numeric(.conf("plot", "legend_height")) - legend_width <- as.numeric(.conf("plot", "legend_width")) - legend_position <- .conf("plot", "legend_position") - - if ("graticules_labels_size" %in% names(dots)) - graticules_labels_size <- dots[["graticules_labels_size"]] - if ("legend_bg_color" %in% names(dots)) - legend_bg_color <- dots[["legend_bg_color"]] - if ("legend_bg_alpha" %in% names(dots)) - legend_bg_alpha <- dots[["legend_bg_alpha"]] - if ("legend_title_size" %in% names(dots)) - legend_title_size <- dots[["legend_title_size"]] - if ("legend_text_size" %in% names(dots)) - legend_text_size <- dots[["legend_text_size"]] - if ("legend_height" %in% names(dots)) - legend_height <- dots[["legend_height"]] - if ("legend_width" %in% names(dots)) - legend_width <- dots[["legend_width"]] - if ("legend_position" %in% names(dots)) - legend_position <- dots[["legend_position"]] - - tmap_params <- list( - "graticules_labels_size" = graticules_labels_size, - "legend_bg_color" = legend_bg_color, - "legend_bg_alpha" = legend_bg_alpha, - "legend_title_size" = legend_title_size, - "legend_text_size" = legend_text_size, - "legend_height" = legend_height, - "legend_width" = legend_width, - "legend_position" = legend_position - ) - return(tmap_params) -} - diff --git a/inst/extdata/tmap/api_tmap_v4.R b/inst/extdata/tmap/api_tmap_v4.R index 4b3c87ae8..9a46be692 100644 --- a/inst/extdata/tmap/api_tmap_v4.R +++ b/inst/extdata/tmap/api_tmap_v4.R @@ -1,80 +1,3 @@ -#' @title Plot a false color image with tmap -#' @name .tmap_false_color -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a set of false color image -#' @keywords internal -#' @noRd -#' @param st stars object. -#' @param band Band 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 tmap_params List with tmap params for detailed plot control -#' @return A list of plot objects -.tmap_false_color <- function(st, - band, - sf_seg, - seg_color, - line_width, - palette, - rev, - scale, - tmap_params){ - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(st) <- "tmap_v3" - else - class(st) <- "tmap_v4" - UseMethod(".tmap_false_color", st) -} -#' @export -.tmap_false_color.tmap_v3 <- function(st, - band, - sf_seg, - seg_color, - line_width, - palette, - rev, - scale, - tmap_params){ - - # reverse the color palette? - if (rev || palette == "Greys") - palette <- paste0("-", palette) - - # generate plot - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - 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) -} #' @export .tmap_false_color.tmap_v4 <- function(st, band, @@ -122,61 +45,6 @@ return(p) } -#' @title Plot a DEM -#' @name .tmap_dem_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param r Raster object. -#' @param band Band of DEM cube -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_dem_map <- function(r, band, - palette, rev, - scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(r) <- "tmap_v3" - else - class(r) <- "tmap_v4" - UseMethod(".tmap_dem_map", r) -} -# -#' @export -.tmap_dem_map.tmap_v3 <- function(r, band, - palette, rev, - scale, tmap_params){ - # reverse order of colors? - if (rev) - palette <- paste0("-", palette) - # generate plot - p <- tmap::tm_shape(r, raster.downsample = FALSE) + - tmap::tm_raster( - palette = palette, - title = band, - midpoint = NA, - style = "cont", - style.args = list(na.rm = TRUE) - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} #' @export #' .tmap_dem_map.tmap_v4 <- function(r, band, @@ -212,55 +80,6 @@ ) return(p) } -#' @title Plot a RGB color image with tmap -#' @name .tmap_rgb_color -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st 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 -#' @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_rgb_color <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(rgb_st) <- "tmap_v3" - else - class(rgb_st) <- "tmap_v4" - UseMethod(".tmap_rgb_color", rgb_st) -} -#' @export -.tmap_rgb_color.tmap_v3 <- function(rgb_st, - sf_seg, seg_color, line_width, - scale, tmap_params) { - - # tmap params - labels_size <- tmap_params[["graticules_labels_size"]] - - p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) + - tmap::tm_raster() + - tmap::tm_graticules( - labels.size = labels_size - ) + - tmap::tm_layout( - scale = scale - ) + - tmap::tm_compass() - - # include segments - if (.has(sf_seg)) { - p <- p + tmap::tm_shape(sf_seg) + - tmap::tm_borders(col = seg_color, lwd = line_width) - } - - return(p) -} #' @export .tmap_rgb_color.tmap_v4 <- function(rgb_st, sf_seg, seg_color, line_width, @@ -283,74 +102,6 @@ } return(p) } - -#' @title Plot a probs image -#' @name .tmap_probs_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st Stars object. -#' @param labels Class labels -#' @param labels_plot Class labels to be plotted -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_probs_map <- function(probs_st, - labels, - labels_plot, - palette, - rev, - scale, - tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(probs_st) <- "tmap_v3" - else - class(probs_st) <- "tmap_v4" - UseMethod(".tmap_probs_map", probs_st) -} -# -#' @export -#' -.tmap_probs_map.tmap_v3 <- function(probs_st, - labels, - labels_plot, - palette, - rev, - scale, - tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # select stars bands to be plotted - bds <- as.numeric(names(labels[labels %in% labels_plot])) - - p <- tmap::tm_shape(probs_st[, , , bds]) + - tmap::tm_raster( - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot] - ) + - tmap::tm_facets(sync = FALSE) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.outside = FALSE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - scale = scale - ) - return(p) -} # #' @export #' @@ -396,64 +147,6 @@ scale = scale ) } -#' @title Plot a vector probs map -#' @name .tmap_vector_probs -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param labels Class labels -#' @param labels_plot Class labels to be plotted -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Tmap parameters -#' @return A plot object -.tmap_vector_probs <- function(sf_seg, palette, rev, - labels, labels_plot, - scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v4" - UseMethod(".tmap_vector_probs", sf_seg) -} -#' @export -.tmap_vector_probs.tmap_v3 <- function(sf_seg, palette, rev, - labels, labels_plot, - scale, tmap_params){ - # revert the palette? - if (rev) { - palette <- paste0("-", palette) - } - - # plot the segments - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - labels_plot, - style = "cont", - palette = palette, - midpoint = NA, - title = labels[labels %in% labels_plot]) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_facets(sync = FALSE) + - tmap::tm_compass() + - tmap::tm_layout( - legend.show = TRUE, - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.1) - - return(p) -} #' @export .tmap_vector_probs.tmap_v4 <- function(sf_seg, palette, rev, labels, labels_plot, @@ -489,50 +182,6 @@ ) return(p) } -#' @title Plot a color image with legend -#' @name .tmap_class_map -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param st Stars object. -#' @param colors Named vector with colors to be displayed -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params List with tmap params for detailed plot control -#' @return A plot object -.tmap_class_map <- function(st, colors, scale, tmap_params) { - - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(st) <- "tmap_v3" - else - class(st) <- "tmap_v4" - UseMethod(".tmap_class_map", st) -} -#' @export -.tmap_class_map.tmap_v3 <- function(st, colors, scale, tmap_params) { - - # plot using tmap - p <- tmap::tm_shape(st, raster.downsample = FALSE) + - tmap::tm_raster( - style = "cat", - labels = colors[["label"]], - palette = colors[["color"]] - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]], - ndiscr = 50 - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) - return(p) -} #' @export .tmap_class_map.tmap_v4 <- function(st, colors, scale, tmap_params) { @@ -561,54 +210,6 @@ ) return(p) } -#' @title Plot a vector class map -#' @name .tmap_vector_class -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf object. -#' @param colors Named vector with colors to be displayed -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Parameters to control tmap output -#' @return A plot object -.tmap_vector_class <- function(sf_seg, colors, scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v4" - UseMethod(".tmap_vector_class", sf_seg) -} -# -#' @export -.tmap_vector_class.tmap_v3 <- function(sf_seg, - colors, - scale, - tmap_params){ - # plot the data using tmap - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = "class", - palette = colors - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - - return(p) -} #' @export .tmap_vector_class.tmap_v4 <- function(sf_seg, colors, @@ -645,59 +246,7 @@ return(p) } - -#' @title Plot a vector uncertainty map -#' @name .tmap_vector_uncert -#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br} -#' @description plots a RGB color image -#' @keywords internal -#' @noRd -#' @param sf_seg sf -#' @param palette A sequential RColorBrewer palette -#' @param rev Reverse the color palette? -#' @param type Uncertainty type -#' @param scale Scale to plot map (0.4 to 1.0) -#' @param tmap_params Tmap parameters -#' @return A plot object -.tmap_vector_uncert <- function(sf_seg, palette, rev, - type, scale, tmap_params){ - if (as.numeric_version(utils::packageVersion("tmap")) < "3.9") - class(sf_seg) <- "tmap_v3" - else - class(sf_seg) <- "tmap_v4" - UseMethod(".tmap_vector_uncert", sf_seg) - -} -.tmap_vector_uncert.tmap_v3 <- function(sf_seg, palette, rev, - type, scale, tmap_params){ - # revert the palette - if (rev) { - palette <- paste0("-", palette) - } - # plot - p <- tmap::tm_shape(sf_seg) + - tmap::tm_fill( - col = type, - palette = palette - ) + - tmap::tm_graticules( - labels.size = tmap_params[["graticules_labels_size"]] - ) + - tmap::tm_compass() + - tmap::tm_layout( - legend.bg.color = tmap_params[["legend_bg_color"]], - legend.bg.alpha = tmap_params[["legend_bg_alpha"]], - legend.title.size = tmap_params[["legend_title_size"]], - legend.text.size = tmap_params[["legend_text_size"]], - legend.height = tmap_params[["legend_height"]], - legend.width = tmap_params[["legend_width"]], - legend.position = tmap_params[["legend_position"]], - scale = scale - ) + - tmap::tm_borders(lwd = 0.2) - - return(suppressWarnings(p)) -} +#' @export .tmap_vector_uncert.tmap_v4 <- function(sf_seg, palette, rev, type, scale, tmap_params){ # recover palette name used by cols4all @@ -746,9 +295,7 @@ #' \item \code{legend_text_size}: relative size of legend text (default = 1.0) #' \item \code{legend_bg_color}: color of legend background (default = "white") #' \item \code{legend_bg_alpha}: legend opacity (default = 0.5) -#' \item \code{legend_width}: relative width of legend (default = 1.0) #' \item \code{legend_position}: 2D position of legend (default = c("left", "bottom")) -#' \item \code{legend_height}: relative height of legend (default = 1.0) #' } .tmap_params_set <- function(dots){ @@ -758,8 +305,6 @@ 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")) - legend_height <- as.numeric(.conf("plot", "legend_height")) - legend_width <- as.numeric(.conf("plot", "legend_width")) legend_position <- .conf("plot", "legend_position") if ("graticules_labels_size" %in% names(dots)) @@ -772,10 +317,6 @@ legend_title_size <- dots[["legend_title_size"]] if ("legend_text_size" %in% names(dots)) legend_text_size <- dots[["legend_text_size"]] - if ("legend_height" %in% names(dots)) - legend_height <- dots[["legend_height"]] - if ("legend_width" %in% names(dots)) - legend_width <- dots[["legend_width"]] if ("legend_position" %in% names(dots)) legend_position <- dots[["legend_position"]] @@ -785,8 +326,6 @@ "legend_bg_alpha" = legend_bg_alpha, "legend_title_size" = legend_title_size, "legend_text_size" = legend_text_size, - "legend_height" = legend_height, - "legend_width" = legend_width, "legend_position" = legend_position ) return(tmap_params) diff --git a/man/sits_combine_predictions.Rd b/man/sits_combine_predictions.Rd index 3d6ef43fa..ef0775a39 100644 --- a/man/sits_combine_predictions.Rd +++ b/man/sits_combine_predictions.Rd @@ -93,7 +93,7 @@ if (sits_run_examples()) { data = cube, ml_model = rfor_model, output_dir = tempdir(), version = "rfor" ) - # create an XGBoost model + # create an SVM model svm_model <- sits_train(samples_modis_ndvi, sits_svm()) # classify a data cube using SVM model probs_svm_cube <- sits_classify( diff --git a/man/sits_confidence_sampling.Rd b/man/sits_confidence_sampling.Rd index 4a76bed20..9f67c5fe6 100644 --- a/man/sits_confidence_sampling.Rd +++ b/man/sits_confidence_sampling.Rd @@ -62,7 +62,7 @@ if (sits_run_examples()) { data_dir <- system.file("extdata/raster/mod13q1", package = "sits") cube <- sits_cube( source = "BDC", - collection = "MOD13Q1-6", + collection = "MOD13Q1-6.1", data_dir = data_dir ) # build a random forest model diff --git a/man/sits_cube.Rd b/man/sits_cube.Rd index cba6e0729..095fa3797 100644 --- a/man/sits_cube.Rd +++ b/man/sits_cube.Rd @@ -295,18 +295,19 @@ if (sits_run_examples()) { # --- Access to Digital Earth Australia cube_deaustralia <- sits_cube( source = "DEAUSTRALIA", - collection = "LS8-GEOMEDIAN", - bands = c("B05", "B07"), + collection = "GA_LS8C_NBART_GM_CYEAR_3", + bands = c("BLUE", "GREEN", "RED", "NIR", "SWIR1"), roi = c( lon_min = 137.15991, lon_max = 138.18467, lat_min = -33.85777, lat_max = -32.56690 ), - start_date = "2016-01-01", - end_date = "2017-01-01" + start_date = "2018-01-01", + end_date = "2018-12-31" ) # --- Access to CDSE open data Sentinel 2/2A level 2 collection + # --- remember to set the appropriate environmental variables # It is recommended that `multicores` be used to accelerate the process. s2_cube <- sits_cube( source = "CDSE", @@ -318,6 +319,7 @@ if (sits_run_examples()) { ) ## --- Sentinel-1 SAR from CDSE + # --- remember to set the appropriate environmental variables roi_sar <- c("lon_min" = 33.546, "lon_max" = 34.999, "lat_min" = 1.427, "lat_max" = 3.726) s1_cube_open <- sits_cube( diff --git a/man/sits_mlp.Rd b/man/sits_mlp.Rd index 5a8d3a3c8..608ae4de1 100644 --- a/man/sits_mlp.Rd +++ b/man/sits_mlp.Rd @@ -90,7 +90,7 @@ if (sits_run_examples()) { data_dir <- system.file("extdata/raster/mod13q1", package = "sits") cube <- sits_cube( source = "BDC", - collection = "MOD13Q1-6", + collection = "MOD13Q1-6.1", data_dir = data_dir ) # classify a data cube diff --git a/man/sits_sampling_design.Rd b/man/sits_sampling_design.Rd index 93dc99bc1..19af8bf63 100644 --- a/man/sits_sampling_design.Rd +++ b/man/sits_sampling_design.Rd @@ -7,6 +7,7 @@ sits_sampling_design( cube, expected_ua = 0.75, + alloc_options = c(100, 75, 50), std_err = 0.01, rare_class_prop = 0.1 ) @@ -16,6 +17,8 @@ sits_sampling_design( \item{expected_ua}{Expected values of user's accuracy} +\item{alloc_options}{Fixed sample allocation for rare classes} + \item{std_err}{Standard error we would like to achieve} \item{rare_class_prop}{Proportional area limit for rare classes} diff --git a/tests/testthat/test-accuracy.R b/tests/testthat/test-accuracy.R index 05c90f490..f629f6548 100644 --- a/tests/testthat/test-accuracy.R +++ b/tests/testthat/test-accuracy.R @@ -109,8 +109,6 @@ test_that("Accuracy areas", { multicores = 1, progress = FALSE ) - - expect_true(all(file.exists(unlist(probs_cube$file_info[[1]]$path)))) tc_obj <- .raster_open_rast(probs_cube$file_info[[1]]$path[[1]]) expect_true(nrow(tc_obj) == .tile_nrows(probs_cube)) diff --git a/tests/testthat/test-samples.R b/tests/testthat/test-samples.R index 12244e73e..a6b22f27b 100644 --- a/tests/testthat/test-samples.R +++ b/tests/testthat/test-samples.R @@ -49,7 +49,7 @@ test_that("Sampling design", { expect_true(all(c("prop", "expected_ua", "std_dev", "equal", "alloc_100", "alloc_75", "alloc_50", "alloc_prop") - %in% colnames(sampling_design))) + %in% colnames(sampling_design))) # select samples shp_file <- paste0(tempdir(),"/strata.shp")