Skip to content

Commit 614d06f

Browse files
committed
Add BDN grids (#61)
1 parent 513f6b7 commit 614d06f

11 files changed

+409
-7
lines changed

CITATION.cff

-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ keywords:
6464
- administrative-boundaries
6565
- ccaa
6666
- static-tiles
67-
- cran
68-
- ggplot2
69-
- ropenspain
70-
- spain
7167
references:
7268
- type: software
7369
title: 'R: A Language and Environment for Statistical Computing'

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export(esp_get_capimun)
1212
export(esp_get_ccaa)
1313
export(esp_get_ccaa_siane)
1414
export(esp_get_country)
15+
export(esp_get_grid_BDN)
16+
export(esp_get_grid_BDN_ccaa)
1517
export(esp_get_grid_MTN)
1618
export(esp_get_grid_ccaa)
1719
export(esp_get_grid_prov)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Rebuild coding database to avoid errors due to encoding.
44
- New grid functions (#61):
55
- `esp_get_grid_MTN()`
6+
- `esp_get_grid_BDN()`
67

78

89

R/esp_get_grid_BDN.R

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#' Get `sf` polygons of the national geographic grids provided by BDN
2+
#'
3+
#' @description
4+
#' Loads a `sf` polygon with the geographic grids of Spain as provided on
5+
#' the Banco de Datos de la Naturaleza (Nature Data Bank), by the
6+
#' Ministry of Environment (MITECO):
7+
#' * [esp_get_grid_BDN()] extracts country-wide grids with resolutions
8+
#' 5x5 or 10x10 kms.
9+
#' * [esp_get_grid_BDN_ccaa()] extracts grids by Autonomous Community with
10+
#' resolution 1x1 km.
11+
#'
12+
#' @family grids
13+
#'
14+
#' @return A `sf` polygon
15+
#'
16+
#'
17+
#' @source BDN data via a custom CDN (see
18+
#' <https://github.com/rOpenSpain/mapSpain/tree/sianedata/MTN>).
19+
#'
20+
#' See original metadata and source on
21+
#' <https://www.miteco.gob.es/es/biodiversidad/servicios/banco-datos-naturaleza/informacion-disponible/bdn-cart-aux-descargas-ccaa.aspx>
22+
#'
23+
#' @export
24+
#'
25+
#' @param resolution Resolution of the grid in kms. Could be `5` or `10`.
26+
#' @param type The scope of the grid. It could be mainland Spain (`"main"`) or
27+
#' the Canary Islands (`"canary"`).
28+
#'
29+
#' @inheritParams esp_get_nuts
30+
#'
31+
#' @inheritSection esp_get_nuts About caching
32+
#' @examplesIf esp_check_access()
33+
#' \donttest{
34+
#' grid <- esp_get_grid_BDN(resolution = "10", type = "main")
35+
#'
36+
#' library(ggplot2)
37+
#'
38+
#' ggplot(grid) +
39+
#' geom_sf() +
40+
#' theme_light() +
41+
#' labs(title = "BDN Grid for Spain")
42+
#' }
43+
esp_get_grid_BDN <- function(resolution = 10,
44+
type = "main",
45+
update_cache = FALSE,
46+
cache_dir = NULL,
47+
verbose = FALSE) {
48+
49+
# Check grid
50+
res <- as.numeric(resolution)
51+
52+
if (!res %in% c(5, 10)) {
53+
stop(
54+
"resolution should be one of 5, 10"
55+
)
56+
}
57+
58+
if (!type %in% c("main", "canary")) {
59+
stop(
60+
"type should be one of 'main', 'canary'"
61+
)
62+
}
63+
64+
65+
66+
67+
# Url
68+
api_entry <- "https://github.com/rOpenSpain/mapSpain/raw/sianedata/MITECO/dist/"
69+
70+
# Filename
71+
if (res == 10) {
72+
filename <- switch(type,
73+
"main" = "Malla10x10_Ter_p.gpkg",
74+
"Malla10x10_Ter_c.gpkg"
75+
)
76+
} else {
77+
filename <- switch(type,
78+
"main" = "Malla_5x5_tierra_p.gpkg",
79+
"Malla_5x5_tierra_c.gpkg"
80+
)
81+
}
82+
result <- esp_hlp_dwnload_sianedata(
83+
api_entry = api_entry,
84+
filename = filename,
85+
cache_dir = cache_dir,
86+
verbose = verbose,
87+
update_cache = update_cache,
88+
cache = TRUE
89+
)
90+
91+
return(result)
92+
}
93+
94+
#' @rdname esp_get_grid_BDN
95+
#' @export
96+
#' @param ccaa A vector of names and/or codes for autonomous communities.
97+
#' See **Details** on [esp_get_ccaa()].
98+
#' @seealso [esp_get_ccaa()]
99+
100+
esp_get_grid_BDN_ccaa <- function(ccaa,
101+
update_cache = FALSE,
102+
cache_dir = NULL,
103+
verbose = FALSE) {
104+
105+
106+
# Get region id
107+
108+
ccaa <- ccaa[!is.na(ccaa)]
109+
110+
111+
region <- ccaa
112+
if (is.null(region)) {
113+
stop("ccaa can't be null")
114+
} else {
115+
nuts_id <- esp_hlp_all2ccaa(region)
116+
117+
nuts_id <- unique(nuts_id)
118+
}
119+
120+
# Check if it is a valid NUTS, if not throws an error
121+
122+
data <- esp_codelist
123+
124+
if (!nuts_id %in% data$nuts2.code) stop(ccaa, " is not a CCAA")
125+
126+
127+
# Switch name. The ids are the same than the NUTS code removing the "ES" part
128+
id <- gsub("ES", "", nuts_id)
129+
130+
131+
api_entry <- "https://github.com/rOpenSpain/mapSpain/raw/sianedata/MITECO/dist/"
132+
filename <- paste0("malla1x1_", id, ".gpkg")
133+
134+
result <- esp_hlp_dwnload_sianedata(
135+
api_entry = api_entry,
136+
filename = filename,
137+
cache_dir = cache_dir,
138+
verbose = verbose,
139+
update_cache = update_cache,
140+
cache = TRUE
141+
)
142+
143+
144+
return(result)
145+
}

R/utils_download_sianedata.R

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
esp_hlp_dwnload_sianedata <- function(api_entry, filename, cache_dir,
2+
verbose, update_cache, cache) {
3+
url <- file.path(api_entry, filename)
4+
5+
cache_dir <- esp_hlp_cachedir(cache_dir)
6+
7+
if (verbose) message("Cache dir is ", cache_dir)
8+
9+
# Create filepath
10+
filepath <- file.path(cache_dir, filename)
11+
localfile <- file.exists(filepath)
12+
13+
if (isFALSE(cache)) {
14+
dwnload <- FALSE
15+
filepath <- url
16+
if (verbose) {
17+
message("Try loading from ", filepath)
18+
}
19+
} else if (update_cache | isFALSE(localfile)) {
20+
dwnload <- TRUE
21+
if (verbose) {
22+
message(
23+
"Downloading file from ",
24+
url,
25+
"\n\nSee https://github.com/rOpenSpain/mapSpain/tree/sianedata/ ",
26+
"for more info"
27+
)
28+
}
29+
if (verbose & update_cache) {
30+
message("\nUpdating cache")
31+
}
32+
} else if (localfile) {
33+
dwnload <- FALSE
34+
if (verbose & isFALSE(update_cache)) {
35+
message("File already available on ", filepath)
36+
}
37+
}
38+
39+
# Downloading
40+
if (dwnload) {
41+
err_dwnload <- try(download.file(url, filepath,
42+
quiet = isFALSE(verbose),
43+
mode = "wb"
44+
), silent = TRUE)
45+
46+
if (inherits(err_dwnload, "try-error")) {
47+
if (verbose) message("Retrying query")
48+
err_dwnload <- try(download.file(url, filepath,
49+
quiet = isFALSE(verbose),
50+
mode = "wb"
51+
), silent = TRUE)
52+
}
53+
54+
# If not then message
55+
if (inherits(err_dwnload, "try-error")) {
56+
message(
57+
"Download failed",
58+
"\n\nurl \n ",
59+
url,
60+
" not reachable.\n\nPlease try with another options. ",
61+
"If you think this ",
62+
"is a bug please consider opening an issue on:",
63+
"\nhttps://github.com/rOpenSpain/mapSpain/issues"
64+
)
65+
stop("\nExecution halted")
66+
} else if (verbose) {
67+
message("Download succesful")
68+
}
69+
}
70+
71+
72+
if (verbose & isTRUE(cache)) {
73+
message("Reading from local file ", filepath)
74+
size <- file.size(filepath)
75+
class(size) <- "object_size"
76+
message(format(size, units = "auto"))
77+
}
78+
79+
# Load
80+
81+
err_onload <- try(
82+
data_sf <- sf::st_read(
83+
filepath,
84+
quiet = isFALSE(verbose),
85+
stringsAsFactors = FALSE
86+
),
87+
silent = TRUE
88+
)
89+
90+
if (inherits(err_onload, "try-error")) {
91+
message(
92+
"File may be corrupt. Please try again using cache = TRUE ",
93+
"and update_cache = TRUE"
94+
)
95+
stop("\nExecution halted")
96+
}
97+
98+
if (verbose) message("File loaded")
99+
return(err_onload)
100+
}

codemeta.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@
245245
},
246246
"applicationCategory": "cartography",
247247
"isPartOf": "https://ropenspain.es/",
248-
"keywords": ["rOpenSpain", "tiles", "r", "maps", "spatial", "rstats", "r-package", "municipalities", "Spain", "gisco", "provinces", "ign", "administrative-boundaries", "ccaa", "static-tiles", "spain", "cran", "ropenspain", "ggplot2"],
249-
"fileSize": "2542.155KB",
248+
"keywords": ["rOpenSpain", "tiles", "r", "maps", "spatial", "rstats", "r-package", "municipalities", "Spain", "gisco", "provinces", "ign", "administrative-boundaries", "ccaa", "static-tiles"],
249+
"fileSize": "2545.247KB",
250250
"citation": [
251251
{
252252
"@type": "SoftwareSourceCode",
@@ -266,7 +266,6 @@
266266
}
267267
],
268268
"releaseNotes": "https://github.com/rOpenSpain/mapSpain/blob/master/NEWS.md",
269-
"readme": "https://github.com/rOpenSpain/mapSpain/blob/main/README.md",
270269
"contIntegration": ["https://github.com/rOpenSpain/mapSpain/actions?query=workflow%3AR-CMD-check", "https://app.codecov.io/gh/rOpenSpain/mapSpain"],
271270
"developmentStatus": "https://www.repostatus.org/#active"
272271
}

data/esp_codelist.rda

-547 Bytes
Binary file not shown.

data/leaflet.providersESP.df.rda

-577 Bytes
Binary file not shown.

man/esp_get_grid_BDN.Rd

+92
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/esp_get_grid_MTN.Rd

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)