i want to display raster images using a color palette in a leaflet map. The images are tiled (gdal2tiles) from a single .tif to multiple .png files with values from 0 to 100.

I add the image using the tileLayer function.

Is there a way to use a color palette in leaflet to display the values (e.g: 0=blue, 100=green)?

if not

is there a way to transform grayscale .png (or the input .tif) to a RGBA png using a predefined color palette?


If you're willing to use TileMill and MBTiles, you can do this pretty easily using CartoCSS to style the original Tiff, and output an MBTiles file.

#dem {
  raster-colorizer-default-mode: linear;
  raster-colorizer-default-color: transparent;
    stop(100, #afa);

There's an old tutorial posted here.

As far as the original question, 'is it possible to do it in the browser via leaflet', no, not as far as I know.


In R, this can easily be done using the mapview package. For instance, displaying Landsat 8 band 5 with a customized color scheme and breaks works as follows:


## custom color palette
cols <- colorRampPalette(c("green", "blue"))

## visualize data 
m <- mapview(poppendorf[[5]], col.regions = cols(100), 
             at = seq(5750, 30750, 500))


The 'true' leaflet map, if required, can be found via m@map:

> class(m@map)
[1] "leaflet"    "htmlwidget"

Furthermore, mapview allows you to add additional mapview (or Raster*, or Spatial*) objects via +, thus enabling to handle multiple tiles at once. Use e.g. m + poppendorf[[4]] to add band 4 to the initial map.


I solved it by writing the RGB values to individual layers in a stack, adding opacity information (RGBA) and write that to png.

I create a color palette with colorRampPalette() and get the RGB values with col2rgb()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.