leaflet icon indicating copy to clipboard operation
leaflet copied to clipboard

FR: Raster Image RGB / multi layer support

Open noerw opened this issue 9 years ago • 17 comments

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.

noerw avatar Dec 01 '15 13:12 noerw

I'm surprised the color palette approach didn't work (though inconvenient, I admit). Do you have a reproducible example of what you attempted?

jcheng5 avatar Dec 01 '15 20:12 jcheng5

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.

noerw avatar Dec 01 '15 21:12 noerw

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.

noerw avatar Dec 04 '15 18:12 noerw

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-salabim avatar Jan 02 '16 21:01 tim-salabim

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)

ghost avatar Sep 29 '17 06:09 ghost

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.

tim-salabim avatar Sep 29 '17 07:09 tim-salabim

Thanks for your quick response. The @map did the trick. Looking forward to stars!

ghost avatar Sep 29 '17 07:09 ghost

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

mikel89larioja avatar Feb 02 '18 13:02 mikel89larioja

Sorry for the trouble. This is a bug in the latest mapview release. Should be fixed soon.

tim-salabim avatar Feb 02 '18 14:02 tim-salabim

@mikel89larioja this should be fixed now.

tim-salabim avatar Feb 03 '18 13:02 tim-salabim

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 avatar Apr 10 '18 19:04 agubas

@agubas Can you provide a reproducible example using reprex? Thank you!

reprex::reprex({
library(leaflet)
# code to reproduce problem
})

schloerke avatar Apr 12 '18 13:04 schloerke

viewRGB is a function from package mapview. So you need to install mapview (currently from github).

devtools::install_github("r-spatial/mapview@develop")

tim-salabim avatar Apr 12 '18 13:04 tim-salabim

Thanks Tim! minor confusion there

agubas avatar Apr 12 '18 13:04 agubas

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?

aloboa avatar Jul 03 '24 12:07 aloboa

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).

aloboa avatar Jul 03 '24 14:07 aloboa

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))

image

aloboa avatar Sep 05 '24 07:09 aloboa