@@ -206,7 +206,9 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
206
206
# ' @param x a \code{\link[terra]{SpatRaster}} or a \code{RasterLayer} object--see \code{\link[raster]{raster}}
207
207
# ' @param colors the color palette (see \code{\link{colorNumeric}}) or function
208
208
# ' 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.
210
212
# ' @param opacity the base opacity of the raster, expressed from 0 to 1
211
213
# ' @param attribution the HTML string to show as the attribution for this layer
212
214
# ' @param layerId the layer id
@@ -370,50 +372,70 @@ addRasterImage_SpatRaster <- function(
370
372
data = getMapData(map )
371
373
) {
372
374
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 ) {
374
380
x <- x [[1 ]]
375
381
warning(" using the first layer in 'x'" , call. = FALSE )
376
382
}
377
383
378
384
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
+
379
390
method <- match.arg(method )
380
391
if (method == " ngb" ) method = " near"
381
392
if (method == " auto" ) {
382
- if (raster_is_factor ) {
393
+ if (raster_is_factor || has_colors ) {
383
394
method <- " near"
384
395
} else {
385
396
method <- " bilinear"
386
397
}
387
398
}
388
399
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
-
397
400
bounds <- terra :: ext(
398
401
terra :: project(
399
402
terra :: project(
400
403
terra :: as.points(terra :: ext(x ), crs = terra :: crs(x )),
401
404
epsg3857 ),
402
405
epsg4326 )
403
406
)
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
+ }
404
421
405
422
if (! is.function(colors )) {
406
- if (method == " near" ) {
423
+ if (method == " near" || has_colors ) {
407
424
# '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 )
409
431
} else {
410
432
# 'numeric'
411
433
colors <- colorNumeric(colors , domain = NULL , na.color = " #00000000" , alpha = TRUE )
412
434
}
413
435
}
414
436
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 ))
417
439
pngData <- png :: writePNG(tileData )
418
440
if (length(pngData ) > maxBytes ) {
419
441
stop(
0 commit comments