Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

shiny - How to render custom map tiles created with gdal2tiles in Leaflet for R?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...