Skip to content

Commit

Permalink
Add simplified parameter for geospatial
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-otero committed Aug 1, 2024
1 parent 3f90e93 commit 815a134
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
17 changes: 13 additions & 4 deletions R/download_geospatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#' \item \code{"ZU" } or \code{"urban_zone"}: Urban Zone.
#' \item \code{"MZN"} or \code{"block"}: Block.
#' }
#' @param simplified logical for indicating if the downloaded spatial data
#' should be a simplified version of the geometries. Simplified versions are
#' lighter but less precise, and are recommended for easier applications like
#' plots. Default is \code{TRUE}.
#' @param include_geom logical for including (or not) the spatial geometry.
#' Default is \code{TRUE}. If \code{TRUE}, the function will return an
#' \code{"sf"} \code{data.frame}.
Expand All @@ -27,27 +31,32 @@
#'
#' @examples
#' \dontrun{
#' departments <- download_geospatial("department", TRUE, FALSE)
#' departments <- download_geospatial("department")
#' head(departments)
#' }
#'
#' @return \code{data.frame} object with downloaded data.
#'
#' @export
download_geospatial <- function(spatial_level, include_geom = TRUE,
include_cnpv = TRUE) {
download_geospatial <- function(spatial_level, simplified = TRUE,
include_geom = TRUE, include_cnpv = TRUE) {
checkmate::assert_logical(simplified)
checkmate::assert_logical(include_geom)
checkmate::assert_logical(include_cnpv)
stopifnot(
"At least one of the groups (`geom` and/or `cnpv`)
must be TRUE" = any(include_geom, include_cnpv)
)

dataset <- retrieve_geospatial_name(spatial_level)
dataset_path <- retrieve_path(dataset)
if(simplified){
dataset_path <- sub("\\.gpkg$", "_SIM.gpkg", dataset_path)
}
geospatial_data <- sf::st_read(dataset_path, quiet = TRUE)
geospatial_vars <- c("area", "latitud", "longitud")
shape_vars <- c("shape_length", "shape_area")

if (include_geom && !include_cnpv) {
last_base_index <- which(colnames(geospatial_data) == "longitud")
geospatial_data <- geospatial_data %>%
Expand Down
16 changes: 11 additions & 5 deletions R/merge_geo_demographic.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#' @param demographic_dataset character with the demographic dataset name.
#' Please use \code{list_datasets(""EN"M demographic")} to check available
#' datasets.
#' @param simplified logical for indicating if the downloaded spatial data
#' should be a simplified version of the geometries. Simplified versions are
#' lighter but less precise, and are recommended for easier applications like
#' plots. Default is \code{TRUE}.
#'
#' @examples
#' \dontrun{
Expand All @@ -19,7 +23,7 @@
#'
#' @return \code{data.frame} object with the merged data.
#' @export
merge_geo_demographic <- function(demographic_dataset) {
merge_geo_demographic <- function(demographic_dataset, simplified = TRUE) {
checkmate::assert_character(demographic_dataset)

datasets <- list_datasets("demographic", "EN")
Expand Down Expand Up @@ -51,8 +55,9 @@ merge_geo_demographic <- function(demographic_dataset) {
names_from = dplyr::all_of(column),
values_from = dplyr::all_of(total_col)
)
geospatial <- suppressMessages(download_geospatial("department",
include_cnpv = FALSE
geospatial <- suppressMessages(download_geospatial("department",
simplified = simplified,
include_cnpv = FALSE
))
merged_data <- merge(geospatial, filtered_df,
by.x = "codigo_departamento",
Expand All @@ -67,8 +72,9 @@ merge_geo_demographic <- function(demographic_dataset) {
names_from = dplyr::all_of(column),
values_from = dplyr::all_of(total_col)
)
geospatial <- suppressMessages(download_geospatial("municipality",
include_cnpv = FALSE
geospatial <- suppressMessages(download_geospatial("municipality",
simplified = simplified,
include_cnpv = FALSE
))
merged_data <- merge(geospatial, filtered_df,
by.x = "codigo_municipio",
Expand Down
4 changes: 0 additions & 4 deletions R/retrieve.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ retrieve_climate_path <- function() {
#' Dictionaries are not included in the general documentation file. Therefore,
#' the path is built internally.
#'
<<<<<<< Updated upstream
#' @param dict_name character with the dictionary name.
=======
#' @param dict_name character with the dictionary file name.
>>>>>>> Stashed changes
#'
#' @return character with path to retrieve the dataset.
#'
Expand Down

0 comments on commit 815a134

Please sign in to comment.