7

Is it possible to load offline/local leaflet map tiles within a Shiny app? I am able to load the tiles in an interactive R session as shown here, but I now want to try and load them for use in a Shiny app. Here's an example of what I have so far. I'm thinking it has something to do with Shiny running through an IP and port and needing to load the tiles through an IP and port as well. I've tried a few things to change IPs and ports (making them the same) as explained here but haven't figured out anything that works. I can also get it to work using online tiles, but I need it to work with local map tiles.

library(shiny)
library(leaflet)
library(RColorBrewer)
library(RgoogleMaps)

options(shiny.port = 8000)

  (bwi <- getGeoCode("BWI;MD"))

df <- as.data.frame(rbind(bwi))
df$col <- c("orange")
df$name <- c("BWI")

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = df$col
)
#################################################################################

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                style = "padding: 8px; background: #FFFFEE; opacity:.9",
    checkboxInput("markers", "Show Markers?", TRUE)
  )
)    
#################################################################################

server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(urlTemplate = "http:/localhost:8000/C:/Users/OTAD USER/Documents/mapTiles/ESRIWorldTopoMap/{z}_{x}_{y}.png") %>%
      setView(lat = bwi[1], lng = bwi[2], zoom = 8)
  })

  observe({
    proxy <- leafletProxy("map", data = df)

    # Remove/show any markers
    proxy %>% clearMarkers()
    if (input$markers) {
      proxy %>% addAwesomeMarkers(lat = df$lat, lng = df$lon,
                                  icon = icons, label = df$name)
    }
  })
}

#Put the ui and server together and run
runApp(shinyApp(ui = ui, 
         server = server), launch.browser=TRUE
)
Community
  • 1
  • 1
Jake
  • 510
  • 11
  • 19
  • ,need to do something similar. Where did you download the tiles from? – Dhiraj Jun 09 '19 at 04:49
  • @Dhiraj, The `GetMapTiles` function in the `RgoogleMaps` package helps you download tiles from a few different places (see the `urlBase` argument). I think I actually downloaded from https://services.arcgisonline.com/arcgis/rest/services specifically the World Topo Map tiles, but in order to do that using `GetMapTiles` I had to modify the code in that function a little because that's not one of the accepted `urlBase` argument inputs. – Jake Jun 18 '19 at 13:02
  • @Jake do you have an example of what you used? – Anonymous coward Sep 30 '20 at 21:54
  • @Jake do you still have the code to download tiles from ArcGIS? – Brooke Gibbons Apr 20 '22 at 02:11
  • @BrookeGibbons I found your question and provided an answer directly to that. Hope it helps! For anyone else the link to that is: https://stackoverflow.com/questions/71933475/is-there-a-way-to-use-esri-world-imagery-tiles-offline-in-leaflet-map-in-shiny – Jake Apr 20 '22 at 19:04

1 Answers1

10

1- You have to authorize shiny to serve tiles in that folder by providing an "alias" on the ressource with addResourcePath

2- then use that alias as the base URL in addTiles

server <- function(input, output, session) {
    addResourcePath("mytiles", "C:/Users/OTAD USER/Documents/mapTiles/ESRIWorldTopoMap")
    output$map <- renderLeaflet({
      leaflet() %>% 
        addTiles(urlTemplate = "/mytiles/{z}_{x}_{y}.png") %>%
        setView(lat = bwi[1], lng = bwi[2], zoom = 8)
    })
...
HubertL
  • 19,246
  • 3
  • 32
  • 51