mapSpain provides a powerful interface for working with imagery. mapSpain can download static files as .png orjpeg 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(tmap)

# Logroño

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

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

tile_LGN <- esp_getTiles(LGN, "IDErioja", bbox_expand = 0.5)

tm_shape(tile_LGN, raster.downsample = FALSE) +
  tm_rgb() +
  # Mix with a sf polygon
  tm_shape(LGN) +
  tm_borders(lwd = 2, lty = "dotted")

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

BG <- esp_getTiles(Madrid, "IGNBase.Gris")
ADIF.opaque <-
  esp_getTiles(Madrid, "RedTransporte.Ferroviario",
               transparent = FALSE)

tm_shape(BG, raster.downsample = FALSE) +
  tm_rgb() +
  tm_shape(ADIF.opaque, raster.downsample = FALSE) +
  tm_rgb()

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

# Example with transparency

ADIF.alpha <- esp_getTiles(Madrid,
                           "RedTransporte.Ferroviario",
                           transparent = TRUE)

# Same code than above for plotting

tm_shape(BG, raster.downsample = FALSE) +
  tm_rgb() +
  tm_shape(ADIF.alpha, raster.downsample = FALSE) +
  tm_rgb()

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


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

Basemap <- esp_getTiles(LaRioja, bbox_expand = 0.1, zoom = 10)

Mask <- esp_getTiles(LaRioja, "PNOA", mask = TRUE, , zoom = 10)

tm_shape(Basemap, raster.downsample = FALSE) +
  tm_rgb() +
  tm_shape(Mask, raster.downsample = FALSE) +
  tm_rgb()

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,
             width = "100%",
             options = leafletOptions(minZoom = 9,
                                      maxZoom = 18))

# Add layers
m <- m %>%
  addProviderEspTiles("MDT.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


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/>%s pers. / km<sup>2</sup>",
  munic_pop$rotulo,
  munic_pop$dens_label
) %>% lapply(htmltools::HTML)



leaflet() %>%
  setView(
    lng = -3.684444,
    lat = 40.308611,
    zoom = 6
  ) %>%
  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"
  )