Search code examples
rshinyleafletgdalr-leaflet

Rendering large tile dataset


I have spent considerable time troubleshooting this problem on SO but haven't been able to find a suitable answer. In short, I cannot view locally stored map tiles using R Shiny and leaflet.

I have used gdal2tiles.py to create a nested directory of tiles from an original high-resolution .tif file projected in EPSG:27700. The output of the tiling process can be viewed with the function's auto-generated .html files (i.e. googlemaps.html, leaflet.html, openlayers.html; see below for a link to a GitHub repo with the tiles and leaflet.html for viewing). I've therefore concluded that gdal2tiles.py is correctly generating the tiles which can be displayed in a browser.

The problem comes when attempting to use leaflet and R Shiny. I have followed the following instructions here and here to point leaflet to my local tiles but nothing is displayed either within R Studio's viewer or my browser. It's frustrating because there are no errors printed to the R console when running the code. The code I am using, based on the previous SO answers I have just linked to looks like:

library(leaflet)
addResourcePath("mytiles", "C:/path/to/my/tiles/")

leaflet() %>% 
  addTiles(urlTemplate = "mytiles/{z}/{x}/{y}.png") %>% 
  addMouseCoordinates()

The folders and files are named correctly as per the syntax {z}/{x}/{y} e.g. the directory structure looks like: path/to/my/files/6/30/42.png. I have also uploaded my files to GitHub and passed the repo URL to urlTemplate but to no avail. If it helps anyone, the tiles I am using can be found here. The repo is rather hefty in terms of the numbers of files and folders (the input raster is at 25m resolution and I require plenty of zoom levels), but not in size.

I have noticed--when zooming into my tiles in leaflet.html--that there are multiple shades of grey. This is weird because the input .tif was binary; either forest is present (1) or absent (NoData or NA). I have included a screenshot and wonder if this is a rendering problem within Chrome or whether this is causing the tiles to not show in Shiny. The screenshot can be viewed here.

Could someone please help me display my tiles within R Shiny using local files? I will accept an answer if the best solution is to to GitHub-hosted tiles but my preference is to display locally-stored files.


Solution

  • I have a solution from the package's Github repo issues page. For anyone who may experience issues in the future, it turns out that the flag tms=TRUE is required in tileOptions. I thank Barret Schloerke for his help with this. His full (working) code is copied below and can be found here on Github.

    library(leaflet)
    library(rgdal)
    library(leaflet.extras)
    library(mapview)
    
    leaflet() %>%
      addMouseCoordinates() %>%
    
      # Set high zoom level as I have only created tiles at zoom level 6 for a quick example. If you can get this to display, it'll be tough to discern as it's quite zoomed out!
    
      # This should center the view somewhere over the UK where my tiles have been created  
      setView(0, 51.5135085, zoom = 6) %>%
      # Added toner provider tiles as the white background makes it easier to see my custom tiles
      addProviderTiles(group = "Toner", providers$Stamen.Toner) %>%
      addTiles(group="test", urlTemplate = "https://simon-tarr.github.io/tilestest/tiles/{z}/{x}/{y}.png",
               options = tileOptions(tms = TRUE, minZoom = 6, maxZoom = 6)) %>% 
      addLayersControl(
        overlayGroups = c("test"),
        options = layersControlOptions(collapsed = FALSE)
    
      )