diff --git a/NEWS.md b/NEWS.md index 66f7a793..35b68bf3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,9 @@ # mapSpain (development version) - Fix a bug on `esp_get_prov_siane()` when selecting columns #115. +- New argument `type` in `esp_get_comarcas()`. Now it is possible to extract + different types of comarcas: Official (IGN), statistical (INE), agrarian or + livestock comarcas (MAPA). # mapSpain 0.9.2 diff --git a/R/esp_get_comarca.R b/R/esp_get_comarca.R index fbbeab13..12d2bfc8 100644 --- a/R/esp_get_comarca.R +++ b/R/esp_get_comarca.R @@ -1,10 +1,10 @@ #' Get 'comarcas' of Spain as [`sf`][sf::st_sf] `POLYGON` #' #' @description -#' Returns 'comarcas' of Spain as `sf` `POLYGON` objects, as provided by the -#' **INE** (Instituto Nacional de Estadistica). +#' Returns 'comarcas' of Spain as `sf` `POLYGON` objects. #' -#' @source INE: PC_Axis files. +#' @source +#' INE: PC_Axis files, IGN, Ministry of Agriculture, Fisheries and Food (MAPA). #' #' @return A [`sf`][sf::st_sf] polygon object. #' @@ -16,6 +16,9 @@ #' @param comarca A name or [`regex`][base::grep()] expression with the names of #' the required comarcas. `NULL` would return all the possible comarcas. #' +#' @param type One of `"INE"`, `"IGN"`, `"AGR"`, `"LIV"`. Type of comarca to +#' return, see **Details**. +#' #' @inheritParams esp_get_munic #' #' @inheritSection esp_get_nuts About caching @@ -26,11 +29,33 @@ #' #' @details #' +#' ## About comarcas #' 'Comarcas' (English equivalent: district, county, area or zone) does not #' always have a formal legal status. They correspond mainly to natural areas #' (valleys, river basins etc.) or even to historical regions or ancient #' kingdoms. #' +#' In the case of Spain, comarcas only have an administrative character legally +#' recognized in Catalonia, the Basque Country, Navarra (named merindades +#' instead), in the region of El Bierzo (Castilla y Leon) and Aragon. Galicia, +#' the Principality of Asturias, and Andalusia have functional comarcas. +#' +#' ## Types +#' +#' `esp_get_comarcas()` can retrieve several types of comarcas, each one +#' provided under different classification criteria. +#' +#' - `"INE"`: Comarcas as defined by the National Statistics Institute (INE). +#' - `"IGN"`: Official comarcas, only available on some Autonomous Communities, +#' provided by the National Geographic Institute. +#' - `"AGR"`: Agrarian comarcas defined by the Ministry of Agriculture, +#' Fisheries and Food (MAPA). +#' +#' - `"LIV"`: Livestock comarcas defined by the Ministry of Agriculture, +#' Fisheries and Food (MAPA). +#' +#' ## Misc +#' #' When using `region` you can use and mix names and NUTS codes #' (levels 1, 2 or 3), ISO codes (corresponding to level 2 or 3) or #' "cpro" (see [esp_codelist]). @@ -56,18 +81,26 @@ #' ggplot(comarcas) + #' geom_sf() #' -#' # Comarcas of Castille and Leon +#' # IGN provides recognized comarcas +#' +#' rec <- esp_get_comarca(type = "IGN") +#' +#' ggplot(rec) + +#' geom_sf(aes(fill = t_comarca)) #' -#' comarcas_cyl <- esp_get_comarca("Castilla y Leon") +#' # Legal Comarcas of Catalunya #' -#' ggplot(comarcas_cyl) + +#' comarcas_cat <- esp_get_comarca("Catalunya", type = "IGN") +#' +#' ggplot(comarcas_cat) + #' geom_sf(aes(fill = ine.prov.name)) + #' labs(fill = "Province") #' -#' # Comarcas with Mountains or Alt(o,a) in the name +#' # Livestock Comarcas with Mountains or Alt(o,a) in the name #' #' comarcas_alto <- esp_get_comarca( #' comarca = "Montaña|Monte|Sierra|Alt", +#' type = "LIV", #' epsg = 3857 #' ) #' @@ -78,18 +111,31 @@ #' } #' esp_get_comarca <- function(region = NULL, comarca = NULL, moveCAN = TRUE, + type = c("INE", "IGN", "AGR", "LIV"), epsg = "4258", update_cache = FALSE, cache_dir = NULL, verbose = FALSE) { init_epsg <- as.character(epsg) - + type <- match.arg(type) if (!init_epsg %in% c("4326", "4258", "3035", "3857")) { stop("epsg value not valid. It should be one of 4326, 4258, 3035 or 3857") } - # Url - api_entry <- "https://github.com/rOpenSpain/mapSpain/raw/sianedata/INE/" - filename <- "esp_com_99.gpkg" + api_entry <- switch(type, + "INE" = "https://github.com/rOpenSpain/mapSpain/raw/sianedata/INE/", + "IGN" = "https://github.com/rOpenSpain/mapSpain/raw/sianedata/IGNComarcas/", + "AGR" = "https://github.com/rOpenSpain/mapSpain/raw/sianedata/MITECO/dist/", + "LIV" = "https://github.com/rOpenSpain/mapSpain/raw/sianedata/MITECO/dist/", + ) + + + filename <- switch(type, + "INE" = "esp_com_99.gpkg", + "IGN" = "comarcas_ign.gpkg", + "AGR" = "comarcas_agrarias.gpkg", + "LIV" = "comarcas_ganaderas.gpkg" + ) + data_sf <- esp_hlp_dwnload_sianedata( @@ -101,7 +147,6 @@ esp_get_comarca <- function(region = NULL, comarca = NULL, moveCAN = TRUE, cache = TRUE ) - if (!is.null(comarca)) { comarca <- paste(comarca, collapse = "|") data_sf <- data_sf[grep(comarca, data_sf$name, ignore.case = TRUE), ] @@ -151,7 +196,6 @@ esp_get_comarca <- function(region = NULL, comarca = NULL, moveCAN = TRUE, } data_sf <- sf::st_transform(data_sf, as.double(init_epsg)) - data_sf <- data_sf[order(data_sf$codauto, data_sf$cpro, data_sf$COMAGR), ] return(data_sf) } diff --git a/R/utils_siane.R b/R/utils_siane.R index 96c0a77f..a90bdac7 100644 --- a/R/utils_siane.R +++ b/R/utils_siane.R @@ -290,6 +290,10 @@ esp_hlp_get_siane <- function(type, } # Filter + if (type %in% c("ffccline", "ffccpoint")) { + return(data_sf) + } + df <- data_sf # By date df$fecha_bajamod <- as.character(df$fecha_baja) diff --git a/codemeta.json b/codemeta.json index 8c99adce..d36a5439 100644 --- a/codemeta.json +++ b/codemeta.json @@ -245,7 +245,7 @@ "applicationCategory": "cartography", "isPartOf": "https://ropenspain.es/", "keywords": ["ropenspain", "tiles", "r", "maps", "spatial", "rstats", "r-package", "municipalities", "spain", "gisco", "provinces", "ign", "administrative-boundaries", "ccaa", "static-tiles", "cran", "ggplot2", "gis"], - "fileSize": "2436.658KB", + "fileSize": "2841.115KB", "citation": [ { "@type": "SoftwareSourceCode", diff --git a/img/README-tile-1.png b/img/README-tile-1.png index a4cc124d..61f5df7b 100644 Binary files a/img/README-tile-1.png and b/img/README-tile-1.png differ diff --git a/inst/WORDLIST b/inst/WORDLIST index 7119d976..15b4190a 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,16 +1,17 @@ ANE Alboran -Andalucia BDN Baleares Banco Bathymetry +Bierzo CCAA CDN CLDR CRS Canarias CartoBase +Castilla Centre Ceuta Comarcas @@ -19,10 +20,12 @@ EEA EPSG ESDAC ETRS +El Estadistica Eurostat FID GISCO +Galicia Galician Geografico Hexbin @@ -34,6 +37,7 @@ Instituto LAEA LAU Liedekerke +MAPA MITECO MTN Mapa @@ -41,6 +45,7 @@ Melilla Montanarella Nacional Naturaleza +Navarra OGC ORCID OpenStreetMap @@ -63,7 +68,7 @@ ceuta choropleth choropleths cmun -codauto +comarca comarcas config cpro @@ -82,6 +87,7 @@ javascript jrc kms melilla +merindades pre providersESP spanish diff --git a/man/esp_get_comarca.Rd b/man/esp_get_comarca.Rd index 843de937..7d638ac8 100644 --- a/man/esp_get_comarca.Rd +++ b/man/esp_get_comarca.Rd @@ -4,13 +4,14 @@ \alias{esp_get_comarca} \title{Get 'comarcas' of Spain as \code{\link[sf:sf]{sf}} \code{POLYGON}} \source{ -INE: PC_Axis files. +INE: PC_Axis files, IGN, Ministry of Agriculture, Fisheries and Food (MAPA). } \usage{ esp_get_comarca( region = NULL, comarca = NULL, moveCAN = TRUE, + type = c("INE", "IGN", "AGR", "LIV"), epsg = "4258", update_cache = FALSE, cache_dir = NULL, @@ -29,6 +30,9 @@ the required comarcas. \code{NULL} would return all the possible comarcas.} Initial position can be adjusted using the vector of coordinates. See \strong{Displacing the Canary Islands}.} +\item{type}{One of \code{"INE"}, \code{"IGN"}, \code{"AGR"}, \code{"LIV"}. Type of comarca to +return, see \strong{Details}.} + \item{epsg}{projection of the map: 4-digit \href{https://epsg.io/}{EPSG code}. One of: \itemize{ @@ -50,21 +54,47 @@ default is \code{FALSE}.} A \code{\link[sf:sf]{sf}} polygon object. } \description{ -Returns 'comarcas' of Spain as \code{sf} \code{POLYGON} objects, as provided by the -\strong{INE} (Instituto Nacional de Estadistica). +Returns 'comarcas' of Spain as \code{sf} \code{POLYGON} objects. } \details{ +\subsection{About comarcas}{ + 'Comarcas' (English equivalent: district, county, area or zone) does not always have a formal legal status. They correspond mainly to natural areas (valleys, river basins etc.) or even to historical regions or ancient kingdoms. +In the case of Spain, comarcas only have an administrative character legally +recognized in Catalonia, the Basque Country, Navarra (named merindades +instead), in the region of El Bierzo (Castilla y Leon) and Aragon. Galicia, +the Principality of Asturias, and Andalusia have functional comarcas. +} + +\subsection{Types}{ + +\code{esp_get_comarcas()} can retrieve several types of comarcas, each one +provided under different classification criteria. +\itemize{ +\item \code{"INE"}: Comarcas as defined by the National Statistics Institute (INE). +\item \code{"IGN"}: Official comarcas, only available on some Autonomous Communities, +provided by the National Geographic Institute. +\item \code{"AGR"}: Agrarian comarcas defined by the Ministry of Agriculture, +Fisheries and Food (MAPA). +\item \code{"LIV"}: Livestock comarcas defined by the Ministry of Agriculture, +Fisheries and Food (MAPA). +} +} + +\subsection{Misc}{ + When using \code{region} you can use and mix names and NUTS codes (levels 1, 2 or 3), ISO codes (corresponding to level 2 or 3) or "cpro" (see \link{esp_codelist}). When calling a higher level (Province, Autonomous Community or NUTS1), all the comarcas of that level would be added. +} + \subsection{Legal Notice}{ The use of the information contained on the @@ -104,18 +134,26 @@ library(ggplot2) ggplot(comarcas) + geom_sf() -# Comarcas of Castille and Leon +# IGN provides recognized comarcas + +rec <- esp_get_comarca(type = "IGN") + +ggplot(rec) + + geom_sf(aes(fill = t_comarca)) + +# Legal Comarcas of Catalunya -comarcas_cyl <- esp_get_comarca("Castilla y Leon") +comarcas_cat <- esp_get_comarca("Catalunya", type = "IGN") -ggplot(comarcas_cyl) + +ggplot(comarcas_cat) + geom_sf(aes(fill = ine.prov.name)) + labs(fill = "Province") -# Comarcas with Mountains or Alt(o,a) in the name +# Livestock Comarcas with Mountains or Alt(o,a) in the name comarcas_alto <- esp_get_comarca( comarca = "Montaña|Monte|Sierra|Alt", + type = "LIV", epsg = 3857 ) diff --git a/tests/testthat/_snaps/esp_getTiles/frombbox.png b/tests/testthat/_snaps/esp_getTiles/frombbox.png index 5baf8f3a..e7241726 100644 Binary files a/tests/testthat/_snaps/esp_getTiles/frombbox.png and b/tests/testthat/_snaps/esp_getTiles/frombbox.png differ diff --git a/tests/testthat/_snaps/esp_getTiles/point.png b/tests/testthat/_snaps/esp_getTiles/point.png index 37031fe0..0c8e4b50 100644 Binary files a/tests/testthat/_snaps/esp_getTiles/point.png and b/tests/testthat/_snaps/esp_getTiles/point.png differ diff --git a/tests/testthat/_snaps/esp_getTiles/sfc.png b/tests/testthat/_snaps/esp_getTiles/sfc.png index 5baf8f3a..e7241726 100644 Binary files a/tests/testthat/_snaps/esp_getTiles/sfc.png and b/tests/testthat/_snaps/esp_getTiles/sfc.png differ diff --git a/tests/testthat/_snaps/esp_getTiles/silent.png b/tests/testthat/_snaps/esp_getTiles/silent.png index 532faee2..2558a025 100644 Binary files a/tests/testthat/_snaps/esp_getTiles/silent.png and b/tests/testthat/_snaps/esp_getTiles/silent.png differ diff --git a/tests/testthat/test-esp_get_comarca.R b/tests/testthat/test-esp_get_comarca.R index 95ea0f1b..3edbf452 100644 --- a/tests/testthat/test-esp_get_comarca.R +++ b/tests/testthat/test-esp_get_comarca.R @@ -24,4 +24,12 @@ test_that("comarcas online", { s <- esp_get_comarca(region = n) expect_equal(length(unique(s$cpro)), 52) + + # Types + ine <- esp_get_comarca() + ign <- esp_get_comarca(type = "IGN") + agr <- esp_get_comarca(type = "AGR") + liv <- esp_get_comarca(type = "LIV") + + expect_length(unique(c(nrow(ine), nrow(ign), nrow(agr), nrow(liv))), 4) }) diff --git a/tests/testthat/test-esp_get_munic.R b/tests/testthat/test-esp_get_munic.R index 34e2e9bd..d5318b6d 100644 --- a/tests/testthat/test-esp_get_munic.R +++ b/tests/testthat/test-esp_get_munic.R @@ -20,7 +20,6 @@ test_that("munic local", { skip_on_cran() skip_if_gisco_offline() - expect_silent(esp_get_munic(year = 2013, region = "Alava")) a2 <- esp_get_munic(year = 2018, region = "Alava") expect_s3_class(a2, "sf") }) diff --git a/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_756.png b/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_756.png index 53ff0ab0..915f6fc1 100644 Binary files a/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_756.png and b/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_756.png differ diff --git a/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_757.png b/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_757.png index 9fc1e056..f953c5d9 100644 Binary files a/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_757.png and b/vignettes/articles/artcache/IDErioja/IDErioja_11_1009_757.png differ diff --git a/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_756.png b/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_756.png index c42501c4..0ff4dd3c 100644 Binary files a/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_756.png and b/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_756.png differ diff --git a/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_757.png b/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_757.png index eaf75357..db87a94d 100644 Binary files a/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_757.png and b/vignettes/articles/artcache/IDErioja/IDErioja_11_1010_757.png differ diff --git a/vignettes/articles/artcache/RedTransporte.Carreteras/59138810664d941888513648f7d6e490.png b/vignettes/articles/artcache/RedTransporte.Carreteras/59138810664d941888513648f7d6e490.png new file mode 100644 index 00000000..00dc1f15 Binary files /dev/null and b/vignettes/articles/artcache/RedTransporte.Carreteras/59138810664d941888513648f7d6e490.png differ diff --git a/vignettes/articles/artcache/UnidadesAdm.Limites/6f319583d5db6da1ac63cd5b4f39125d.png b/vignettes/articles/artcache/UnidadesAdm.Limites/6f319583d5db6da1ac63cd5b4f39125d.png deleted file mode 100644 index bb5f1dd8..00000000 Binary files a/vignettes/articles/artcache/UnidadesAdm.Limites/6f319583d5db6da1ac63cd5b4f39125d.png and /dev/null differ diff --git a/vignettes/articles/working_imagery.Rmd b/vignettes/articles/working_imagery.Rmd index 3d3362a8..f6a7923c 100644 --- a/vignettes/articles/working_imagery.Rmd +++ b/vignettes/articles/working_imagery.Rmd @@ -47,7 +47,7 @@ here on layer provided by La Rioja's [Infraestructura de Datos Espaciales **When working with imagery, it is important to set `moveCAN = FALSE`, otherwise the images for the Canary Islands won't be accurate.** -```{r static1} +```{r static1, fig.alt="Map of the limits of city of Logroño using a tile as a basemap"} library(mapSpain) library(sf) library(ggplot2) @@ -72,13 +72,18 @@ ggplot(lgn_borders) + Some tiles could be loaded with or without an alpha value, that controls the transparency of the object: -```{r static2} +```{r static2, fig.alt="Map of the roads of Autonomous Communities surrouding Madrid"} madrid <- esp_get_ccaa("Madrid", epsg = 3857) # Example without transparency -basemap <- esp_getTiles(madrid, "IDErioja.Claro", zoommin = 1) -tile_opaque <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = FALSE) +basemap <- esp_getTiles(madrid, "IDErioja.Claro", + zoommin = 1, + crop = TRUE, bbox_expand = 0 +) +tile_opaque <- esp_getTiles(madrid, "RedTransporte.Carreteras", + transparent = FALSE, crop = TRUE, bbox_expand = 0 +) ggplot() + geom_spatraster_rgb(data = basemap) + @@ -88,10 +93,12 @@ ggplot() + Now let's check the same code using the `tranparent = TRUE` option: -```{r static_transp} +```{r static_transp, fig.alt="Example on how to use alpha value for combining different types of basemaps."} # Example with transparency -tile_alpha <- esp_getTiles(madrid, "UnidadesAdm.Limites", transparent = TRUE) +tile_alpha <- esp_getTiles(madrid, "RedTransporte.Carreteras", + transparent = TRUE, crop = TRUE, bbox_expand = 0 +) # Same code than above for plotting ggplot() + @@ -107,7 +114,7 @@ Now the two tiles overlaps with the desired alpha value. Another nice feature is the ability of masking the tiles, so more advanced maps can be plotted: -```{r static3} +```{r static3, fig.alt="Example of combining types of tiles by masking to a shapefile."} rioja <- esp_get_prov("La Rioja", epsg = 3857) basemap <- esp_getTiles(rioja, "PNOA", bbox_expand = 0.1, zoommin = 1) diff --git a/vignettes/articles/x02_mapasesp.Rmd b/vignettes/articles/x02_mapasesp.Rmd index 1526abeb..03449cc6 100644 --- a/vignettes/articles/x02_mapasesp.Rmd +++ b/vignettes/articles/x02_mapasesp.Rmd @@ -91,7 +91,7 @@ remotes::install_github("rOpenSpain/mapSpain", dependencies = TRUE) ### Un ejemplo rápido -```{r intro} +```{r intro, fig.alt="Mapa de los municipios de Galicia"} library(mapSpain) library(tidyverse) @@ -123,7 +123,7 @@ reactable(galicia, Comparamos ahora **mapSpain** con otro paquete que proporciona objetos `sf` o `SpatVector` de distintos paises: -```{r compara} +```{r compara, fig.alt="Comparación de la resolución de diferentes fuentes de mapas; se muestran bordes de los datos comparados con ortofoto de la Ria de Ferrol"} library(sf) # manipulación de datos espaciales # rnaturalearth @@ -493,6 +493,16 @@ Estas capas se pueden usar también como fondo en mapas estáticos stations <- esp_get_railway(spatialtype = "point", epsg = 4326) library(leaflet) +# Creamos icono + +iconurl <- paste0( + "https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/", + "Map_icons_by_Scott_de_Jonge_-_train-station.svg/480px-Map_", + "icons_by_Scott_de_Jonge_-_train-station.svg.png" +) + +train_icon <- makeIcon(iconurl, iconurl, 18, 18) + leaflet(stations, elementId = "railway", @@ -501,10 +511,14 @@ leaflet(stations, ) %>% addProviderEspTiles("IDErioja.Claro", group = "Base") %>% addProviderEspTiles("MTN", group = "MTN") %>% + addProviderEspTiles("RedTransporte.Carreteras", + group = "Carreteras" + ) %>% addProviderEspTiles("RedTransporte.Ferroviario", group = "Lineas Ferroviarias" ) %>% addMarkers( + icon = train_icon, group = "Estaciones", popup = sprintf( "%s", @@ -514,9 +528,10 @@ leaflet(stations, ) %>% addLayersControl( baseGroups = c("Base", "MTN"), - overlayGroups = c("Lineas Ferroviarias", "Estaciones"), + overlayGroups = c("Estaciones", "Lineas Ferroviarias", "Carreteras"), options = layersControlOptions(collapsed = FALSE) - ) + ) %>% + hideGroup(c("Lineas Ferroviarias", "Carreteras")) ``` ## Otros recursos diff --git a/vignettes/basic2-1.png b/vignettes/basic2-1.png index c5dbdc41..430fd8f4 100644 Binary files a/vignettes/basic2-1.png and b/vignettes/basic2-1.png differ diff --git a/vignettes/basic3-1.png b/vignettes/basic3-1.png index e88a0d2e..96ab8d07 100644 Binary files a/vignettes/basic3-1.png and b/vignettes/basic3-1.png differ diff --git a/vignettes/choro-1.png b/vignettes/choro-1.png index fa115fa4..d32f4f9e 100644 Binary files a/vignettes/choro-1.png and b/vignettes/choro-1.png differ diff --git a/vignettes/giscoR-1.png b/vignettes/giscoR-1.png index 87b49f67..2465f0c6 100644 Binary files a/vignettes/giscoR-1.png and b/vignettes/giscoR-1.png differ diff --git a/vignettes/mapSpain.Rmd b/vignettes/mapSpain.Rmd index ff718d10..872c2e54 100644 --- a/vignettes/mapSpain.Rmd +++ b/vignettes/mapSpain.Rmd @@ -79,7 +79,7 @@ ggplot(country) + ```