Skip to content

Commit 73958f1

Browse files
committed
support SpatRaster with color table or RGB
1 parent 5d13177 commit 73958f1

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
@@ -372,50 +374,70 @@ addRasterImage_SpatRaster <- function(
372374
data = getMapData(map)
373375
) {
374376

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

380386
raster_is_factor <- terra::is.factor(x)
387+
388+
# there 1.5-50 has terra::has.colors(x)
389+
ctab <- terra::coltab(x)[[1]]
390+
has_colors <- !is.null(ctab)
391+
381392
method <- match.arg(method)
382393
if (method == "ngb") method = "near"
383394
if (method == "auto") {
384-
if (raster_is_factor) {
395+
if (raster_is_factor || has_colors) {
385396
method <- "near"
386397
} else {
387398
method <- "bilinear"
388399
}
389400
}
390401

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

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

417-
tileData <- terra::values(projected) %>% as.vector() %>% colors() %>% col2rgb(alpha = TRUE) %>% as.raw()
418-
dim(tileData) <- c(4, ncol(projected), nrow(projected))
439+
tileData <- terra::values(x) %>% as.vector() %>% colors() %>% col2rgb(alpha = TRUE) %>% as.raw()
440+
dim(tileData) <- c(4, ncol(x), nrow(x))
419441
pngData <- png::writePNG(tileData)
420442
if (length(pngData) > maxBytes) {
421443
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
@@ -32,4 +32,11 @@ test_that("rasters", {
3232

3333
expect_maps_equal(r1, r2)
3434

35+
# test with color map
36+
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)
37+
r[5,] <- NA
38+
coltab(r) <- c(rep("#FFFFFF", 10), rainbow(6, end=.9))
39+
(r3 <- rtest(r))
40+
3541
})
42+

0 commit comments

Comments
 (0)