get_tiles: Get basemap tiles from map servers

View source: R/get_tiles.R

get_tilesR Documentation

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,
  project = TRUE,
  verbose = FALSE,
  apikey,
  cachedir,
  forceDownload = FALSE
)

Arguments

x

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

provider

tile server to get the tiles from. It can be one of the builtin providers (see Details for the list) or a named list produced by create_provider (see Examples).

zoom

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.

project

if TRUE, the output is projected to the crs of x. If FALSE the output uses "EPSG:3857" (Web Mercator).

verbose

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

apikey

API key. Not needed for Thunderforest or Stadia servers if environment variables named "THUNDERFOREST_MAPS" or "STADIA_MAPS" are set.

cachedir

name of a folder 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 in the OpenStreetMap wiki: https://wiki.openstreetmap.org/wiki/Zoom_levels.

Providers:
"OpenStreetMap", "OpenStreetMap.DE", "OpenStreetMap.France", "OpenStreetMap.HOT", "OpenTopoMap",
"Stadia.StamenToner", "Stadia.StamenTonerBackground", "Stadia.StamenTonerLines", "Stadia.StamenTonerLabels", "Stadia.StamenTonerLite", "Stadia.StamenWatercolor", "Stadia.StamenTerrain", "Stadia.StamenTerrainBackground", "Stadia.StamenTerrainLabels",
"Esri.WorldStreetMap", "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"

Value

A SpatRaster is returned.

Examples

## 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)

# Create a provider from a custom url
osm_tiles <- create_provider(
  name = "osm_tiles",
  url = "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
  citation = "© OpenStreetMap contributors."
)
# Download tiles and compose raster (SpatRaster)
nc_osm2 <- get_tiles(
  x = nc, provider = osm_tiles, crop = FALSE,
  zoom = 6, project = FALSE, verbose = TRUE
)
# Plot the tiles
plot_tiles(nc_osm2)
# Add attribution
mtext(get_credit(osm_tiles), side = 1, line = -1)

## End(Not run)

maptiles documentation built on Oct. 30, 2024, 9:09 a.m.