@@ -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
@@ -372,50 +374,70 @@ addRasterImage_SpatRaster <- function(
372
374
data = getMapData(map )
373
375
) {
374
376
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 ) {
376
382
x <- x [[1 ]]
377
383
warning(" using the first layer in 'x'" , call. = FALSE )
378
384
}
379
385
380
386
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
+
381
392
method <- match.arg(method )
382
393
if (method == " ngb" ) method = " near"
383
394
if (method == " auto" ) {
384
- if (raster_is_factor ) {
395
+ if (raster_is_factor || has_colors ) {
385
396
method <- " near"
386
397
} else {
387
398
method <- " bilinear"
388
399
}
389
400
}
390
401
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
-
399
402
bounds <- terra :: ext(
400
403
terra :: project(
401
404
terra :: project(
402
405
terra :: as.points(terra :: ext(x ), crs = terra :: crs(x )),
403
406
epsg3857 ),
404
407
epsg4326 )
405
408
)
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
+ }
406
423
407
424
if (! is.function(colors )) {
408
- if (method == " near" ) {
425
+ if (method == " near" || has_colors ) {
409
426
# '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 )
411
433
} else {
412
434
# 'numeric'
413
435
colors <- colorNumeric(colors , domain = NULL , na.color = " #00000000" , alpha = TRUE )
414
436
}
415
437
}
416
438
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 ))
419
441
pngData <- png :: writePNG(tileData )
420
442
if (length(pngData ) > maxBytes ) {
421
443
stop(
0 commit comments