Skip to content

Commit 3e6fabf

Browse files
committed
Merge remote-tracking branch 'rhijmans/main'
2 parents 57ea627 + 73958f1 commit 3e6fabf

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

R/layers.R

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
206206
#' @param x a \code{\link[terra]{SpatRaster}} or a \code{RasterLayer} object--see \code{\link[raster]{raster}}
207207
#' @param colors the color palette (see \code{\link{colorNumeric}}) or function
208208
#' to use to color the raster values (hint: if providing a function, set
209-
#' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent)
209+
#' \code{na.color} to \code{"#00000000"} to make \code{NA} areas transparent).
210+
#' The palette is ignored if \code{x} is a SpatRaster with a color table or if
211+
#' it has RGB channels.
210212
#' @param opacity the base opacity of the raster, expressed from 0 to 1
211213
#' @param attribution the HTML string to show as the attribution for this layer
212214
#' @param layerId the layer id
@@ -370,50 +372,70 @@ addRasterImage_SpatRaster <- function(
370372
data = getMapData(map)
371373
) {
372374

373-
if (terra::nlyr(x) > 1) {
375+
# terra 1.5-50 has terra::has.RGB()
376+
if (x@ptr$rgb) {
377+
# RGB(A) channels to color table
378+
x <- terra::colorize(x, "col")
379+
} else if (terra::nlyr(x) > 1) {
374380
x <- x[[1]]
375381
warning("using the first layer in 'x'", call. = FALSE)
376382
}
377383

378384
raster_is_factor <- terra::is.factor(x)
385+
386+
# there 1.5-50 has terra::has.colors(x)
387+
ctab <- terra::coltab(x)[[1]]
388+
has_colors <- !is.null(ctab)
389+
379390
method <- match.arg(method)
380391
if (method == "ngb") method = "near"
381392
if (method == "auto") {
382-
if (raster_is_factor) {
393+
if (raster_is_factor || has_colors) {
383394
method <- "near"
384395
} else {
385396
method <- "bilinear"
386397
}
387398
}
388399

389-
if (project) {
390-
# if we should project the data
391-
projected <- projectRasterForLeaflet(x, method)
392-
} else {
393-
# do not project data
394-
projected <- x
395-
}
396-
397400
bounds <- terra::ext(
398401
terra::project(
399402
terra::project(
400403
terra::as.points(terra::ext(x), crs=terra::crs(x)),
401404
epsg3857),
402405
epsg4326)
403406
)
407+
## can't the above be simplified to this?
408+
# bounds <- terra::ext(
409+
# terra::project(
410+
# terra::as.points(terra::ext(x), crs=terra::crs(x)),
411+
# epsg4326)
412+
# )
413+
414+
if (project) {
415+
# if we should project the data
416+
x <- projectRasterForLeaflet(x, method)
417+
if (method=="bilinear") {
418+
has_colors <- FALSE
419+
}
420+
}
404421

405422
if (!is.function(colors)) {
406-
if (method == "near") {
423+
if (method == "near" || has_colors) {
407424
# 'factors'
408-
colors <- colorFactor(colors, domain = NULL, na.color = "#00000000", alpha = TRUE)
425+
domain <- NULL
426+
if (has_colors) {
427+
colors <- rgb(ctab[,2], ctab[,3], ctab[,4], ctab[,5], maxColorValue=255)
428+
domain <- ctab[,1]
429+
}
430+
colors <- colorFactor(colors, domain = domain, na.color = "#00000000", alpha = TRUE)
409431
} else {
410432
# 'numeric'
411433
colors <- colorNumeric(colors, domain = NULL, na.color = "#00000000", alpha = TRUE)
412434
}
413435
}
414436

415-
tileData <- terra::values(projected) %>% as.vector() %>% colors() %>% col2rgb(alpha = TRUE) %>% as.raw()
416-
dim(tileData) <- c(4, ncol(projected), nrow(projected))
437+
tileData <- terra::values(x) %>% as.vector() %>% colors() %>% col2rgb(alpha = TRUE) %>% as.raw()
438+
dim(tileData) <- c(4, ncol(x), nrow(x))
417439
pngData <- png::writePNG(tileData)
418440
if (length(pngData) > maxBytes) {
419441
stop(

R/normalize-terra.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pointData.SpatVector <- function(obj) {
2424
polygonData.SpatVector <- function(obj) {
2525
check_crs_terra(obj)
2626

27-
# this is a bit convoluted. I will add a simpler
28-
# and more efficient method to terra to replace the below
2927
xy = data.frame(terra::geom(obj))
3028
names(xy)[3:4] = c("lng", "lat")
3129
xy = split(xy[,2:5], xy[,1]) # polygons
@@ -40,6 +38,9 @@ polygonData.SpatVector <- function(obj) {
4038
})
4139
})
4240

41+
# with terra >= 1.5-50 you can do this instead
42+
# xy = terra::geom(obj, list=TRUE, xnm="lng", ynm="lat")
43+
4344
structure(
4445
xy,
4546
bbox = terra_bbox(obj)

man/addRasterImage.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-raster.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ test_that("rasters", {
3737

3838
expect_maps_equal(r1, r2)
3939

40+
# test with color map
41+
r <- rast(ncols=10, nrows=10, vals=rep_len(10:15, length.out=100), xmin=0, xmax=10^6, ymin=0, ymax=10^6, crs=pmerc)
42+
r[5,] <- NA
43+
coltab(r) <- c(rep("#FFFFFF", 10), rainbow(6, end=.9))
44+
(r3 <- rtest(r))
45+
4046
})
47+

0 commit comments

Comments
 (0)