Skip to content

Commit

Permalink
Make boundaries_select more user friendly
Browse files Browse the repository at this point in the history
by showing the actual boundary name instead of their short name.
  • Loading branch information
ccamara committed Jul 25, 2024
1 parent e822a2e commit bd7bccc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 27 deletions.
8 changes: 4 additions & 4 deletions R/boundaries_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Queries [ONS' Geoportal](https://geoportal.statistics.gov.uk/) endpoints and
#' retrieves the requested geographical boundaries in the form of a sf object.
#'
#' @param boundary a string containing the name of the boundary to be
#' downloaded. Accepted values are: `r levels(ons_boundaries$boundary)`
#' @param boundary a string containing the acronym of name of the boundary to be
#' downloaded. Accepted values are: `r levels(ons_boundaries$boundary_short)`
#' @param year a number containing the year when the boundary was created.
#' @param detail_level a string defining the level of detail in the geometry.
#' (this affects the download size).
Expand Down Expand Up @@ -46,11 +46,11 @@ boundaries_get <- function(boundary, year = NULL, detail_level = "BUC") {
class = "error_not_single_string"
)
}
if (!boundary %in% levels(ons_boundaries$boundary)) {
if (!boundary %in% levels(ons_boundaries$boundary_short)) {
cli::cli_abort(
paste(
"`boundary` must be one of these values:",
levels(ons_boundaries$boundary)
levels(ons_boundaries$boundary_short)
),
class = "error_boundary_not_valid"
)
Expand Down
38 changes: 29 additions & 9 deletions R/boundaries_select.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#' Interactively Retrieve Geographical Boundaries
#'
#' Constructs a valid combination of geometry type, year and detail level and
#' downloads the selected boundary.
#' A Command Line Interface that constructs a valid combination of geometry
#' type, year and detail level and downloads the selected boundary by calling
#' `boundaries_get()`.
#'
#' @return a `sf` object with the selected boundaries.
#' @return a `sf` object with the selected combination of boundary, year and
#' detail level.
#'
#'
#' @export
Expand Down Expand Up @@ -36,6 +38,12 @@ boundaries_select <- function() {

cat("You chose:", choice_boundary, "\n")

boundaries_lookup <- unique(ons_boundaries[c("boundary", "boundary_short")])

choice_boundary_short <- as.character(
boundaries_lookup$boundary_short[boundaries_lookup$boundary == choice_boundary])


# Year --------------------------------------------------------------------

years <- levels(as.factor(
Expand All @@ -56,16 +64,28 @@ boundaries_select <- function() {
ons_boundaries[ons_boundaries$boundary == choice_boundary & ons_boundaries$year == choice_year, "detail_level"]
))

choice_detail_levels_n <- utils::menu(
detail_levels,
title = "Choose the detail level you'd like to download"
)
# Some combinations do not have any specified detail level
if (nlevels(detail_levels) > 1) {

choice_detail_levels_n <- utils::menu(
detail_levels,
title = "Choose the detail level you'd like to download"
)

choice_detail_levels <- detail_levels[choice_detail_levels_n]

} else {

cli::cli_alert_info("This combination only has a single detail level
available. Assigning it automatically")
choice_detail_levels <- "NA"
}


choice_detail_levels <- detail_levels[choice_detail_levels_n]

# Download ---------------------------------------------------------------
#return(paste(choice_boundary, choice_year, choice_detail_levels, sep = "_"))
return(boundaries_get(choice_boundary, choice_year, choice_detail_levels))
return(boundaries_get(choice_boundary_short, choice_year, choice_detail_levels))

# Check if the user made a valid choice
# if (choice_boundary_type> 0) {
Expand Down
7 changes: 4 additions & 3 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
#' \describe{
#' \item{id}{A unique identifier for every boundary}
#' \item{service}{URL pointing to the API service}
#' \item{boundary_type}{Type of boundary, according to ONS' classification}
#' \item{boundary}{Boundary (short) name, according to ONS' naming}
#' \item{detail_level}{Boundary's level of detail. For a detailed description of the methodology refer to [Digital boundaries](https://www.ons.gov.uk/methodology/geography/geographicalproducts/digitalboundaries) }
#' \item{boundary_type}{Type of boundary, according to ONS' classification: `r levels(ons_boundaries$boundary_type)`}
#' \item{boundary}{Boundary name, according to ONS' naming: `r levels(ons_boundaries$boundary)`}
#' \item{boundary_short}{Boundary (short) name, according to ONS' naming: `r levels(ons_boundaries$boundary_short)`}
#' \item{detail_level}{Boundary's level of detail: `r levels(ons_boundaries$detail_level)`. For a detailed description of the methodology refer to [Digital boundaries](https://www.ons.gov.uk/methodology/geography/geographicalproducts/digitalboundaries) }
#' \item{year}{Year in which the boundaries were created}
#' \item{url_download}{URL querying the API service to return all features as a geojson file}
#'
Expand Down
27 changes: 24 additions & 3 deletions data-raw/data-urls.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ ons_data <- as.data.frame(services) |>
# Infer categories from titles
mutate(
boundary = case_when(
str_detect(service, "Combined_Authorities") ~ "Combined Authorities",
str_detect(service, "/Counties_and_Unitary_Authorities") ~ "Counties and Unitary Authorities",
str_detect(service, "/Counties_") ~ "Counties",
str_detect(service, "/Countries_") ~ "Countries",
str_detect(service, "/County_Electoral_Division") ~ "County Electoral Division",
str_detect(service, "/Local_Authority_Districts") ~ "Local Authority Districts",
str_detect(service, "/Local_Planning_Authorities") ~ "Local Planning Authorities",
str_detect(service, "/Metropolitan_Counties") ~ "Metropolitan Counties",
str_detect(service, "/Parishes_and_Non_Civil_Parished_Areas") ~ "Parishes and Non Civil Parished Areas",
str_detect(service, "/Parishes") ~ "Parishes",
str_detect(service, "/Regions") ~ "Regions",
str_detect(service, "/Upper_Tier") ~ "Upper Tier",
str_detect(service, "/Wards") ~ "Wards",
str_detect(service, "/Lower_Layer") ~ "Lower Layer Output Areas",
str_detect(service, "/Middle_Layer") ~ "Middle Layer Output Areas",
str_detect(service, "/Output_Areas") ~ "Output Areas",
),
boundary = as.factor(boundary)
) |>
mutate(
boundary_short = case_when(
str_detect(service, "Combined_Authorities") ~ "CAUTH",
str_detect(service, "/Counties_and_Unitary_Authorities") ~ "CTYUA",
str_detect(service, "/Counties_") ~ "CTY",
Expand All @@ -49,11 +70,11 @@ ons_data <- as.data.frame(services) |>
str_detect(service, "/Middle_Layer") ~ "MSOA",
str_detect(service, "/Output_Areas") ~ "OA",
),
boundary = as.factor(boundary)
boundary_short = as.factor(boundary_short)
) |>
mutate(
boundary_type = case_when(
boundary %in% c(
boundary_short %in% c(
"CAUTH", "CTYUA", "CTY", "CTRY", "CED", "LAD",
"LPA", "MCTY", "PARNCP", "PAR", "RGN", "UTLA", "WD"
) ~
Expand Down Expand Up @@ -90,7 +111,7 @@ ons_boundaries <- ons_data |>
# Create URL to query featureserver and return a geojson file.
mutate(url_download = paste0(service, "/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")) |>
# Create unique id
mutate(id = paste(boundary, year, detail_level, sep = "_")) |>
mutate(id = paste(boundary_short, year, detail_level, sep = "_")) |>
relocate(id)

usethis::use_data(ons_boundaries, overwrite = TRUE)
Binary file modified data/ons_boundaries.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/boundaries_get.Rd

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

8 changes: 5 additions & 3 deletions man/boundaries_select.Rd

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

9 changes: 5 additions & 4 deletions man/ons_boundaries.Rd

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

0 comments on commit bd7bccc

Please sign in to comment.