Skip to content

Commit

Permalink
support for tmap version 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Aug 1, 2024
1 parent 6a2ca14 commit 2396672
Show file tree
Hide file tree
Showing 23 changed files with 1,227 additions and 1,087 deletions.
7 changes: 3 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,12 @@ S3method(.tile_xres,raster_cube)
S3method(.tile_yres,default)
S3method(.tile_yres,raster_cube)
S3method(.tmap_class_map,tmap_v3)
S3method(.tmap_class_map,tmap_v4)
S3method(.tmap_dem_map,tmap_v3)
S3method(.tmap_false_color,tmap_v3)
S3method(.tmap_false_color,tmap_v4)
S3method(.tmap_probs_map,tmap_v3)
S3method(.tmap_probs_map,tmap_v4)
S3method(.tmap_rgb_color,tmap_v3)
S3method(.tmap_rgb_color,tmap_v4)
S3method(.tmap_vector_class,tmap_v3)
S3method(.tmap_vector_probs,tmap_v3)
S3method(.values_ts,bands_cases_dates)
S3method(.values_ts,bands_dates_cases)
S3method(.values_ts,cases_dates_bands)
Expand Down
1 change: 1 addition & 0 deletions R/api_ml_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
.ml_normalize.torch_model <- function(ml_model, values){
values[is.na(values)] <- 0
values <- softmax(values)
return(values)
}
#' @export
#'
Expand Down
32 changes: 7 additions & 25 deletions R/api_plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
#' @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 main_title Main title for the plot
#' @param rev Reverse the color palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @param style Style for plotting continuous data
#' @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
Expand All @@ -27,10 +25,8 @@
seg_color,
line_width,
palette,
main_title,
rev,
scale,
style,
max_cog_size,
first_quantile,
last_quantile,
Expand Down Expand Up @@ -81,7 +77,6 @@
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
main_title = main_title,
palette = palette,
rev = rev,
scale = scale,
Expand All @@ -101,7 +96,6 @@
#' @param band Band to be plotted.
#' @param dates Dates to be plotted.
#' @param palette A sequential RColorBrewer palette
#' @param main_title Main title for the plot
#' @param rev Reverse the color palette?
#' @param scale Scale to plot map (0.4 to 1.0)
#' @param max_cog_size Maximum size of COG overviews (lines or columns)
Expand All @@ -115,7 +109,6 @@
band,
dates,
palette,
main_title,
rev,
scale,
max_cog_size,
Expand Down Expand Up @@ -143,7 +136,6 @@
blue_file = blue_file,
sizes = sizes,
max_value = max_value,
main_title = main_title,
sf_seg = NULL,
seg_color = NULL,
line_width = NULL,
Expand All @@ -164,7 +156,6 @@
#' @param green Band to be plotted in green
#' @param blue Band to be plotted in blue
#' @param date Date to be plotted
#' @param main_title Main title for the plot
#' @param sf_seg Segments (sf object)
#' @param seg_color Color to use for segment borders
#' @param line_width Line width to plot the segments boundary
Expand All @@ -180,7 +171,6 @@
green,
blue,
date,
main_title,
sf_seg,
seg_color,
line_width,
Expand Down Expand Up @@ -211,7 +201,6 @@
blue_file = blue_file,
sizes = sizes,
max_value = max_value,
main_title = main_title,
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
Expand All @@ -232,7 +221,6 @@
#' @param blue_file File to be plotted in blue
#' @param sizes Image sizes for overview
#' @param max_value Maximum value
#' @param main_title Main title
#' @param sf_seg Segments (sf object)
#' @param seg_color Color to use for segment borders
#' @param line_width Line width to plot the segments boundary
Expand All @@ -247,7 +235,6 @@
blue_file,
sizes,
max_value,
main_title,
sf_seg,
seg_color,
line_width,
Expand Down Expand Up @@ -278,7 +265,6 @@

p <- .tmap_rgb_color(
rgb_st = rgb_st,
main_title = main_title,
scale = scale,
tmap_params = tmap_params,
sf_seg = sf_seg,
Expand Down Expand Up @@ -365,7 +351,7 @@
#' @keywords internal
#' @noRd
#' @param tile Probs cube to be plotted.
#' @param labels_plot Labels to be plotted
#' @param label_plot Label to be plotted
#' @param palette A sequential RColorBrewer palette
#' @param rev Reverse the color palette?
#' @param scale Global scale for plot
Expand All @@ -374,7 +360,7 @@
#' @return A plot object
#'
.plot_probs <- function(tile,
labels_plot,
label_plot,
palette,
rev,
scale,
Expand All @@ -391,13 +377,8 @@
# get all labels to be plotted
labels <- .tile_labels(tile)
names(labels) <- seq_len(length(labels))
# check the labels to be plotted
# if NULL, use all labels
if (.has_not(labels_plot)) {
labels_plot <- labels
} else {
.check_that(all(labels_plot %in% labels))
}
# check the label to be plotted
.check_that(label_plot %in% labels)
# size of data to be read
max_size <- .conf("plot", "max_size")
sizes <- .tile_overview_size(tile = tile, max_cog_size)
Expand All @@ -419,12 +400,13 @@
probs_st <- probs_st * .scale(band_conf)

# rename stars object dimensions to labels
probs_st <- stars::st_set_dimensions(probs_st, "band", values = labels)
probs_st <- stars::st_set_dimensions(probs_st,
"band", values = labels)

p <- .tmap_probs_map(
probs_st = probs_st,
labels = labels,
labels_plot = labels_plot,
label_plot = label_plot,
palette = palette,
rev = rev,
scale = scale,
Expand Down
105 changes: 26 additions & 79 deletions R/api_plot_vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
#' @param legend Legend for the classes
#' @param palette A sequential RColorBrewer palette
#' @param scale Global scale for plot
#'
#' @param tmap_params Parameters for tmap control
#' @return A plot object
#'
.plot_class_vector <- function(tile,
legend,
palette,
scale) {
scale,
tmap_params) {
# set caller to show in errors
.check_set_caller(".plot_class_vector")
# retrieve the segments for this tile
Expand All @@ -41,22 +42,12 @@
sf_seg <- sf_seg |>
dplyr::group_by(.data[["class"]]) |>
dplyr::summarise()
# plot the data using tmap
p <- tmap::tm_shape(sf_seg) +
tmap::tm_fill(
col = "class",
palette = colors
) +
tmap::tm_graticules(
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_compass() +
tmap::tm_layout(
scale = scale,
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.2)

# plot
p <- .tmap_vector_class(sf_seg = sf_seg,
colors = colors,
scale = scale,
tmap_params = tmap_params)
return(p)
}
#' @title Plot a probs vector cube
Expand All @@ -66,22 +57,20 @@
#' @keywords internal
#' @noRd
#' @param tile Tile to be plotted.
#' @param labels_plot Labels to be plotted
#' @param label_plot Label to be plotted
#' @param palette A sequential RColorBrewer palette
#' @param style Method to process the color scale
#' ("cont", "order", "quantile", "fisher",
#' "jenks", "log10")
#' @param rev Revert the color of the palette?
#' @param scale. Global map scale
#' @param scale Global map scale
#' @param tmap_params Tmap parameters
#'
#' @return A plot object
#'
.plot_probs_vector <- function(tile,
labels_plot,
label_plot,
palette,
style,
rev,
scale) {
scale,
tmap_params) {
# set caller to show in errors
.check_set_caller(".plot_probs_vector")
# verifies if stars package is installed
Expand All @@ -90,43 +79,26 @@
.check_require_packages("tmap")
# precondition - check color palette
.check_palette(palette)
# revert the palette
if (rev) {
palette <- paste0("-", palette)
}
# get all labels to be plotted
labels <- .tile_labels(tile)
names(labels) <- seq_len(length(labels))
# check the labels to be plotted
# if NULL, use all labels
if (.has_not(labels_plot))
labels_plot <- labels
.check_that(all(labels_plot %in% labels))
.check_that(label_plot %in% labels)

# get the segments to be plotted
sf_seg <- .segments_read_vec(tile)

# plot the segments by facet
p <- tmap::tm_shape(sf_seg) +
tmap::tm_fill(
labels_plot,
style = style,
palette = palette,
midpoint = 0.5,
title = labels[labels %in% labels_plot]) +
tmap::tm_graticules(
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_facets() +
tmap::tm_compass() +
tmap::tm_layout(
scale = scale,
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.1)

return(suppressWarnings(p))
p <- .tmap_vector_probs(
sf_seg = sf_seg,
palette = palette,
rev = rev,
labels = labels,
label_plot = label_plot,
scale = scale,
tmap_params = tmap_params
)
return(p)
}
#' @title Plot uncertainty vector cube
#' @name .plot_uncertainty_vector
Expand All @@ -136,15 +108,13 @@
#' @noRd
#' @param tile Tile to be plotted.
#' @param palette A sequential RColorBrewer palette
#' @param main_title Main title for the cube
#' @param rev Revert the color of the palette?
#' @param scale Global map scale
#'
#' @return A plot object
#'
.plot_uncertainty_vector <- function(tile,
palette,
main_title,
rev,
scale) {
# verifies if stars package is installed
Expand All @@ -153,32 +123,9 @@
.check_require_packages("tmap")
# precondition - check color palette
.check_palette(palette)
# revert the palette
if (rev) {
palette <- paste0("-", palette)
}
# get the segements to be plotted
sf_seg <- .segments_read_vec(tile)
# obtain the uncertainty type
uncert_type <- .vi(tile)[["band"]]
# plot the segments by facet
p <- tmap::tm_shape(sf_seg) +
tmap::tm_polygons(uncert_type,
palette = palette,
style = "cont") +
tmap::tm_graticules(
labels.size = as.numeric(.conf("plot", "graticules_labels_size"))
) +
tmap::tm_compass() +
tmap::tm_layout(
main.title = main_title,
main.title.size = 1,
main.title.position = "center",
scale = scale,
legend.bg.color = .conf("plot", "legend_bg_color"),
legend.bg.alpha = as.numeric(.conf("plot", "legend_bg_alpha"))
) +
tmap::tm_borders(lwd = 0.2)

return(suppressWarnings(p))
}
Loading

0 comments on commit 2396672

Please sign in to comment.