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

How to style rasters added to a leaflet map with addTileFolder? #103

Open
jpd-defra opened this issue Dec 18, 2024 · 3 comments
Open

How to style rasters added to a leaflet map with addTileFolder? #103

jpd-defra opened this issue Dec 18, 2024 · 3 comments

Comments

@jpd-defra
Copy link

jpd-defra commented Dec 18, 2024

Hello,

I've created a set of tiles with gdal2tiles.py I can get them to display on the leaflet map with addTileFolder, but from the docs can't work out how to style the raster(s). Which options can I use to provide a colour palette?

My code looks like this:

leaflet() %>%
  addTiles() %>%
  addTileFolder(
    "/tmp/30by30_tiles_WGS84",
    tms = TRUE,
    layerId = NULL,
    group = NULL,
    attribution = NULL,
    options = leaflet::tileOptions(),
    data = leaflet::getMapData(.)
  )

And my map looks like this - the raster extent is correct.

image

If there's a bit more detail to add to the docs, I would be happy to do this in a PR.

Any help much appreciated!

@tim-salabim
Copy link
Member

Hi, its a long time since i have looked into addTileFolder. Any chance you can supply the data?

@jpd-defra
Copy link
Author

Hi @tim-salabim , I can't share my actual data, so I've created some dummy data to illustrate the problem with the following code:

library(raster)
set.seed(42) 

xmin <- 400000  
xmax <- 600000
ymin <- 0
ymax <- 300000 
resolution <- 500

r <- raster(xmn = xmin, xmx = xmax, ymn = ymin, ymx = ymax, 
            resolution = resolution, crs = CRS("+init=epsg:27700"))
values(r) <- sample(c(0, 1), ncell(r), replace = TRUE)
r_reprojected <- projectRaster(r, crs = CRS("+init=epsg:4326"), method = "ngb")
writeRaster(r_reprojected, "/tmp/binary_raster_epsg4326.tif", format = "GTiff", overwrite = TRUE)

Then I fed this into gdal and used:

gdal_translate -of VRT -ot Byte -scale /tmp/binary_raster_epsg4326.tif temp.vrt
gdal2tiles.py --zoom=3-10 --processes=8 temp.vrt /tmp/dummy_tiles_WGS84

And the tiled data is here.

I then add this dummy data to the map with:

leaflet() %>%
  addTiles() %>%
  setView(-0.91431, 51.59701, zoom = 7) %>%
  addTileFolder(
    "/tmp/dummy_tiles_WGS84",
    tms = TRUE,
    layerId = NULL,
    group = NULL,
    attribution = NULL,
    options = leaflet::tileOptions(),
    data = leaflet::getMapData(.)
)

And the map looks like this:

image

So it is styling a bit differently to my previous image, but my question still remains around how to change this styling.

@tim-salabim
Copy link
Member

Thanks, I'll have a look

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

2 participants