4

I have created some tiles out of a very large raster using the Qtiles plugin in Qgis. I have saved them to a local directory on my computer, and now want to render them in a leaflet map using R.

The addTiles function passes a URL, but doesn't seem to work with a local filepath. In a different post (How to render custom map tiles created with gdal2tiles in Leaflet for R?), Lauren recommends using a www folder inside the shiny directory. Firstly, I'm not 100% sure what is meant by that, and secondly I don't know if that solution is applicable to what I'm trying to do; all I want to do is render these tiles in a leaflet map object and save it locally as html. Is it possible to do what I am attempting?

The code looks something like this:

library(leaflet)
map <- leaflet()
map <- addProviderTiles(map, "CartoDB.Positron")
map <- addTiles(map, "C:/mapTiles/level100Tiles/{z}/{x}/{y}.png")

Is there a different leaflet function for this specific purpose that I am not aware of? Or is it just not something that's done?

Thanks :)

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
gvan
  • 473
  • 4
  • 15
  • My recommendation would be to open a free github repository and post them out there for your application to query against because you will need them hosted when the app is live in a website and not able to access your local drive. you can make it work locally, but then the map will only work on your machine. – sconfluentus Aug 16 '17 at 20:34
  • Thanks for your reply! If I only want to use the map locally, how would I go about this? The map isn't for an app, but for sharing information (as an html widget) internally -- i.e. I'm thinking I could keep the tiles on the lan. Basically, I want to know how to get the tiles using a conventional filepath as opposed to a url. – gvan Aug 17 '17 at 13:02
  • you need to set a path to your local tiles...I have never done that, so it is outside of my scope...sorry. – sconfluentus Aug 17 '17 at 21:19
  • do you have an answer for rendering tiles from a local directory in a Shiny App? all i get is a blank map. Using addTiles(urlTemplate = "map/tiles/{z}/{x}/{y}.png"), with the tile data inside a www folder. – Sam Sep 05 '17 at 08:55

1 Answers1

4

Add a ResourcePath inside server and it'll work, no need for the www folder anywhere. Source.

server <- function(input, output, session) {
    addResourcePath("mytiles", "C:/Users/.../mapTiles")
    output$map <- renderLeaflet({
      leaflet() %>% 
        addTiles(urlTemplate = "/mytiles/{z}/{x}/{y}.png")
    })
Sam
  • 1,400
  • 13
  • 29
  • Hi Sam. The source link is not working anymore. Is the "mytiles" just a folder name? It is not working for me. Is there a reproducible example somewhere? Thanks! – ayasugihada Dec 04 '19 at 09:21
  • Hi, ah that's annoying. Think of 'mytiles' as an object that is the path in `addResourcePath` - this path should be the folder in which the tile structure is contained. In the `addTiles` function you are linking to the local path that contains the tiles (which should be in the correct format). – Sam Dec 04 '19 at 10:34
  • Thanks for the reply, Sam. I have added SO question https://stackoverflow.com/questions/59174298/r-using-addresourcepath-for-rendering-local-leaflet-tiles . If you have a bit of time at hand, I'd appreciate some help. – ayasugihada Dec 04 '19 at 10:54