Skip to contents

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

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

esp_get_attributions gets the attribution of a tile provider defined as the type argument.

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
)

esp_get_attributions(type, options = NULL)

Arguments

x

An sf or sfc object.

type

This argument can be either:

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, the results will be cropped 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. Provides transparent background, if supported.

mask

logical. TRUE if the result should be masked to x. See terra::mask().

update_cache

logical. Should the cached file be refreshed? Default is FALSE. When set to TRUE, it will force a new download.

cache_dir

character string. A path to a cache directory. See Caching strategies section in esp_set_cache_dir().

verbose

logical. If TRUE displays informational messages.

options

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

Value

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

Details

Zoom levels are described on the OpenStreetMap wiki:

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

For a complete list of providers see esp_tiles_providers.

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

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

See also

terra::rast(), esp_tiles_providers, maptiles::get_tiles()

giscoR::gisco_attributions()

Other functions for creating maps with images: addProviderEspTiles(), esp_make_provider()

Examples

# \dontrun{

# 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)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter

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 WMS provided

custom_wms <- esp_make_provider(
  id = "an_id_for_caching",
  q = "https://idecyl.jcyl.es/geoserver/ge/wms?",
  service = "WMS",
  version = "1.3.0",
  format = "image/png",
  layers = "geolog_cyl_litologia"
)

custom_wms_tile <- esp_get_tiles(segovia, custom_wms)

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


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

# }