diff --git a/R/boundaries_get.R b/R/boundaries_get.R index b590396..3e54ec9 100644 --- a/R/boundaries_get.R +++ b/R/boundaries_get.R @@ -4,10 +4,10 @@ #' retrieves the requested geographical boundaries in the form of a sf object. #' #' @param boundary a string containing ... Accepted values are: -#' `r levels(data_urls$boundary)` +#' `r levels(ons_boundaries$boundary)` #' @param year a number containing... #' @param detail_level a string defining the level of detail in the geometry. -#' Accepted values are: `r levels(data_urls$resolution)`. Each value +#' Accepted values are: `r levels(ons_boundaries$detail_level)`. Each value #' corresponds to: #' #' - Full Extent (BFE) – Full resolution boundaries go to the Extent of the Realm (Low Water Mark) and are the most detailed of the boundaries. @@ -44,11 +44,11 @@ boundaries_get <- function(boundary, year = NULL, detail_level = "BUC") { class = "error_not_single_string" ) } - if (!boundary %in% levels(data_urls$boundary)) { + if (!boundary %in% levels(ons_boundaries$boundary)) { cli::cli_abort( paste( "`boundary` must be one of these values:", - levels(data_urls$boundary) + levels(ons_boundaries$boundary) ), class = "error_boundary_not_valid" ) @@ -70,7 +70,7 @@ boundaries_get <- function(boundary, year = NULL, detail_level = "BUC") { lookup <- paste(boundary, year, detail_level, sep = "_") - url <- data_urls$url_download[data_urls$id == lookup] + url <- ons_boundaries$url_download[ons_boundaries$id == lookup] if (length(url) == 0) { cli::cli_abort( diff --git a/R/data.R b/R/data.R new file mode 100644 index 0000000..f0dcf73 --- /dev/null +++ b/R/data.R @@ -0,0 +1,26 @@ +#' ONS Boundaries +#' +#' Metadata about a subset of the geographical boundaries from [ONS' +#' geoportal](https://geoportal.statistics.gov.uk/) that can be downloaded by +#' `{ukgeographies}`. +#' These boundaries have been retrieved from the services provided by +#' [ONS' geoportal API](https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/) and +#' automatically categorised from their URLS, to +#' match Geoportal's menus and datasets. +#' +#' @format +#' A data frame with `r nrow(ons_boundaries)` rows and `r ncol(ons_boundaries)` +#' columns: +#' \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{year}{Year in which the boundaries were created} +#' \item{url_download}{URL querying the API service to return all features as a geojson file} +#' +#' } +#' +#' @source +"ons_boundaries" diff --git a/R/sysdata.rda b/R/sysdata.rda deleted file mode 100644 index 902d8cd..0000000 Binary files a/R/sysdata.rda and /dev/null differ diff --git a/data-raw/data-urls.R b/data-raw/data-urls.R index 1932649..f642c7b 100644 --- a/data-raw/data-urls.R +++ b/data-raw/data-urls.R @@ -23,33 +23,31 @@ services <- ons_geoportal |> # Build the dataframe ----------------------------------------------------- -data_urls <- as.data.frame(services) |> +ons_data <- as.data.frame(services) |> + rename(service = services) |> # Remove Mapserver - filter(str_detect(services, "/FeatureServer")) |> + filter(str_detect(service, "/FeatureServer")) |> # Convert absolute URLS - mutate(services = paste0(geoportal_base_url, services)) |> - mutate(type = case_when( - str_detect(services, "Lookup") ~ "Lookup", - )) |> + mutate(service = paste0(geoportal_base_url, service)) |> # Infer categories from titles mutate( boundary = case_when( - str_detect(services, "Combined_Authorities") ~ "CAUTH", - str_detect(services, "/Counties_and_Unitary_Authorities") ~ "CTYUA", - str_detect(services, "/Counties_") ~ "CTY", - str_detect(services, "/Countries_") ~ "CTRY", - str_detect(services, "/County_Electoral_Division") ~ "CED", - str_detect(services, "/Local_Authority_Districts") ~ "LAD", - str_detect(services, "/Local_Planning_Authorities") ~ "LPA", - str_detect(services, "/Metropolitan_Counties") ~ "MCTY", - str_detect(services, "/Parishes_and_Non_Civil_Parished_Areas") ~ "PARNCP", - str_detect(services, "/Parishes") ~ "PAR", - str_detect(services, "/Regions") ~ "RGN", - str_detect(services, "/Upper_Tier") ~ "UTLA", - str_detect(services, "/Wards") ~ "WD", - str_detect(services, "/Lower_Layer") ~ "LSOA", - str_detect(services, "/Middle_Layer") ~ "MSOA", - str_detect(services, "/Output_Areas") ~ "OA", + str_detect(service, "Combined_Authorities") ~ "CAUTH", + str_detect(service, "/Counties_and_Unitary_Authorities") ~ "CTYUA", + str_detect(service, "/Counties_") ~ "CTY", + str_detect(service, "/Countries_") ~ "CTRY", + str_detect(service, "/County_Electoral_Division") ~ "CED", + str_detect(service, "/Local_Authority_Districts") ~ "LAD", + str_detect(service, "/Local_Planning_Authorities") ~ "LPA", + str_detect(service, "/Metropolitan_Counties") ~ "MCTY", + str_detect(service, "/Parishes_and_Non_Civil_Parished_Areas") ~ "PARNCP", + str_detect(service, "/Parishes") ~ "PAR", + str_detect(service, "/Regions") ~ "RGN", + str_detect(service, "/Upper_Tier") ~ "UTLA", + str_detect(service, "/Wards") ~ "WD", + str_detect(service, "/Lower_Layer") ~ "LSOA", + str_detect(service, "/Middle_Layer") ~ "MSOA", + str_detect(service, "/Output_Areas") ~ "OA", ), boundary = as.factor(boundary) ) |> @@ -66,25 +64,33 @@ data_urls <- as.data.frame(services) |> ) |> relocate(boundary_type, .before = boundary) |> mutate( - resolution = case_when( - str_detect(services, "_BFC") ~ "BFC", - str_detect(services, "_BFE") ~ "BFE", - str_detect(services, "_BGC") ~ "BGC", - str_detect(services, "_BUC") ~ "BUC" + detail_level = case_when( + str_detect(service, "_BFC") ~ "BFC", + str_detect(service, "_BFE") ~ "BFE", + str_detect(service, "_BGC") ~ "BGC", + str_detect(service, "_BUC") ~ "BUC" ), - detail_level = as.factor(resolution) + detail_level = as.factor(detail_level) ) |> mutate( - year = str_extract(services, "_(19|20)(\\d){2}"), + year = str_extract(service, "_(19|20)(\\d){2}"), year = as.numeric(str_remove(year, "_")) ) |> + mutate( + type = case_when( + str_detect(service, "Lookup") ~ "Lookup", + !is.na(boundary) ~ "Boundary", + ), + type = as.factor(type) + ) + +ons_boundaries <- ons_data |> + filter(type == "Boundary") |> + select(-type) |> # Create URL to query featureserver and return a geojson file. - mutate(url_download = paste0(services, "/0/query?where=1%3D1&outFields=*&outSR=4326&f=json")) |> - # - # data_boundaries <- data_urls |> - # filter(!is.na(boundary)) |> + 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 = "_")) |> relocate(id) -usethis::use_data(data_urls, overwrite = TRUE, internal = TRUE) +usethis::use_data(ons_boundaries, overwrite = TRUE) diff --git a/data/ons_boundaries.rda b/data/ons_boundaries.rda new file mode 100644 index 0000000..4db78ec Binary files /dev/null and b/data/ons_boundaries.rda differ diff --git a/man/boundaries_get.Rd b/man/boundaries_get.Rd index bc96ce4..d78310e 100644 --- a/man/boundaries_get.Rd +++ b/man/boundaries_get.Rd @@ -4,7 +4,7 @@ \alias{boundaries_get} \title{Retrieve Geographical Boundaries} \usage{ -boundaries_get(boundary, year, detail_level = "BUC") +boundaries_get(boundary, year = NULL, detail_level = "BUC") } \arguments{ \item{boundary}{a string containing ... Accepted values are: diff --git a/man/ons_boundaries.Rd b/man/ons_boundaries.Rd new file mode 100644 index 0000000..e96943a --- /dev/null +++ b/man/ons_boundaries.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{ons_boundaries} +\alias{ons_boundaries} +\title{ONS Boundaries} +\format{ +A data frame with 513 rows and 7 +columns: +\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 \href{https://www.ons.gov.uk/methodology/geography/geographicalproducts/digitalboundaries}{Digital boundaries} } +\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} + +} +} +\source{ +\url{https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/} +} +\usage{ +ons_boundaries +} +\description{ +Metadata about a subset of the geographical boundaries from \href{https://geoportal.statistics.gov.uk/}{ONS' geoportal} that can be downloaded by +\code{{ukgeographies}}. +These boundaries have been retrieved from the services provided by +\href{https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/}{ONS' geoportal API} and +automatically categorised from their URLS, to +match Geoportal's menus and datasets. +} +\keyword{datasets}