Introduction

The tiler R package can be used to render spatial data sets as tiles. The advantage of rendering spatial data as tiles for use in web applications is that the tile format reduces the amount of data that users need to download to view the data. The disadvantage, however, is that the spatial data first needs to be converted to the tile format. Depending on the size of your data and the computational resources that you have available, tiling your data could take a very long time. I would recommend trying out the normal mapping functions available in the leaflet R package, and make sure that you really need to use tiling before out trying out this package.

Installation

The tiler R package requires python (version 2.7+) installed. It also requires that the gdal python package is also installed. I have found setting up this software on Windows 7 to be quite a nightmare. Therefore, the tiler R package is only available on Unix systems for the moment. The easiest way to setup a system on Ubuntu (version 16+) is using the following system commands.

sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
sudo apt-get install gdal-bin python-gdal

Note that this may affect previously installed versions of the rgdal, sf, and maptools R packages. If these R packages—or indeed any other packages—fail to load after running the previous system commands, you will need to reinstall the R packages to use them.

Example usage

The tiler R package is designed to be used in conjunction with the leaflet R package. Below are some examples using the tiler R package.

Leaflet

Here is an example of using the tiler package to visualize a small raster data set. Although the data set used in this example is rather small, the tiles function can be used to create tile data for large raster data sets. Note that you will need to use options = tileOptions(tms = TRUE) when adding tiles to a map. If you forget to include this option, then the map will not show the spatial data.

# load packages
library(tiler)
library(leaflet)

# load example data set
data(netherlands, package = "mapmisc")

# create leaflet map and visualize data using tiles
map <- leaflet() %>%
       setView(lng = 5.75560, lat = 50.94723, zoom = 12) %>%
       addProviderTiles("Esri.WorldImagery", group = "Basemap") %>%
       addTiles(tiles(nldElev, output_dir = tempdir(), zoom = "10-16"),
                group = "Elevation",
                options = tileOptions(minZoom = 5, maxZoom = 16,
                                      tms = TRUE)) %>%
       addLayersControl(baseGroups = "Basemap", overlayGroups = "Elevation",
                        options = layersControlOptions(collapsed = FALSE))

# render map
print(map)

Shiny

Now let’s build a shiny application that displays the spatial data set. Note that we have set shiny = TRUE to tell the tiles function that the tiles function that we are running it inside a shiny application. Also, we have put the tiles function in a start function so the tiles are made while initializing the application (equivalent to putting code in a “global.R” R shiny script).

# load packages
library(shiny)
library(leaflet)
library(tiler)

# define function to build tiles on start
start <- function() {
  data(netherlands, package = "mapmisc")
  path <<- tiles(nldElev, output_dir = tempdir(), zoom = "10-16", shiny = TRUE)
}

# define ui
ui <- fluidPage(leafletOutput("map"))

# define server function
server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>%
    setView(lng = 5.75560, lat = 50.94723, zoom = 12) %>%
    addProviderTiles("Esri.WorldImagery", group = "Basemap") %>%
    addTiles(path, group = "Elevation",
             options = tileOptions(minZoom = 5, maxZoom = 16, tms = TRUE)) %>%
    addLayersControl(baseGroups = "Basemap", overlayGroups = "Elevation",
                     options = layersControlOptions(collapsed = FALSE))
  })
}

# launch shiny app
shinyApp(ui, server, onStart = start)