Learn R Programming

maptiles (version 0.3.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
)

Value

A SpatRaster is returned.

Arguments

x

an sf, sfc, bbox, SpatRaster, SpatVerctor or SpatExtent object. If x is a SpatExtent it must express coordinates in lon/lat WGS84 (epsg:4326).

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

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
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 tiles from OSM, no labels
osmnolbl <- list(
  src = 'osmnolabel',
  q = 'https://{s}.tiles.wmflabs.org/osm-no-labels/{z}/{x}/{y}.png',
  sub = c('a','b', 'c'),
  cit = '© OpenStreetMap contributors.'
)
# dowload tiles and compose raster (SpatRaster)
nc_osmnolbl <- get_tiles(x = nc, provider = osmnolbl, crop = TRUE,
                         zoom = 6, verbose = TRUE)
# Plot the tiles
plot_tiles(nc_osmnolbl)

Run the code above in your browser using DataLab