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:
When I click the leaflet.html file
, the tiles are rendered in my browser, like so:
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!
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))
来源:https://stackoverflow.com/questions/41620176/how-to-render-custom-map-tiles-created-with-gdal2tiles-in-leaflet-for-r