Skip to content

Commit

Permalink
fix errors in plot for tmap version 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Nov 28, 2024
1 parent 7cb69cb commit 54bdea7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
27 changes: 23 additions & 4 deletions R/api_plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
#' @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
Expand All @@ -124,6 +126,8 @@
rev,
scale,
max_cog_size,
first_quantile,
last_quantile,
tmap_params) {
# crop using ROI
if (.has(roi)) {
Expand Down Expand Up @@ -154,11 +158,13 @@
green_file = green_file,
blue_file = blue_file,
sizes = sizes,
max_value = max_value,
sf_seg = NULL,
seg_color = NULL,
line_width = NULL,
scale = scale,
max_value = max_value,
first_quantile = first_quantile,
last_quantile = last_quantile,
tmap_params = tmap_params
)
return(p)
Expand All @@ -178,6 +184,8 @@
#' @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
#'
Expand All @@ -192,6 +200,8 @@
line_width,
scale,
max_cog_size,
first_quantile,
last_quantile,
tmap_params) {

# crop using ROI
Expand Down Expand Up @@ -224,11 +234,13 @@
green_file = green_file,
blue_file = blue_file,
sizes = sizes,
max_value = max_value,
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
scale = scale,
max_value = max_value,
first_quantile = first_quantile,
last_quantile = last_quantile,
tmap_params = tmap_params
)
return(p)
Expand All @@ -242,23 +254,27 @@
#' @param green_file File to be plotted in green
#' @param blue_file File to be plotted in blue
#' @param sizes Image sizes for overview
#' @param max_value Maximum value
#' @param 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 max_value Maximum value
#' @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
#'
.plot_rgb_stars <- function(red_file,
green_file,
blue_file,
sizes,
max_value,
sf_seg,
seg_color,
line_width,
scale,
max_value,
first_quantile,
last_quantile,
tmap_params) {

# read raster data as a stars object with separate RGB bands
Expand All @@ -274,6 +290,9 @@
p <- .tmap_rgb_color(
rgb_st = rgb_st,
scale = scale,
max_value = max_value,
first_quantile = first_quantile,
last_quantile = last_quantile,
tmap_params = tmap_params,
sf_seg = sf_seg,
seg_color = seg_color,
Expand Down
17 changes: 11 additions & 6 deletions R/api_tmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@
#' @keywords internal
#' @noRd
#' @param rgb_st RGB stars object.
#' @param scale Scale to plot map (0.4 to 1.0)
#' @param max_value Maximum value
#' @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
#' @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,
scale,
max_value,
first_quantile,
last_quantile,
tmap_params,
sf_seg,
seg_color,
line_width,
scale,
tmap_params) {

line_width) {
if (as.numeric_version(utils::packageVersion("tmap")) < "3.9")
class(rgb_st) <- "tmap_v3"
else
Expand Down
20 changes: 17 additions & 3 deletions R/api_tmap_v3.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,24 @@
return(p)
}
#' @export
.tmap_rgb_color.tmap_v3 <- function(rgb_st, ...,
sf_seg, seg_color, line_width,
scale, tmap_params) {
.tmap_rgb_color.tmap_v3 <- function(rgb_st,
scale,
max_value,
first_quantile,
last_quantile,
tmap_params,
sf_seg,
seg_color,
line_width) {

# 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
)
# tmap params
labels_size <- tmap_params[["graticules_labels_size"]]

Expand Down
13 changes: 8 additions & 5 deletions R/api_tmap_v4.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,23 @@
}
#' @export
.tmap_rgb_color.tmap_v4 <- function(rgb_st,
scale,
max_value,
first_quantile,
last_quantile,
tmap_params,
sf_seg,
seg_color,
line_width,
scale,
tmap_params) {
line_width) {

p <- tmap::tm_shape(rgb_st, raster.downsample = FALSE) +
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
probs = c(first_quantile, last_quantile),
maxColorValue = max_value
)
) +
tmap::tm_graticules(
Expand Down
6 changes: 5 additions & 1 deletion R/sits_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,13 @@ plot.raster_cube <- function(x, ...,
tile = tile,
band = band,
dates = dates,
roi = roi,
palette = palette,
rev = rev,
scale = scale,
max_cog_size = max_cog_size,
roi = roi,
first_quantile = first_quantile,
last_quantile = last_quantile,
tmap_params = tmap_params
)
return(p)
Expand Down Expand Up @@ -485,6 +487,8 @@ plot.raster_cube <- function(x, ...,
line_width = NULL,
scale = scale,
max_cog_size = max_cog_size,
first_quantile = first_quantile,
last_quantile = last_quantile,
tmap_params = tmap_params
)
}
Expand Down
14 changes: 5 additions & 9 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
Documentation avaliable in %s.",
utils::packageDescription("sits")[["Version"]],
"https://e-sensing.github.io/sitsbook/"

)
)
if (Sys.info()["sysname"] == "Darwin") {
if (grepl("4.4", R.version.string)) {
packageStartupMessage(
sprintf(
"Running R-4.4 on MacOS.
Please read section \"Fixing problems in MacOS\" in
packageStartupMessage(
sprintf(
"Important: Please read \"Release Notes for SITS 1.5.2\" in
https://github.com/e-sensing/sits."
)
)
}
}
)
}
.onLoad <- function(lib, pkg) {
Sys.setenv(R_CONFIG_FILE = "config.yml")
Expand Down
8 changes: 8 additions & 0 deletions inst/extdata/torch/download_new_torch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
options(timeout = 600)
kind <- "cpu-intel"
version <- "0.13.0.9001"
options(repos = c(
torch = sprintf("https://torch-cdn.mlverse.org/packages/%s/%s/", kind, version),
CRAN = "https://cloud.r-project.org" # or any other from which you want to install the other R dependencies.
))
install.packages("torch", type = "binary")

0 comments on commit 54bdea7

Please sign in to comment.