Skip to contents

mapSpain provides a powerful interface for working with imagery. mapSpain can download static files as .png or .jpeg files (depending on the Web Map Service) and use them along your shapefiles.

mapSpain also includes a plugin for R leaflet package, that allows you to include several basemaps on your interactive maps.

The services are implemented via the leaflet plugin leaflet-providersESP. You can check a display of each provider on the previous link.

Static tiles

An example of how you can include several tiles to create a static map. We focus here on layer provided by La Rioja’s Infraestructura de Datos Espaciales (IDERioja).

When working with imagery, it is important to set moveCAN = FALSE, otherwise the images for the Canary Islands won’t be accurate.

library(mapSpain)
library(sf)
library(ggplot2)
library(tidyterra)

# Logroño

lgn_borders <- esp_get_munic(munic = "Logroño")

# Convert to Mercator (EPSG:3857) as a general advice when working with tiles
lgn_borders <- st_transform(lgn_borders, 3857)

tile_lgn <- esp_getTiles(lgn_borders, "IGNBase.Todo", bbox_expand = 0.5)

ggplot(lgn_borders) +
  geom_spatraster_rgb(data = tile_lgn) +
  geom_sf(fill = NA, linewidth = 2, color = "blue")

Alpha value on tiles

Some tiles could be loaded with or without an alpha value, that controls the transparency of the object:

madrid <- esp_get_ccaa("Madrid", epsg = 3857)

# Example without transparency

basemap <- esp_getTiles(madrid, "IGNBase.Gris", zoommin = 1)
tile_opaque <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = FALSE)

ggplot() +
  geom_spatraster_rgb(data = basemap) +
  geom_spatraster_rgb(data = tile_opaque) +
  theme_void()

Now let’s check the same code using the tranparent = TRUE option:

# Example with transparency

tile_alpha <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = TRUE)

# Same code than above for plotting
ggplot() +
  geom_spatraster_rgb(data = basemap) +
  geom_spatraster_rgb(data = tile_alpha) +
  theme_void()

Now the two tiles overlaps with the desired alpha value.

Masking tiles

Another nice feature is the ability of masking the tiles, so more advanced maps can be plotted:

rioja <- esp_get_prov("La Rioja", epsg = 3857)

basemap <- esp_getTiles(rioja, "IGNBase.Gris", bbox_expand = 0.1, zoommin = 1)

masked <- esp_getTiles(rioja, "IGNBase.Todo", mask = TRUE, zoommin = 1)

ggplot() +
  geom_spatraster_rgb(data = basemap, maxcell = 10e6) +
  geom_spatraster_rgb(data = masked, maxcell = 10e6)

Dynamic maps with Leaflet

mapSpain provides a plugin to be used with the leaflet package. Here you can find some quick examples:

Earthquakes in Tenerife (last year)

library(leaflet)

tenerife_leaf <- esp_get_nuts(
  region = "Tenerife", epsg = 4326,
  moveCAN = FALSE
)


bbox <- as.double(round(st_bbox(tenerife_leaf) + c(-1, -1, 1, 1), 2))

# Start leaflet
m <- leaflet(tenerife_leaf,
  elementId = "tenerife-earthquakes",
  width = "100%", height = "60vh",
  options = leafletOptions(minZoom = 9, maxZoom = 18)
)

# Add layers
m <- m %>%
  addProviderEspTiles("IDErioja.Relieve") %>%
  addPolygons(color = NA, fillColor = "red", group = "Polygon") %>%
  addProviderEspTiles("Geofisica.Terremotos365dias",
    group = "Earthquakes"
  )

# Add additional options
m %>%
  addLayersControl(
    overlayGroups = c("Polygon", "Earthquakes"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  setMaxBounds(bbox[1], bbox[2], bbox[3], bbox[4])

Population density in Spain

A map showing the population density of Spain as of 2019:

munic <- esp_get_munic_siane(
  year = 2019,
  epsg = 4326,
  moveCAN = FALSE,
  rawcols = TRUE
)

# Get area in km2 from siane munic
# Already on the shapefile

munic$area_km2 <- munic$st_area_sh * 10000

# Get population

pop <- mapSpain::pobmun19

# Paste
munic_pop <- merge(munic, pop[, c("cmun", "cpro", "pob19")],
  by = c("cmun", "cpro"),
  all.x = TRUE
)

munic_pop$dens <- munic_pop$pob19 / munic_pop$area_km2
munic_pop$dens_label <- prettyNum(round(munic_pop$dens, 2),
  big.mark = ".",
  decimal.mark = ","
)

# Create leaflet
bins <- c(0, 10, 25, 100, 200, 500, 1000, 5000, 10000, Inf)


pal <- colorBin("inferno", domain = munic_pop$dens, bins = bins, reverse = TRUE)

labels <- sprintf(
  "<strong>%s</strong><br/><em>%s</em><br/>%s pers. / km<sup>2</sup>",
  munic_pop$rotulo,
  munic_pop$ine.prov.name,
  munic_pop$dens_label
) %>% lapply(htmltools::HTML)


leaflet(elementId = "SpainDemo", width = "100%", height = "60vh") %>%
  setView(lng = -3.684444, lat = 40.308611, zoom = 5) %>%
  addProviderEspTiles("IGNBase.Gris") %>%
  addPolygons(
    data = munic_pop,
    fillColor = ~ pal(dens),
    fillOpacity = 0.6,
    color = "#44444",
    weight = 0.5,
    smoothFactor = .1,
    opacity = 1,
    highlightOptions = highlightOptions(
      color = "white",
      weight = 1,
      bringToFront = TRUE
    ),
    popup = labels
  ) %>%
  addLegend(
    pal = pal, values = bins, opacity = 0.7,
    title = "<small>Pop. Density km<sup>2</sup></small><br><small>(2019)</small>",
    position = "bottomright"
  )

Providers available

The list esp_tiles_providers includes the data of the available providers you can use on functions described above. This list includes all the parameters needed to replicate the API request. See the static url of each provider: