Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Raster Image RGB / multi layer support #212

Open
noerw opened this issue Dec 1, 2015 · 17 comments
Open

FR: Raster Image RGB / multi layer support #212

noerw opened this issue Dec 1, 2015 · 17 comments

Comments

@noerw
Copy link

noerw commented Dec 1, 2015

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.

@jcheng5
Copy link
Member

jcheng5 commented Dec 1, 2015

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

@noerw
Copy link
Author

noerw commented Dec 1, 2015

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
Copy link
Author

noerw commented Dec 4, 2015

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.

@tim-salabim
Copy link
Contributor

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.

@ghost
Copy link

ghost commented Sep 29, 2017

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)

@tim-salabim
Copy link
Contributor

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.

@ghost
Copy link

ghost commented Sep 29, 2017

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

@mikel89larioja
Copy link

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

@tim-salabim
Copy link
Contributor

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

@tim-salabim
Copy link
Contributor

@mikel89larioja this should be fixed now.

@agubas
Copy link

agubas commented Apr 10, 2018

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?

@schloerke
Copy link
Contributor

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

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

@tim-salabim
Copy link
Contributor

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

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

@agubas
Copy link

agubas commented Apr 12, 2018

Thanks Tim! minor confusion there

@aloboa
Copy link

aloboa commented Jul 3, 2024

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
Copy link

aloboa commented Jul 3, 2024

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 #900 (the leaflet display i very poor).

@aloboa
Copy link

aloboa commented Sep 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants