leaflet
leaflet copied to clipboard
FR: Raster Image RGB / multi layer support
I am trying to display an GeoTIFF with 3 Layers (RGB) in leaflet, preserving the true colors.
From my investigations it seems that this is currently not possible, as only single layer raster files are supported: leaflet::addRasterImage() only supports RasterLayer as input. RasterStack and RasterBrick - which (I believe) would enable multi layer support - are not accepted.
I also tried to generate a color palette, which would map the "true" colors to the pixels, but had no success.
Is there currently a way to solve this problem?
It would be great, if RGB true color support was integrated in the addRasterImage() function.
I'm surprised the color palette approach didn't work (though inconvenient, I admit). Do you have a reproducible example of what you attempted?
I have scaled the values of the 3 layers into one layer, containing RGB values (for 8bit images in the range 0:16777215):
library(raster)
tif <- brick("./someFile.tif")
maxVal = maxValue(tif) + 1
# merge the first three layers into one layer
# values are now in the range of [0, maxVal^3 - 1]
# layer1 is most significant, layer3 is least significant
rgb <- tif[[1]] * maxVal^2 + tif[[2]] * maxVal + tif[[3]]
Next step would be to create a color palette, that assigns each of these values an RGB color. But I have no clue how to properly do that.
Any advice would be great!
Edit:
- Here is a dataset to test, if needed.
I managed to do it, but this is probably the worst code i ever wrote.
Here's the code to create such a palette.
palette can be applied to the above rgb Raster object while adding it to the map.
This colorramp takes ~900MB of RAM, and around 20seconds to calculate on my laptop, so I ended up loading this thing from a Rdata file. As long as there is no other way this is my way to go, but it would be great if there was native RGB support.
here's our solution:
https://github.com/environmentalinformatics-marburg/mapview/blob/master/R/viewRGB.R
The function let's you plot any 3-band combination of your LS image, so false color images are also possible.
Tim, can you use viewRGB to display color composites as below in leaflet? (as opposed to mapview). If so, can you please provide an example? Thanks!
mapview::viewRGB(poppendorf, 4, 3, 2)
There is currently no addRGB if that is what you mean. But you should be able to set up your leaflet map first and then pass that to viewRGB like viewRGB(poppendorf, 4, 3, 2, map = my_leaflet_map). In case you are happy with the map layout provided by mapview but need a leaflet object you can simply use the @map slot which is the leaflet map part, e.g. my_leaflet_map@map.
A addRGB function is probably the best way in the long run, but I don't know when I will have time to work on such a thing. I am planning to rewrite a lot of the raster based methods in mapview once stars is a bit more mature.
Thanks for your quick response. The @map did the trick. Looking forward to stars!
Hi,
I want to add a sentinel 2 image subset to a leaflet map, but I cannot.
First, I try to apply this code and then I will do it with my image.
library(mapview) viewRGB(poppendorf, 4, 3, 2)
But I get this error:
Error in raster::projectRaster(x, raster::projectExtent(x, crs = sp::CRS(wmcrs)), : argument "method" is missing, with no default
Can anybody help me? Thanks,
MIKEL
Sorry for the trouble. This is a bug in the latest mapview release. Should be fixed soon.
@mikel89larioja this should be fixed now.
Hi, I'm having a problem with the viewRGB function when trying to plot a RasterBrick object.
Error in initMap(map, map.types, projection(x)) : could not find function "initMap"
I tried to install the package that contains this functions but I cant seem to find it?
any ideas?
@agubas Can you provide a reproducible example using reprex? Thank you!
reprex::reprex({
library(leaflet)
# code to reproduce problem
})
viewRGB is a function from package mapview. So you need to install mapview (currently from github).
devtools::install_github("r-spatial/mapview@develop")
Thanks Tim! minor confusion there
I am trying to display a 3-band terra rast object as a color composite with leaflet::addRasterImage() Is using mapview::viewRGB() still the way to go?
Answering myself: It seems the way to go is with terra::colorize():
r <- subset(rast("enmapL2A_199-114-037_gmap.tif"),1:3)
RGB(r) <- 1:3
r[r==0] <- NA
a <- colorize(r,"col")
plot(a)
Then I use
addRasterImage(a, group="a")
but see https://github.com/rstudio/leaflet/issues/900 (the leaflet display i very poor).
Actually, using leaflet::addRasterRGB() is a better alternative:
library(RStoolbox) #for the example satellite image
library(raster)
library(terra)
library(leaflet)
library(leafem)
B4 <- project(rast(system.file("external/landsat/LT52240631988227CUB02_B4.TIF", package="RStoolbox")), "EPSG:4326")
B3 <- project(rast(system.file("external/landsat/LT52240631988227CUB02_B3.TIF", package="RStoolbox")), "EPSG:4326")
B2 <- project(rast(system.file("external/landsat/LT52240631988227CUB02_B2.TIF", package="RStoolbox")), "EPSG:4326")
B <- c(B4,B3,B2)
leaflet() %>%
setView(lng = -49.9, lat=-3.75 , zoom = 12)%>%
addTiles() %>% addMouseCoordinates() %>%
leafem::addRasterRGB(raster::brick(B), r=1,g=2,b=3, quantile=c(.2,.98))