2

I'm working with the ESA's landcover raster layer and ultimately want to display that data for the globe in a Leaflet Shiny app. Rendering such a massive file is impossible, so I've decided to create map tiles to display the data.

Creating the tiles was simple--I used the gdal2tiles tool in QGIS. Here's a quick look of the output, which is in a local directory on my computer: enter image description here

When I click the leaflet.html file, the tiles are rendered in my browser, like so:

enter image description here

Obviously the tiles are in working order. The problem is that I don't know how to render these tiles in Leaflet for R. I tried following this tutorial, but nothing is rendered when I altered my code to fit the example. I also explored answers from this StackOverflow question, but all of the answers seem several years out of date.

Here's the R code that I'm using to try to get the tiles to render in any way:

library(leaflet)

leaflet() %>% 
  setView(0, 0, zoom = 1) %>% 
  addTiles(urlTemplate = "http://my-username.github.io/tiles/{z}/{x}/{y}.png", 
           options = tileOptions(minZoom = 1, maxZoom = 2, tms = TRUE)) %>% 
  addCircles(lat = 0, lng = 0, radius = 100) #just to see if anything is rendering

This code renders the circle I've drawn, but nothing else.

Is there a way to render these tiles directly from my local machine? If not, how do I host these tiles so that they can be rendered in Leaflet for R? It seems like this should be pretty straightforward, but I can't figure it out!

Community
  • 1
  • 1
Lauren
  • 1,035
  • 1
  • 14
  • 32

1 Answers1

0

Figured it out. You have to use a "www" folder inside your Shiny directory. So in the question, I just had the folder "Tiles" and all of the tiled folders listed inside of it (0 - 7). Instead, move the Tiles folder inside a www directory (in my example, they are further moved into a folder called "map").

So instead of the above structure Tiles > x, it needs to be www > map > Tiles > x

leaflet() %>%
    addTiles(urlTemplate = "map/Tiles/{z}/{x}/{y}.png",
             option = tileOptions(tms = T, minZoom = 5, maxZoom = 9))
Lauren
  • 1,035
  • 1
  • 14
  • 32
  • no matter what i do, i have no tiles displayed (only the basemap). I have my tiles inside a www folder, in the app folder. I used gdal2tiles as well. – Sam Sep 05 '17 at 09:03
  • 1
    This article to the rescue; http://www.projsolution.com/a107-208908-r. i added a ResourcePath and all was fine :) – Sam Sep 05 '17 at 09:59
  • Glad to hear it @Sam! Once you get to a certain level of sophistication in Leaflet mapping in R/Shiny, you really have to figure stuff out on your own. It can be a struggle! I always post to SO and update if I figure it out myself! – Lauren Sep 05 '17 at 13:39