Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more sources for comarcas #117

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
70 changes: 57 additions & 13 deletions R/esp_get_comarca.R
Original file line number Diff line number Diff line change
@@ -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.
#'
Expand All @@ -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
Expand All @@ -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]).
Expand All @@ -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
#' )
#'
Expand All @@ -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(
Expand All @@ -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), ]
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions R/utils_siane.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file modified img/README-tile-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
ANE
Alboran
Andalucia
BDN
Baleares
Banco
Bathymetry
Bierzo
CCAA
CDN
CLDR
CRS
Canarias
CartoBase
Castilla
Centre
Ceuta
Comarcas
Expand All @@ -19,10 +20,12 @@ EEA
EPSG
ESDAC
ETRS
El
Estadistica
Eurostat
FID
GISCO
Galicia
Galician
Geografico
Hexbin
Expand All @@ -34,13 +37,15 @@ Instituto
LAEA
LAU
Liedekerke
MAPA
MITECO
MTN
Mapa
Melilla
Montanarella
Nacional
Naturaleza
Navarra
OGC
ORCID
OpenStreetMap
Expand All @@ -63,7 +68,7 @@ ceuta
choropleth
choropleths
cmun
codauto
comarca
comarcas
config
cpro
Expand All @@ -82,6 +87,7 @@ javascript
jrc
kms
melilla
merindades
pre
providersESP
spanish
52 changes: 45 additions & 7 deletions man/esp_get_comarca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/_snaps/esp_getTiles/frombbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/esp_getTiles/point.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/esp_getTiles/sfc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/_snaps/esp_getTiles/silent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/testthat/test-esp_get_comarca.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
1 change: 0 additions & 1 deletion tests/testthat/test-esp_get_munic.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
Binary file modified vignettes/articles/artcache/IDErioja/IDErioja_11_1009_756.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/articles/artcache/IDErioja/IDErioja_11_1009_757.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/articles/artcache/IDErioja/IDErioja_11_1010_756.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/articles/artcache/IDErioja/IDErioja_11_1010_757.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Loading