Learn R Programming

maptiles (version 0.1.0)

get_tiles: Get basemap tiles from map servers

Description

Get map tiles based on a spatial object extent. Maps can be fetched from various map servers.

Usage

get_tiles(
  x,
  provider = "OpenStreetMap",
  zoom,
  crop = FALSE,
  verbose = FALSE,
  apikey,
  cachedir,
  forceDownload = FALSE
)

Arguments

x

an sf or sfc object.

provider

the tile server from which to get the map. It can be a name (see Details for providers) or a named list like this one: provider = list(src = "name of the source", q = "server address", sub = "subdomains", cit = "how to cite the tiles") (see Examples).

zoom

the zoom level (see Details).

crop

TRUE if results should be cropped to the specified x extent, FALSE otherwise. If x is an sf object with one POINT, crop is set to FALSE.

verbose

if TRUE, tiles filepaths, zoom level and citation are displayed.

apikey

API key, needed for Thunderforest servers

cachedir

name of a directory used to cache tiles. If not set, tiles are cached in a tempdir folder.

forceDownload

if TRUE, existing cached tiles may be overwritten

Value

A SpatRaster is returned.

Details

Zoom levels are described on the OpenStreetMap wiki: https://wiki.openstreetmap.org/wiki/Zoom_levels. Providers: "OpenStreetMap.MapnikBW", "OpenStreetMap", "OpenStreetMap.DE", "OpenStreetMap.France", "OpenStreetMap.HOT", "Stamen.Toner", "Stamen.TonerBackground", "Stamen.TonerHybrid", "Stamen.TonerLines", "Stamen.TonerLabels", "Stamen.TonerLite", "Stamen.Watercolor", "Stamen.Terrain", "Stamen.TerrainBackground", "Stamen.TerrainLabels", "Esri.WorldStreetMap", "Esri.DeLorme", "Esri.WorldTopoMap", "Esri.WorldImagery", "Esri.WorldTerrain", "Esri.WorldShadedRelief", "Esri.OceanBasemap", "Esri.NatGeoWorldMap", "Esri.WorldGrayCanvas", "CartoDB.Positron", "CartoDB.PositronNoLabels", "CartoDB.PositronOnlyLabels", "CartoDB.DarkMatter", "CartoDB.DarkMatterNoLabels", "CartoDB.DarkMatterOnlyLabels", "CartoDB.Voyager", "CartoDB.VoyagerNoLabels", "CartoDB.VoyagerOnlyLabels", "Thunderforest.OpenCycleMap", "Thunderforest.Transport", "Thunderforest.TransportDark", "Thunderforest.SpinalMap", "Thunderforest.Landscape", "Thunderforest.Outdoors", "Thunderforest.Pioneer", "Thunderforest.MobileAtlas", "Thunderforest.Neighbourhood", "OpenTopoMap", "HikeBike", "Wikimedia",

Examples

Run this code
# NOT RUN {
library(sf)
library(maptiles)
nc <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc_osm <- get_tiles(nc, crop = TRUE, zoom = 6)
plot_tiles(nc_osm)

# Download esri tiles
fullserver <- paste("https://server.arcgisonline.com/ArcGIS/rest/services",
                   "Specialty/DeLorme_World_Base_Map/MapServer",
                   "tile/{z}/{y}/{x}.jpg",
                   sep = "/")
esri <-  list(
  src = 'esri',
  q = fullserver,
  sub = NA,
  cit = 'Tiles: Esri; Copyright: 2012 DeLorme'
)
nc_ESRI <- get_tiles(x = nc, provider = esri, crop = TRUE,
                        verbose = TRUE, zoom = 6)
# Plot the tiles
plot_tiles(nc_ESRI)
txt <- esri$cit
mtext(text = txt, side = 1, adj = 0, cex = .9, font = 3)
# }

Run the code above in your browser using DataLab