From 955908391890902dcf9be3b73fd1f63ef10a4a81 Mon Sep 17 00:00:00 2001 From: mtennekes Date: Thu, 11 Apr 2024 12:04:21 +0200 Subject: [PATCH] improved qtm: bbox and v3 comp., leaflet layer collapse enabled #851 --- R/qtm.R | 78 +++++++++++++++++++++++++++++++++++++++--------- R/shapeTM.R | 14 +++++---- R/tmap_options.R | 3 +- 3 files changed, 75 insertions(+), 20 deletions(-) diff --git a/R/qtm.R b/R/qtm.R index 2d6aa6019..662ecdcd2 100644 --- a/R/qtm.R +++ b/R/qtm.R @@ -91,6 +91,43 @@ qtm <- function(shp, shp_name <- deparse(substitute(shp))[1] called <- names(match.call(expand.dots = TRUE)[-1]) + v3 = c("symbols.size", "symbols.col", "symbols.shape", "dots.col", + "text", "text.size", "text.col", "lines.lwd", "lines.col", "raster", + "borders", "projection") + + if (any(v3 %in% names(args))) { + mes = "tmap v3 code detected" + + if ("symbols.size" %in% names(args)) { + size = args$symbols.size + called = unique(c(called, "shape")) + mes = paste0(mes, "; use 'size' instead of 'symbols.size'") + } + if ("symbols.col" %in% names(args)) { + fill = args$symbols.col + called = unique(c(called, "shape")) + mes = paste0(mes, "; use 'fill' instead of 'symbols.col'") + } else if ("dots.col" %in% names(args)) { + fill = args$dots.col + mes = paste0(mes, "; use 'fill' instead of 'dots.col'") + } + + if ("lines.lwd" %in% names(args)) { + lwd = args$lines.lwd + mes = paste0(mes, "; use 'lwd' instead of 'lines.lwd'") + } + if ("lines.col" %in% names(args)) { + col = args$lines.col + mes = paste0(mes, "; use 'col' instead of 'lines.col'") + } + if ("raster" %in% names(args)) { + col = args$raster + mes = paste0(mes, "; use 'col' instead of 'raster'") + } + message(mes) + + } + tmapOptions <- tmap_options_mode() show.warnings = tmapOptions$show.warnings @@ -105,24 +142,37 @@ qtm <- function(shp, class(g) <- "tmap" return(g) } + - lst = c(list(size = size, - fill = fill, - col = col, - shape = shape, - lwd = lwd, - lty = lty, - fill_alpha = fill_alpha, - col_alpha = col_alpha, - zindex = zindex, - group = group, - group.control = group.control), args) + s = tm_shape(shp, crs = crs, bbox = bbox) + + is_rst = inherits(shp, c("stars", "SpatRaster")) - # if shape is specified at tm_sf, symbols are drawn instead of dots - if (!"shape" %in% called) lst$shape = NULL + if (is_rst) { + lst = c(list(col = col, + zindex = zindex, + group = group, + group.control = group.control), args) + g = s + do.call(tm_raster, lst) + } else { + lst = c(list(size = size, + fill = fill, + col = col, + shape = shape, + lwd = lwd, + lty = lty, + fill_alpha = fill_alpha, + col_alpha = col_alpha, + zindex = zindex, + group = group, + group.control = group.control), args) + + # if shape is specified at tm_sf, symbols are drawn instead of dots + if (!"shape" %in% called) lst$shape = NULL + g = s + do.call(tm_sf, lst) + } - g = tm_shape(shp) + do.call(tm_sf, lst) assign("last_map_new", match.call(), envir = .TMAP) attr(g, "qtm_shortcut") <- FALSE diff --git a/R/shapeTM.R b/R/shapeTM.R index 789132b23..c793c7005 100644 --- a/R/shapeTM.R +++ b/R/shapeTM.R @@ -9,11 +9,15 @@ #' @keywords internal shapeTM = function(shp, tmapID = NULL, bbox = NULL, ...) { if (!is.null(bbox) && (!inherits(bbox, "bbox"))) { - tryCatch({ - bbox = sf::st_bbox(bbox) - }, error = function(e) { - stop("Invalid bbox", call. = FALSE) - }) + if (is.character(bbox)) { + bbox = tmaptools::geocode_OSM(bbox)$bbox + } else { + tryCatch({ + bbox = sf::st_bbox(bbox) + }, error = function(e) { + stop("Invalid bbox", call. = FALSE) + }) + } } # filter empty geometries diff --git a/R/tmap_options.R b/R/tmap_options.R index 0d026927b..f1097d3e8 100644 --- a/R/tmap_options.R +++ b/R/tmap_options.R @@ -9,9 +9,10 @@ legend.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "left", align.v = "top", just.h = "left", just.v = "bottom"), crs = list(dimensions = 3857, 4326), facet.max = 16, + #legend.bg.alpha = 0.8, #view.legend.position = c("right", "top"), control.position = c("left", "top"), - control.collapse = FALSE, + control.collapse = TRUE, panel.show = FALSE, basemap.show = TRUE, set.bounds = FALSE,