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.
An example of how you can include several tiles to create a static map. We focus here on the earthquakes of the last 365 days in the island of Tenerife. Note that the tile is actively maintained so the final plot based on a static file would vary depending on the date.
In this example, it is important to set moveCAN = FALSE
, otherwise Tenerife would be geographically displaced while the imagery won’t.
library(mapSpain)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(cartography)
<- esp_get_nuts(region = "Tenerife", moveCAN = FALSE)
Tenerife <- st_transform(Tenerife, 4083)
Tenerife
<- esp_getTiles(Tenerife, "MDT.Relieve", bbox_expand = 0.2)
Base <-
Earthquake esp_getTiles(Tenerife, "Geofisica.Terremotos365dias", bbox_expand = 0.2)
tilesLayer(Base)
plot(st_geometry(Tenerife),
col = adjustcolor("red", alpha.f = 0.4),
add = TRUE)
tilesLayer(Earthquake, add = TRUE)
Static tiles could be loaded with or without an alpha value, that controls the transparency of the object:
<- esp_get_ccaa("Madrid", epsg = 3857)
Madrid
# Example without transparency
<- esp_getTiles(Madrid, "IGNBase.Gris")
BG <-
ADIF.opaque esp_getTiles(Madrid, "RedTransporte.Ferroviario",
transparent = FALSE)
tilesLayer(BG)
plot(
st_geometry(Madrid),
col = adjustcolor("red", alpha.f = 0.2),
add = TRUE,
border = NA
)tilesLayer(ADIF.opaque, add = TRUE)
# Example with transparency
<- esp_getTiles(Madrid,
ADIF.alpha "RedTransporte.Ferroviario")
tilesLayer(BG)
plot(
st_geometry(Madrid),
col = adjustcolor("red", alpha.f = 0.2),
add = TRUE,
border = NA
)tilesLayer(ADIF.alpha, add = TRUE)
Another nice feature is the ability of masking the tiles, so more advanced maps can be plotted:
<- esp_get_ccaa("Asturias", epsg = 3857)
Asturias
<- esp_getTiles(Asturias, "IDErioja", bbox_expand = 0.3)
Basemap
<- esp_getTiles(Asturias, "PNOA", mask = TRUE)
Mask
tilesLayer(Basemap)
tilesLayer(Mask, add = TRUE)
mapSpain
provides a plugin to be used with the leaflet
package. Here you can find the leaflet versions of some of the previous examples:
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
<- leaflet(Tenerife.leaf,
m 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]) )
<- esp_get_ccaa("Madrid", epsg = 4326)
Madrid.leaflet
<- leaflet(Madrid.leaflet, width = "100%") %>%
m2 addProviderEspTiles("IGNBase.Gris") %>%
addPolygons(color = NA,
fillColor = "red",
group = "Polygon") %>%
addProviderEspTiles("RedTransporte.Ferroviario",
group = "Railway")
# Add layer control
%>% addLayersControl(
m2 overlayGroups = c("Polygon", "Railway"),
options = layersControlOptions(collapsed = FALSE)
)
The data frame leaflet.providersESP.df
includes the data of the available providers you can use on functions described above. This data frame includes all the parameters needed to replicate the API request. In this summary only the parameter url_static
is shown:
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
#> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
#> [5] LC_TIME=Spanish_Spain.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] reactable_0.2.3 leaflet_2.0.4.1 cartography_2.4.2 sf_0.9-7
#> [5] mapSpain_0.2.0
#>
#> loaded via a namespace (and not attached):
#> [1] tidyselect_1.1.0 xfun_0.21 bslib_0.2.4 purrr_0.3.4
#> [5] lattice_0.20-41 vctrs_0.3.6 generics_0.1.0 htmltools_0.5.1.1
#> [9] yaml_2.2.1 utf8_1.1.4 rlang_0.4.10 e1071_1.7-4
#> [13] jquerylib_0.1.3 pillar_1.5.0 reactR_0.4.4 glue_1.4.2
#> [17] DBI_1.1.1 slippymath_0.3.1 geojsonsf_2.0.1 sp_1.4-5
#> [21] lifecycle_1.0.0 stringr_1.4.0 rgeos_0.5-5 raster_3.4-5
#> [25] htmlwidgets_1.5.3 codetools_0.2-18 evaluate_0.14 knitr_1.31
#> [29] crosstalk_1.1.1 class_7.3-18 fansi_0.4.2 highr_0.8
#> [33] giscoR_0.2.4-9000 Rcpp_1.0.6 KernSmooth_2.23-18 classInt_0.4-3
#> [37] jsonlite_1.7.2 countrycode_1.2.0 png_0.1-7 digest_0.6.27
#> [41] stringi_1.5.3 dplyr_1.0.4 grid_4.0.3 rgdal_1.5-23
#> [45] tools_4.0.3 magrittr_2.0.1 sass_0.3.1 tibble_3.0.6
#> [49] crayon_1.4.1 pkgconfig_2.0.3 ellipsis_0.3.1 assertthat_0.2.1
#> [53] rmarkdown_2.7 R6_2.5.0 units_0.6-7 compiler_4.0.3