Skip to content

Commit 05a9b85

Browse files
authored
Call terra::has.RGB() with namespace (#869)
1 parent 6a947d1 commit 05a9b85

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ BUG FIXES and IMPROVEMENTS
1616

1717
- Respect option scrollWheelZoom=FALSE. (#827)
1818

19+
- Fixed #866: Correctly call `terra::has.RGB()` in `addRasterImage()` for a `SpatRaster` object. (#869)
20+
1921
## leaflet 2.1.2
2022

2123
BUG FIXES and IMPROVEMENTS

R/layers.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ addRasterImage <- function(
325325
addRasterLegend <- function(map, x, layer = 1, ...) {
326326
stopifnot(inherits(x, "SpatRaster"))
327327
stopifnot(length(layer) == 1 && layer > 0 && layer <= terra::nlyr(x))
328-
329-
## might as well do this here and only once. Subsetting would otherwise have been necessary in
328+
329+
## might as well do this here and only once. Subsetting would otherwise have been necessary in
330330
## color_info <- base::subset(color_info, value %in% terra::values(x))
331331
x <- x[[layer]]
332-
332+
333333
# Retrieve the color table from the layer. If one doesn't exist, that means
334334
# the raster was colored some other way, like using colorFactor or something,
335335
# and the regular addLegend() is designed for those cases.
@@ -348,13 +348,13 @@ addRasterLegend <- function(map, x, layer = 1, ...) {
348348
lvls <- terra::levels(x)[[1]]
349349

350350
# Drop values that aren't part of the layer
351-
## unlike "values", "unique" is memory-safe; it does not load all values
351+
## unlike "values", "unique" is memory-safe; it does not load all values
352352
## into memory if the raster is large. So instead of:
353353

354354
# color_info <- base::subset(color_info, value %in% terra::values(x))
355355

356356
## remove the levels to get the raw cell values
357-
levels(x) <- NULL
357+
levels(x) <- NULL
358358
color_info <- base::subset(color_info, value %in% terra::unique(x)[[1]])
359359

360360
res <- if (is.data.frame(lvls)) {
@@ -463,9 +463,14 @@ addRasterImage_SpatRaster <- function(
463463
maxBytes = 4 * 1024 * 1024,
464464
data = getMapData(map)
465465
) {
466+
if (!is_installed("terra", "1.6-3")) { # for terra::has.RGB()
467+
stop(
468+
"`addRasterImage()` for SpatRaster objects requires {terra} 1.6-3 or higher",
469+
call. = FALSE
470+
)
471+
}
466472

467-
# terra 1.5-50 has terra::has.RGB()
468-
if (has.RGB(x)) {
473+
if (terra::has.RGB(x)) {
469474
# RGB(A) channels to color table
470475
x <- terra::colorize(x, "col")
471476
} else if (terra::nlyr(x) > 1) {

R/staticimports.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@ get_package_version <- function(pkg) {
2626

2727
is_installed <- function(pkg, version = NULL) {
2828
installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg))
29+
2930
if (is.null(version)) {
3031
return(installed)
3132
}
33+
34+
if (!is.character(version) && !inherits(version, "numeric_version")) {
35+
# Avoid https://bugs.r-project.org/show_bug.cgi?id=18548
36+
alert <- if (identical(Sys.getenv("TESTTHAT"), "true")) stop else warning
37+
alert("`version` must be a character string or a `package_version` or `numeric_version` object.")
38+
39+
version <- numeric_version(sprintf("%0.9g", version))
40+
}
41+
3242
installed && isTRUE(get_package_version(pkg) >= version)
3343
}
3444

0 commit comments

Comments
 (0)