esp_get_tiles: Get static map tiles from public administrations of Spain

View source: R/esp-get-tiles.R

esp_get_tilesR Documentation

Get static map tiles from public administrations of Spain

Description

Get static map tiles based on a spatial object. Maps can be fetched from WMS and WMTS providers.

This function is an implementation of the JavaScript plugin leaflet-providersESP v1.3.3.

Usage

esp_get_tiles(
  x,
  type = "IDErioja",
  zoom = NULL,
  zoommin = 0,
  crop = TRUE,
  res = 512,
  bbox_expand = 0.05,
  transparent = TRUE,
  mask = FALSE,
  update_cache = FALSE,
  cache_dir = NULL,
  verbose = FALSE,
  options = NULL
)

Arguments

x

An sf or sfc object.

type

This argument can be one of:

  • The name of one of the pre-defined providers (see esp_tiles_providers).

  • A list with two named elements id and q with your own arguments. See esp_make_provider() and examples.

zoom

Character string or number. Only valid for WMTS providers, zoom level to be downloaded. If NULL, it is determined automatically. If set, it overrides zoommin. If a single sf POINT and zoom = NULL, the function sets a zoom level of 18. See Details.

zoommin

Character string or number. Delta on default zoom. The default value is designed to download fewer tiles than you probably want. Use 1 or 2 to increase the resolution.

crop

Logical. If TRUE, crop results to the specified x extent. If x is an sf object with one POINT, crop is set to FALSE. See terra::crop().

res

Character string or number. Only valid for WMS providers. Resolution (in pixels) of the final tile.

bbox_expand

Number. Expansion percentage of the bounding box of x.

transparent

Logical. Whether to use a transparent background, if supported.

mask

Logical. TRUE to mask the result to x. See terra::mask().

update_cache

Logical. If TRUE, refreshes the cached file and forces a new download. Defaults to FALSE.

cache_dir

Character string. A path to a cache directory. See Caching.

verbose

A logical value indicating whether to display informational messages.

options

A named list containing additional options to pass to the query.

Details

Zoom levels are described on the OpenStreetMap wiki:

zoom area to represent
0 whole world
3 large country
5 state
8 county
10 metropolitan area
11 city
13 village or suburb
16 streets
18 some buildings, trees

For a complete list of providers, see esp_tiles_providers.

Most WMS/WMTS providers provide tiles on "EPSG:3857". If the tile looks deformed, try projecting x first:

x <- sf::st_transform(x, 3857)

Value

A SpatRaster with 3 (RGB) or 4 (RGBA) layers, depending on the provider. See terra::rast().

Caching

Functions that download data store files in cache_dir. When cache_dir is NULL, they use the active package cache, which defaults to a temporary directory. Set update_cache = TRUE to replace an existing cached file. See Caching strategies in esp_set_cache_dir() to configure a persistent cache.

Source

https://dieghernan.github.io/leaflet-providersESP/, a plugin for leaflet, v1.3.3.

See Also

  • terra::rast().

  • esp_tiles_providers.

Static map tiles and imagery: addProviderEspTiles(), esp_get_attributions(), esp_make_provider()

Examples


## Not run: 

# This example downloads data to your local computer.

segovia <- esp_get_prov_siane("segovia", epsg = 3857)
tile <- esp_get_tiles(segovia, "IGNBase.Todo")

library(ggplot2)
library(tidyterra)

ggplot(segovia) +
  geom_spatraster_rgb(data = tile, maxcell = Inf) +
  geom_sf(fill = NA, linewidth = 1)

# Another provider.

tile2 <- esp_get_tiles(segovia, type = "MDT")

ggplot(segovia) +
  geom_spatraster_rgb(data = tile2, maxcell = Inf) +
  geom_sf(fill = NA, linewidth = 1, color = "red")

# A custom WMTS provider.

custom_wmts <- esp_make_provider(
  id = "cyl_wmts",
  q = "https://www.ign.es/wmts/pnoa-ma?",
  service = "WMTS",
  layer = "OI.OrthoimageCoverage"
)

custom_wmts_tile <- esp_get_tiles(segovia, custom_wmts)

autoplot(custom_wmts_tile, maxcell = Inf) +
  geom_sf(data = segovia, fill = NA, color = "white", linewidth = 1)

# Example from https://leaflet-extras.github.io/leaflet-providers/preview/.
cartodb_dark <- list(
  id = "CartoDB_DarkMatter",
  q = "https://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png"
)
cartodb_dark_tile <- esp_get_tiles(segovia, cartodb_dark,
  zoommin = 1,
  update_cache = TRUE
)

autoplot(cartodb_dark_tile, maxcell = Inf) +
  geom_sf(data = segovia, fill = NA, color = "white", linewidth = 1)

## End(Not run)


mapSpain documentation built on Aug. 31, 2026, 5:07 p.m.