diff --git a/.Rbuildignore b/.Rbuildignore index c4363469..be48ce7e 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -21,3 +21,5 @@ ^depends\.Rds$ ^revdep$ ^CRAN-SUBMISSION$ +^[\.]?air\.toml$ +^\.vscode$ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..344f76eb --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "Posit.air-vscode" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..f2d0b79d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[r]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "Posit.air-vscode" + } +} diff --git a/R/bcdata-package.R b/R/bcdata-package.R index 5ffeea97..e47153a7 100644 --- a/R/bcdata-package.R +++ b/R/bcdata-package.R @@ -10,7 +10,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - #' @keywords internal #' "_PACKAGE" @@ -21,7 +20,7 @@ NULL release_bullets <- function() { c( - "Run test and check with internet turned off", + "Run test and check with internet turned off", "Precompile vignettes" ) } diff --git a/R/bcdc-get-citation.R b/R/bcdc-get-citation.R index 7ff2682e..0dd4cb46 100644 --- a/R/bcdc-get-citation.R +++ b/R/bcdc-get-citation.R @@ -51,13 +51,15 @@ bcdc_get_citation <- function(record) { #' @export bcdc_get_citation.default <- function(record) { - stop("No bcdc_get_citation method for an object of class ", class(record), - call. = FALSE) + stop( + "No bcdc_get_citation method for an object of class ", + class(record), + call. = FALSE + ) } #' @export bcdc_get_citation.character <- function(record) { - if (grepl("/resource/", record)) { # A full url was passed including record and resource compenents. # Grab the resource id and strip it off the url @@ -65,16 +67,13 @@ bcdc_get_citation.character <- function(record) { record <- gsub("/resource/.+", "", record) } - rec <- bcdc_get_record(record) bcdc_get_citation(rec) - } #' @export bcdc_get_citation.bcdc_record <- function(record) { - bib_rec <- utils::bibentry( bibtype = "techreport", title = record$title, @@ -87,9 +86,7 @@ bcdc_get_citation.bcdc_record <- function(record) { note = record$license_title ) - structure(bib_rec, - class = c("citation", setdiff(class(bib_rec), "citation")) - ) + structure(bib_rec, class = c("citation", setdiff(class(bib_rec), "citation"))) } clean_org_name <- function(rec) { diff --git a/R/bcdc-web-services.R b/R/bcdc-web-services.R index e038e216..6dcd8e62 100644 --- a/R/bcdc-web-services.R +++ b/R/bcdc-web-services.R @@ -10,7 +10,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - #' Query data from the B.C. Web Feature Service #' #' Queries features from the B.C. Web Feature Service. See @@ -60,7 +59,7 @@ #' res <- bcdc_query_geodata("bc-airports") %>% #' filter(PHYSICAL_ADDRESS %LIKE% 'Vict%') #' ) -#' +#' #' # To query using %IN% #' try( #' res <- bcdc_query_geodata("bc-airports") %>% @@ -111,13 +110,15 @@ bcdc_query_geodata <- function(record, crs = 3005) { #' @export bcdc_query_geodata.default <- function(record, crs = 3005) { - stop("No bcdc_query_geodata method for an object of class ", class(record), - call. = FALSE) + stop( + "No bcdc_query_geodata method for an object of class ", + class(record), + call. = FALSE + ) } #' @export bcdc_query_geodata.character <- function(record, crs = 3005) { - if (length(record) != 1) { stop("Only one record my be queried at a time.", call. = FALSE) } @@ -137,8 +138,12 @@ bcdc_query_geodata.character <- function(record, crs = 3005) { cols_df <- feature_helper(record) return( - as.bcdc_promise(list(query_list = query_list, cli = cli, record = NULL, - cols_df = cols_df)) + as.bcdc_promise(list( + query_list = query_list, + cli = cli, + record = NULL, + cols_df = cols_df + )) ) } @@ -156,8 +161,9 @@ bcdc_query_geodata.character <- function(record, crs = 3005) { #' @export bcdc_query_geodata.bcdc_record <- function(record, crs = 3005) { if (!any(wfs_available(record$resource_df))) { - stop("No Web Feature Service resource available for this data set.", - call. = FALSE + stop( + "No Web Feature Service resource available for this data set.", + call. = FALSE ) } @@ -170,7 +176,9 @@ bcdc_query_geodata.bcdc_record <- function(record, crs = 3005) { )) if (grepl("_SP?G$", layer_name)) { - message("You are accessing a simplified view of the data - see the catalogue record for details.") + message( + "You are accessing a simplified view of the data - see the catalogue record for details." + ) } ## Parameters for the API call @@ -184,8 +192,12 @@ bcdc_query_geodata.bcdc_record <- function(record, crs = 3005) { cols_df <- feature_helper(query_list$typeNames) - as.bcdc_promise(list(query_list = query_list, cli = cli, record = record, - cols_df = cols_df)) + as.bcdc_promise(list( + query_list = query_list, + cli = cli, + record = record, + cols_df = cols_df + )) } #' Get preview map from the B.C. Web Map Service @@ -213,20 +225,23 @@ bcdc_query_geodata.bcdc_record <- function(record, crs = 3005) { #' ) #' } #' @export -bcdc_preview <- function(record) { # nocov start +bcdc_preview <- function(record) { + # nocov start if (!has_internet()) stop("No access to internet", call. = FALSE) UseMethod("bcdc_preview") } #' @export bcdc_preview.default <- function(record) { - stop("No bcdc_preview method for an object of class ", class(record), - call. = FALSE) + stop( + "No bcdc_preview method for an object of class ", + class(record), + call. = FALSE + ) } #' @export bcdc_preview.character <- function(record) { - if (is_whse_object_name(record)) { make_wms(record) } else { @@ -236,30 +251,36 @@ bcdc_preview.character <- function(record) { #' @export bcdc_preview.bcdc_record <- function(record) { - wfs_resource <- get_wfs_resource_from_record(record) make_wms(basename(dirname(wfs_resource$url))) - } -make_wms <- function(x){ +make_wms <- function(x) { wms_url <- wms_base_url() - wms_options <- leaflet::WMSTileOptions(format = "image/png", - transparent = TRUE, - attribution = "BC Data Catalogue (https://catalogue.data.gov.bc.ca/)") - wms_legend <- glue::glue("{wms_url}?request=GetLegendGraphic& + wms_options <- leaflet::WMSTileOptions( + format = "image/png", + transparent = TRUE, + attribution = "BC Data Catalogue (https://catalogue.data.gov.bc.ca/)" + ) + wms_legend <- glue::glue( + "{wms_url}?request=GetLegendGraphic& format=image%2Fpng& width=20& height=20& - layer=pub%3A{x}") + layer=pub%3A{x}" + ) leaflet::leaflet() %>% - leaflet::addProviderTiles(leaflet::providers$CartoDB.Positron, - options = leaflet::providerTileOptions(noWrap = TRUE)) %>% - leaflet::addWMSTiles(wms_url, - layers=glue::glue("pub:{x}"), - options = wms_options) %>% + leaflet::addProviderTiles( + leaflet::providers$CartoDB.Positron, + options = leaflet::providerTileOptions(noWrap = TRUE) + ) %>% + leaflet::addWMSTiles( + wms_url, + layers = glue::glue("pub:{x}"), + options = wms_options + ) %>% leaflet.extras::addWMSLegend(uri = wms_legend) %>% leaflet::setView(lng = -126.5, lat = 54.5, zoom = 5) } # nocov end @@ -275,4 +296,3 @@ make_query_list <- function(layer_name, crs) { SRSNAME = paste0("EPSG:", crs) ) } - diff --git a/R/bcdc_browse.R b/R/bcdc_browse.R index 158f8a9d..f37e086a 100644 --- a/R/bcdc_browse.R +++ b/R/bcdc_browse.R @@ -44,23 +44,25 @@ #' bcdc_browse("76b1b7a3-2112-4444-857a-afccf7b20da8") #' ) #' } -bcdc_browse <- function(query = NULL, browser = getOption("browser"), - encodeIfNeeded = FALSE) { - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov +bcdc_browse <- function( + query = NULL, + browser = getOption("browser"), + encodeIfNeeded = FALSE +) { + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov base_url <- catalogue_base_url() - if (is.null(query)) { + if (is.null(query)) { url <- base_url } else { - ## Check if the record is valid, if not return a query. # Need to check via HEAD request to the api as the catalogue # doesn't return a 404 status code. cli <- bcdc_catalogue_client("action/package_show") res <- cli$head(query = list(id = query)) - if(res$status_code == 404){ + if (res$status_code == 404) { stop("The specified record does not exist in the catalogue") ## NB - previous version would show a catalogue search in the ## browser, but with new catalogue it doesn't seem possible @@ -73,14 +75,15 @@ bcdc_browse <- function(query = NULL, browser = getOption("browser"), } ## Facilitates testing - if(interactive()){ + if (interactive()) { # nocov start - utils::browseURL(url = url, browser = browser, - encodeIfNeeded = encodeIfNeeded) + utils::browseURL( + url = url, + browser = browser, + encodeIfNeeded = encodeIfNeeded + ) # nocov end } invisible(url) } - - diff --git a/R/bcdc_options.R b/R/bcdc_options.R index 1a456fa9..b9d29402 100644 --- a/R/bcdc_options.R +++ b/R/bcdc_options.R @@ -92,14 +92,27 @@ bcdc_options <- function() { server_single_download_limit <- bcdc_single_download_limit() dplyr::tribble( - ~option, ~value, ~default, - "bcdata.max_geom_pred_size", null_to_na(getOption("bcdata.max_geom_pred_size")), 5E5, - "bcdata.chunk_limit", null_to_na(getOption("bcdata.chunk_limit")), server_single_download_limit, + ~option, + ~value, + ~default, + "bcdata.max_geom_pred_size", + null_to_na(getOption("bcdata.max_geom_pred_size")), + 5E5, + "bcdata.chunk_limit", + null_to_na(getOption("bcdata.chunk_limit")), + server_single_download_limit, "bcdata.single_download_limit", - null_to_na(deprecate_single_download_limit_option()), server_single_download_limit, - "bcdata.max_package_search_limit", null_to_na(getOption("bcdata.max_package_search_limit")), 1000, - "bcdata.max_package_search_facet_limit", null_to_na(getOption("bcdata.max_package_search_facet_limit")), 1000, - "bcdata.max_group_package_show_limit", null_to_na(getOption("bcdata.max_group_package_show_limit")), 1000 + null_to_na(deprecate_single_download_limit_option()), + server_single_download_limit, + "bcdata.max_package_search_limit", + null_to_na(getOption("bcdata.max_package_search_limit")), + 1000, + "bcdata.max_package_search_facet_limit", + null_to_na(getOption("bcdata.max_package_search_facet_limit")), + 1000, + "bcdata.max_group_package_show_limit", + null_to_na(getOption("bcdata.max_group_package_show_limit")), + 1000 ) } @@ -112,7 +125,12 @@ check_chunk_limit <- function() { return(single_download_limit) } if (chunk_limit > single_download_limit) { - stop(glue::glue("Your chunk value of {chunk_limit} exceeds the BC Data Catalogue chunk limit of {single_download_limit}"), call. = FALSE) + stop( + glue::glue( + "Your chunk value of {chunk_limit} exceeds the BC Data Catalogue chunk limit of {single_download_limit}" + ), + call. = FALSE + ) } chunk_limit } @@ -129,11 +147,13 @@ bcdc_get_capabilities <- function() { cli <- bcdc_http_client(url, auth = FALSE) get_caps <- function(cli) { - cc <- cli$get(query = list( - SERVICE = "WFS", - VERSION = "2.0.0", - REQUEST = "GetCapabilities" - )) + cc <- cli$get( + query = list( + SERVICE = "WFS", + VERSION = "2.0.0", + REQUEST = "GetCapabilities" + ) + ) cc$raise_for_status() res <- cc$parse("UTF-8") xml2::read_xml(res) @@ -158,15 +178,26 @@ bcdc_get_capabilities <- function() { bcdc_get_wfs_records <- function() { doc <- bcdc_get_capabilities() - if (is.null(doc)) stop("Unable to access wfs listing from server. Please open an issue. ", call. = FALSE) + if (is.null(doc)) + stop( + "Unable to access wfs listing from server. Please open an issue. ", + call. = FALSE + ) # d1 is the default xml namespace (see xml2::xml_ns(doc)) features <- xml2::xml_find_all(doc, "./d1:FeatureTypeList/d1:FeatureType") tibble::tibble( - whse_name = gsub("^pub:", "", xml2::xml_text(xml2::xml_find_first(features, "./d1:Name"))), + whse_name = gsub( + "^pub:", + "", + xml2::xml_text(xml2::xml_find_first(features, "./d1:Name")) + ), title = xml2::xml_text(xml2::xml_find_first(features, "./d1:Title")), - cat_url = xml2::xml_attr(xml2::xml_find_first(features, "./d1:MetadataURL"), "href") + cat_url = xml2::xml_attr( + xml2::xml_find_first(features, "./d1:MetadataURL"), + "href" + ) ) } @@ -174,7 +205,9 @@ bcdc_single_download_limit <- function() { doc <- bcdc_get_capabilities() if (is.null(doc)) { - message("Unable to access server to determine single download limit; using default download limit of 10000") + message( + "Unable to access server to determine single download limit; using default download limit of 10000" + ) return(10000L) } diff --git a/R/bcdc_search.R b/R/bcdc_search.R index 263ec52d..3341ec7b 100644 --- a/R/bcdc_search.R +++ b/R/bcdc_search.R @@ -29,10 +29,17 @@ #' bcdc_search_facets("res_format") #' ) #' } -bcdc_search_facets <- function(facet = c("license_id", "download_audience", - "res_format", "publish_state", - "organization", "groups")) { - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov +bcdc_search_facets <- function( + facet = c( + "license_id", + "download_audience", + "res_format", + "publish_state", + "organization", + "groups" + ) +) { + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov facet <- match.arg(facet, several.ok = TRUE) query <- paste0("\"", facet, "\"", collapse = ",") @@ -42,7 +49,13 @@ bcdc_search_facets <- function(facet = c("license_id", "download_audience", option_facet_limit <- getOption("bcdata.max_package_search_facet_limit", 1000) - r <- cli$get(query = list(facet.field = query, rows = 0, facet.limit = option_facet_limit)) + r <- cli$get( + query = list( + facet.field = query, + rows = 0, + facet.limit = option_facet_limit + ) + ) r$raise_for_status() res <- jsonlite::fromJSON(r$parse("UTF-8")) @@ -53,11 +66,9 @@ bcdc_search_facets <- function(facet = c("license_id", "download_audience", facet_dfs <- lapply(facet_list, function(x) { x$items$facet <- x$title x$items[, c("facet", setdiff(names(x$items), "facet"))] - } - ) + }) dplyr::bind_rows(facet_dfs) - } #' @export @@ -80,7 +91,7 @@ bcdc_list_groups <- function() bcdc_search_facets("groups") #' } bcdc_list_group_records <- function(group) { - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov cli <- bcdc_catalogue_client("action/group_package_show") @@ -88,8 +99,11 @@ bcdc_list_group_records <- function(group) { r <- cli$get(query = list(id = group, limit = option_group_limit)) - if (r$status_code == 404){ - stop("404: URL not found - you may have specified an invalid group?", call. = FALSE) + if (r$status_code == 404) { + stop( + "404: URL not found - you may have specified an invalid group?", + call. = FALSE + ) } r$raise_for_status() @@ -99,7 +113,6 @@ bcdc_list_group_records <- function(group) { d <- tibble::as_tibble(res$result) as.bcdc_group(d, description = res$result$description) - } #' @export @@ -122,19 +135,24 @@ bcdc_list_organizations <- function() bcdc_search_facets("organization") #' } bcdc_list_organization_records <- function(organization) { - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov option_package_limit <- getOption("bcdata.max_package_search_limit", 1000) cli <- bcdc_catalogue_client("action/package_search") - r <- cli$get(query = list( - fq = paste0("organization:", organization), # filter query for the organization - rows = option_package_limit - )) + r <- cli$get( + query = list( + fq = paste0("organization:", organization), # filter query for the organization + rows = option_package_limit + ) + ) - if (r$status_code == 404){ - stop("404: URL not found - you may have specified an invalid organization?", call. = FALSE) + if (r$status_code == 404) { + stop( + "404: URL not found - you may have specified an invalid organization?", + call. = FALSE + ) } r$raise_for_status() @@ -144,7 +162,6 @@ bcdc_list_organization_records <- function(organization) { d <- tibble::as_tibble(res$result$results) as.bcdc_organization(d, description = res$result$description) - } #' Return a full list of the names of B.C. Data Catalogue records @@ -158,14 +175,13 @@ bcdc_list_organization_records <- function(organization) { #' ) #' } bcdc_list <- function() { - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov l_new_ret <- 1 ret <- character() offset <- 0 limit <- 1000 while (l_new_ret) { - cli <- bcdc_catalogue_client("action/package_list") r <- cli$get(query = list(offset = offset, limit = limit)) @@ -214,26 +230,29 @@ bcdc_list <- function() { #' bcdc_search("angling", groups = "bc-tourism") #' ) #' } -bcdc_search <- function(..., license_id = NULL, - download_audience = NULL, - res_format = NULL, - sector = NULL, - organization = NULL, - groups = NULL, - n = 100) { - +bcdc_search <- function( + ..., + license_id = NULL, + download_audience = NULL, + res_format = NULL, + sector = NULL, + organization = NULL, + groups = NULL, + n = 100 +) { if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov # TODO: allow terms to be passed as a vector, and allow use of | for OR terms <- process_search_terms(...) - facets <- compact(list(license_id = license_id, - download_audience = download_audience, - res_format = res_format, - sector = sector, - organization = organization, - groups = groups - )) + facets <- compact(list( + license_id = license_id, + download_audience = download_audience, + res_format = res_format, + sector = sector, + organization = organization, + groups = groups + )) # build query by collating the terms and any user supplied facets # if there are no supplied facets (e.g., is_empty(facets) returns TRUE) just use terms) @@ -244,17 +263,20 @@ bcdc_search <- function(..., license_id = NULL, lapply(names(facets), function(x) { facet_vals <- bcdc_search_facets(x) if (!facets[x] %in% facet_vals$name) { - stop(facets[x], " is not a valid value for ", x, - call. = FALSE) + stop(facets[x], " is not a valid value for ", x, call. = FALSE) } }) - paste0(terms, "+", paste( - names(facets), - paste0("\"", facets, "\""), - sep = ":", - collapse = "+" - )) + paste0( + terms, + "+", + paste( + names(facets), + paste0("\"", facets, "\""), + sep = ":", + collapse = "+" + ) + ) } query <- gsub("\\s+", "%20", query) @@ -272,10 +294,16 @@ bcdc_search <- function(..., license_id = NULL, cont <- res$result n_found <- cont$count - if(n_found > n){ - message("Found ", n_found, " matches. Returning the first ", n, - ".\nTo see them all, rerun the search and set the 'n' argument to ", - n_found, ".") + if (n_found > n) { + message( + "Found ", + n_found, + " matches. Returning the first ", + n, + ".\nTo see them all, rerun the search and set the 'n' argument to ", + n_found, + "." + ) } ret <- cont$results names(ret) <- vapply(ret, `[[`, "name", FUN.VALUE = character(1)) @@ -317,8 +345,7 @@ bcdc_search <- function(..., license_id = NULL, #' ) #' } bcdc_get_record <- function(id) { - - if(!has_internet()) stop("No access to internet", call. = FALSE) # nocov + if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov id <- slug_from_url(id) @@ -326,8 +353,15 @@ bcdc_get_record <- function(id) { r <- cli$get(query = list(id = id)) - if (r$status_code == 404){ - stop(paste0("'", id, "' is not a valid record id or name in the B.C. Data Catalogue"), call. = FALSE) + if (r$status_code == 404) { + stop( + paste0( + "'", + id, + "' is not a valid record id or name in the B.C. Data Catalogue" + ), + call. = FALSE + ) } r$raise_for_status() @@ -339,8 +373,11 @@ bcdc_get_record <- function(id) { if (ret$id != id) { get_record_warn_once( - "It is advised to use the permanent id ('", ret$id, "') ", - "rather than the name of the record ('", id, + "It is advised to use the permanent id ('", + ret$id, + "') ", + "rather than the name of the record ('", + id, "') to guard against future name changes.\n" ) } @@ -367,15 +404,19 @@ as.bcdc_recordlist <- function(x) { } as.bcdc_group <- function(x, description) { - structure(x, - class = c("bcdc_group", setdiff(class(x), "bcdc_group")), - description = description) + structure( + x, + class = c("bcdc_group", setdiff(class(x), "bcdc_group")), + description = description + ) } as.bcdc_organization <- function(x, description) { - structure(x, - class = c("bcdc_organization", setdiff(class(x), "bcdc_organization")), - description = description) + structure( + x, + class = c("bcdc_organization", setdiff(class(x), "bcdc_organization")), + description = description + ) } #' Provide a data frame containing the metadata for all resources from a single B.C. Data Catalogue record @@ -401,7 +442,7 @@ as.bcdc_organization <- function(x, description) { #' } #' #' @export -bcdc_tidy_resources <- function(record){ +bcdc_tidy_resources <- function(record) { if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov UseMethod("bcdc_tidy_resources") } @@ -409,14 +450,16 @@ bcdc_tidy_resources <- function(record){ #' @export bcdc_tidy_resources.default <- function(record) { - stop("No bcdc_tidy_resources method for an object of class ", class(record), - call. = FALSE) + stop( + "No bcdc_tidy_resources method for an object of class ", + class(record), + call. = FALSE + ) } #' @export -bcdc_tidy_resources.character <- function(record){ - +bcdc_tidy_resources.character <- function(record) { if (is_whse_object_name(record)) { stop("No bcdc_tidy_resources method for a BCGW object name", call. = FALSE) } diff --git a/R/cli.R b/R/cli.R index af7c525c..08e93d40 100644 --- a/R/cli.R +++ b/R/cli.R @@ -10,7 +10,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - bold_blue <- function(...) { cli::col_blue(cli::style_bold(...)) } diff --git a/R/cql-geom-predicates.R b/R/cql-geom-predicates.R index 62c32fa2..749a1b8a 100644 --- a/R/cql-geom-predicates.R +++ b/R/cql-geom-predicates.R @@ -55,18 +55,29 @@ CQL <- function(...) { #' } #' #' @noRd -bcdc_cql_string <- function(x, geometry_predicates, pattern = NULL, - distance = NULL, units = NULL, - coords = NULL, crs = NULL){ - +bcdc_cql_string <- function( + x, + geometry_predicates, + pattern = NULL, + distance = NULL, + units = NULL, + coords = NULL, + crs = NULL +) { if (inherits(x, "sql")) { - stop(glue::glue("object {as.character(x)} not found.\n The object passed to {geometry_predicates} needs to be valid sf object."), - call. = FALSE) + stop( + glue::glue( + "object {as.character(x)} not found.\n The object passed to {geometry_predicates} needs to be valid sf object." + ), + call. = FALSE + ) } if (inherits(x, "bcdc_promise")) { - stop("To use spatial operators, you need to use collect() to retrieve the object used to filter", - call. = FALSE) + stop( + "To use spatial operators, you need to use collect() to retrieve the object used to filter", + call. = FALSE + ) } match.arg(geometry_predicates, cql_geom_predicate_list()) @@ -91,28 +102,37 @@ bcdc_cql_string <- function(x, geometry_predicates, pattern = NULL, x } - CQL(paste0(geometry_predicates,"({geom_name}, ", cql_args, ")")) + CQL(paste0(geometry_predicates, "({geom_name}, ", cql_args, ")")) } ## Geometry Predicates cql_geom_predicate_list <- function() { - c("EQUALS","DISJOINT","INTERSECTS", - "TOUCHES", "CROSSES", "WITHIN", - "CONTAINS","OVERLAPS", "RELATE", - "DWITHIN", "BEYOND", "BBOX") + c( + "EQUALS", + "DISJOINT", + "INTERSECTS", + "TOUCHES", + "CROSSES", + "WITHIN", + "CONTAINS", + "OVERLAPS", + "RELATE", + "DWITHIN", + "BEYOND", + "BBOX" + ) } sf_text <- function(x, pred) { - if (!bcdc_check_geom_size(x)) { message( bold_red( glue::glue( "A bounding box was drawn around the object passed to {pred} and all features within the box will be returned." - ) ) ) + ) x <- sf::st_bbox(x) } @@ -155,8 +175,10 @@ sf_text <- function(x, pred) { #' } bcdc_check_geom_size <- function(x) { if (!inherits(x, c("sf", "sfc", "sfg", "bbox"))) { - stop(paste(deparse(substitute(x)), "is not a valid sf object"), - call. = FALSE) + stop( + paste(deparse(substitute(x)), "is not a valid sf object"), + call. = FALSE + ) } if (inherits(x, "bbox")) return(invisible(TRUE)) @@ -168,9 +190,13 @@ bcdc_check_geom_size <- function(x) { ## If size ok, return TRUE if (obj_size < option_size) return(invisible(TRUE)) - message(bold_blue(glue::glue("The object is too large to perform exact spatial operations using bcdata."))) + message(bold_blue(glue::glue( + "The object is too large to perform exact spatial operations using bcdata." + ))) message(bold_blue(glue::glue("Object size: {obj_size} bytes"))) - message(bold_blue(glue::glue("BC Data Threshold: {formatC(option_size, format = 'd')} bytes"))) + message(bold_blue(glue::glue( + "BC Data Threshold: {formatC(option_size, format = 'd')} bytes" + ))) message(bold_blue(glue::glue("Exceedance: {obj_size-option_size} bytes"))) message(bold_blue("See ?bcdc_check_geom_size for more details")) @@ -247,11 +273,15 @@ OVERLAPS <- function(geom) { #' `*TF012`. Example: `'1*T***T**'` #' @noRd RELATE <- function(geom, pattern) { - if (!is.character(pattern) || + if ( + !is.character(pattern) || length(pattern) != 1L || - !grepl("^[*TF012]{9}$", pattern)) { - stop("pattern must be a 9-character string using the characters '*TF012'", - call. = FALSE) + !grepl("^[*TF012]{9}$", pattern) + ) { + stop( + "pattern must be a 9-character string using the characters '*TF012'", + call. = FALSE + ) } bcdc_cql_string(geom, "RELATE", pattern = pattern) } @@ -266,8 +296,7 @@ RELATE <- function(geom, pattern) { #' (For example, `'EPSG:3005'` or just `3005`. The default is to use the CRS of #' the queried layer) #' @export -BBOX <- function(coords, crs = NULL){ - +BBOX <- function(coords, crs = NULL) { if (inherits(coords, c("sf", "sfc"))) { coords <- sf::st_bbox(coords) } @@ -286,8 +315,10 @@ BBOX <- function(coords, crs = NULL){ } if (!is.null(crs) && !(is.character(crs) && length(crs) == 1L)) { - stop("crs must be a character string denoting the CRS (e.g., 'EPSG:4326')", - call. = FALSE) + stop( + "crs must be a character string denoting the CRS (e.g., 'EPSG:4326')", + call. = FALSE + ) } bcdc_cql_string(x = NULL, "BBOX", coords = coords, crs = crs) } @@ -297,8 +328,11 @@ BBOX <- function(coords, crs = NULL){ #' @param units units that distance is specified in. One of #' `"feet"`, `"meters"`, `"statute miles"`, `"nautical miles"`, `"kilometers"` #' @export -DWITHIN <- function(geom, distance, - units = c("meters", "feet", "statute miles", "nautical miles", "kilometers")) { +DWITHIN <- function( + geom, + distance, + units = c("meters", "feet", "statute miles", "nautical miles", "kilometers") +) { if (!is.numeric(distance)) { stop("'distance' must be numeric", call. = FALSE) } @@ -309,8 +343,11 @@ DWITHIN <- function(geom, distance, #' @rdname cql_geom_predicates #' @noRd # https://osgeo-org.atlassian.net/browse/GEOS-8922 -BEYOND <- function(geom, distance, - units = c("meters", "feet", "statute miles", "nautical miles", "kilometers")) { +BEYOND <- function( + geom, + distance, + units = c("meters", "feet", "statute miles", "nautical miles", "kilometers") +) { if (!is.numeric(distance)) { stop("'distance' must be numeric", call. = FALSE) } diff --git a/R/cql-translator.R b/R/cql-translator.R index 2a4733ca..644c35a0 100644 --- a/R/cql-translator.R +++ b/R/cql-translator.R @@ -34,14 +34,19 @@ cql_translate <- function(..., .colnames = character(0)) { ) }) - sql_where <- try(dbplyr::translate_sql_(dots, con = wfs_con, window = FALSE), - silent = TRUE) + sql_where <- try( + dbplyr::translate_sql_(dots, con = wfs_con, window = FALSE), + silent = TRUE + ) if (inherits(sql_where, "try-error")) { if (grepl("no applicable method", sql_where)) { - stop("Unable to process query. Did you use a function that should be evaluated locally? If so, try wrapping it in 'local()'.", call. = FALSE) + stop( + "Unable to process query. Did you use a function that should be evaluated locally? If so, try wrapping it in 'local()'.", + call. = FALSE + ) } - stop(sql_where, call. = FALSE) + stop(sql_where, call. = FALSE) } build_where(sql_where) @@ -110,19 +115,23 @@ no_agg <- function(f) { force(f) function(...) { - stop("Aggregation function `", f, "()` is not supported by this database", - call. = FALSE) + stop( + "Aggregation function `", + f, + "()` is not supported by this database", + call. = FALSE + ) } } # Construct the errors for common aggregation functions cql_agg <- dbplyr::sql_translator( - n = no_agg("n"), - mean = no_agg("mean"), - var = no_agg("var"), - sum = no_agg("sum"), - min = no_agg("min"), - max = no_agg("max") + n = no_agg("n"), + mean = no_agg("mean"), + var = no_agg("var"), + sum = no_agg("sum"), + min = no_agg("min"), + max = no_agg("max") ) #' @importFrom dbplyr dbplyr_edition @@ -135,9 +144,7 @@ dbplyr_edition.wfsConnection <- function(con) 2L #' @import DBI #' @export #' @keywords internal -setClass("wfsConnection", - contains = "DBIConnection" -) +setClass("wfsConnection", contains = "DBIConnection") # A dummy connection object to ensure the correct sql_translate is used wfs_con <- structure( @@ -169,8 +176,11 @@ setClass("CQL", contains = c("SQL", "character")) #' @rdname wfsConnection-class #' @exportMethod dbQuoteIdentifier #' @export -setMethod("dbQuoteIdentifier", c("wfsConnection", "CQL"), - function(conn, x) dbplyr::sql_quote(x, "\"")) +setMethod( + "dbQuoteIdentifier", + c("wfsConnection", "CQL"), + function(conn, x) dbplyr::sql_quote(x, "\"") +) # Make sure that strings (RHS of relations) are escaped with single quotes @@ -178,5 +188,8 @@ setMethod("dbQuoteIdentifier", c("wfsConnection", "CQL"), #' @rdname wfsConnection-class #' @exportMethod dbQuoteString #' @export -setMethod("dbQuoteString", c("wfsConnection", "CQL"), - function(conn, x) dbplyr::sql_quote(x, "'")) +setMethod( + "dbQuoteString", + c("wfsConnection", "CQL"), + function(conn, x) dbplyr::sql_quote(x, "'") +) diff --git a/R/describe-feature.R b/R/describe-feature.R index 41a05054..e447436b 100644 --- a/R/describe-feature.R +++ b/R/describe-feature.R @@ -39,20 +39,22 @@ #' } #' #' @export -bcdc_describe_feature <- function(record){ +bcdc_describe_feature <- function(record) { if (!has_internet()) stop("No access to internet", call. = FALSE) # nocov UseMethod("bcdc_describe_feature") } #' @export bcdc_describe_feature.default <- function(record) { - stop("No bcdc_describe_feature method for an object of class ", class(record), - call. = FALSE) + stop( + "No bcdc_describe_feature method for an object of class ", + class(record), + call. = FALSE + ) } #' @export -bcdc_describe_feature.character <- function(record){ - +bcdc_describe_feature.character <- function(record) { if (is_whse_object_name(record)) { bgc <- bcdc_get_wfs_records() cat_record <- bcdc_get_record(bgc$cat_url[grepl(record, bgc$whse_name)]) @@ -64,18 +66,14 @@ bcdc_describe_feature.character <- function(record){ #' @export -bcdc_describe_feature.bcdc_record <- function(record){ - +bcdc_describe_feature.bcdc_record <- function(record) { if (!any(wfs_available(record$resource_df))) { - stop("No WFS resource available for this data set.", - call. = FALSE - ) + stop("No WFS resource available for this data set.", call. = FALSE) } obj_desc_join(record) - } -parse_raw_feature_tbl <- function(query_list){ +parse_raw_feature_tbl <- function(query_list) { ## GET and parse data to sf object cli <- bcdc_wfs_client() @@ -89,30 +87,26 @@ parse_raw_feature_tbl <- function(query_list){ xml_res <- purrr::map(xml_res, xml2::xml_attrs) xml_df <- purrr::map_df(xml_res, ~ as.list(.)) - attr(xml_df, "geom_type") <- intersect(xml_df$type, gml_types()) return(xml_df) } -feature_helper <- function(whse_name){ - +feature_helper <- function(whse_name) { query_list <- list( SERVICE = "WFS", VERSION = "2.0.0", REQUEST = "DescribeFeatureType", - typeNames = whse_name) + typeNames = whse_name + ) ## This is an ugly way of doing this ## Manually add id and turn into a row - id_row <- dplyr::tibble(name = "id", - nillable = TRUE, - type = "xsd:string") + id_row <- dplyr::tibble(name = "id", nillable = TRUE, type = "xsd:string") xml_df <- parse_raw_feature_tbl(query_list) geom_type <- attr(xml_df, "geom_type") - ## Fix logicals xml_df$nillable = ifelse(xml_df$nillable == "true", FALSE, TRUE) xml_df <- xml_df[, c("name", "nillable", "type")] @@ -136,16 +130,19 @@ obj_desc_join <- function(record) { dplyr::left_join( feature_helper(whse_name), - wfs_df[,c("column_comments", "column_name")], + wfs_df[, c("column_comments", "column_name")], by = c("col_name" = "column_name") ) } get_wfs_resource_from_record <- function(record) { - wfs_res_id <- record$resource_df$id[record$resource_df$wfs_available] - is_wfs <- vapply(record$resources, function(x) { - x$id == wfs_res_id - }, FUN.VALUE = logical(1)) + is_wfs <- vapply( + record$resources, + function(x) { + x$id == wfs_res_id + }, + FUN.VALUE = logical(1) + ) record$resources[[which(is_wfs)]] } diff --git a/R/get_data.R b/R/get_data.R index 7daf020f..513f3181 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -127,14 +127,26 @@ bcdc_get_data <- function(record, resource = NULL, verbose = TRUE, ...) { } #' @export -bcdc_get_data.default <- function(record, resource = NULL, verbose = TRUE, ...) { - stop("No bcdc_get_data method for an object of class ", class(record), - call. = FALSE) +bcdc_get_data.default <- function( + record, + resource = NULL, + verbose = TRUE, + ... +) { + stop( + "No bcdc_get_data method for an object of class ", + class(record), + call. = FALSE + ) } #' @export -bcdc_get_data.character <- function(record, resource = NULL, verbose = TRUE, ...) { - +bcdc_get_data.character <- function( + record, + resource = NULL, + verbose = TRUE, + ... +) { if (is_whse_object_name(record)) { query <- bcdc_query_geodata(record, ...) return(collect(query)) @@ -154,20 +166,30 @@ bcdc_get_data.character <- function(record, resource = NULL, verbose = TRUE, ... } #' @export -bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, ...) { +bcdc_get_data.bcdc_record <- function( + record, + resource = NULL, + verbose = TRUE, + ... +) { record_id <- record$id # Only work with resources that are available to read into R resource_df <- record$resource_df[record$resource_df$bcdata_available, ] - if (!nrow(resource_df)) { - stop("There are no resources that bcdata can download from this record", call. = FALSE) + stop( + "There are no resources that bcdata can download from this record", + call. = FALSE + ) } ## fail if not using interactively and haven't specified resource if (is.null(resource) && nrow(resource_df) > 1L && !interactive()) { - stop("The record you are trying to access appears to have more than one resource.", call. = TRUE) + stop( + "The record you are trying to access appears to have more than one resource.", + call. = TRUE + ) } # get wms info @@ -185,10 +207,15 @@ bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, . ## non-wms; resource specified if (!is.null(resource)) { if (!resource %in% resource_df$id) { - stop("The specified resource does not exist in this record", call. = FALSE) + stop( + "The specified resource does not exist in this record", + call. = FALSE + ) } - return(read_from_url(resource_df[resource_df$id == resource, , drop = FALSE], - ...)) + return(read_from_url( + resource_df[resource_df$id == resource, , drop = FALSE], + ... + )) } ## non-wms; only one resource and not specified @@ -203,7 +230,9 @@ bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, . # nocov start if (interactive() && verbose) { - cat_line_wrap("The record you are trying to access appears to have more than one resource.") + cat_line_wrap( + "The record you are trying to access appears to have more than one resource." + ) cat_line() cat_line("Resources:") @@ -217,7 +246,8 @@ bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, . choices <- clean_wfs(resource_df$name) ## To deal with situations where the resource names are the same - if(any(duplicated(choices))) choices <- glue::glue("{choices} ({resource_df$format})") + if (any(duplicated(choices))) + choices <- glue::glue("{choices} ({resource_df$format})") choice_input <- utils::menu(choices, title = "Please choose one option:") @@ -235,8 +265,11 @@ bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, . resource <- resource_df[choice_input, , drop = FALSE] id_choice <- resource_df$id[choice_input] - message("To directly access this resource in the future please use this command:\n", - glue::glue("bcdc_get_data('{record_id}', resource = '{id_choice}')"),"\n") + message( + "To directly access this resource in the future please use this command:\n", + glue::glue("bcdc_get_data('{record_id}', resource = '{id_choice}')"), + "\n" + ) read_from_url(resource, ...) } # nocov end @@ -257,21 +290,46 @@ bcdc_get_data.bcdc_record <- function(record, resource = NULL, verbose = TRUE, . #' @export #' - -bcdc_read_functions <- function(){ +bcdc_read_functions <- function() { dplyr::tribble( - ~format, ~package, ~fun, - "kml", "sf", "read_sf", - "geojson", "sf", "read_sf", - "gpkg", "sf", "read_sf", - "gdb", "sf", "read_sf", - "fgdb", "sf", "read_sf", - "shp", "sf", "read_sf", - "csv", "readr", "read_csv", - "txt", "readr", "read_tsv", - "tsv", "readr", "read_tsv", - "xlsx", "readxl", "read_xlsx", - "xls", "readxl", "read_xls", - "json", "jsonlite", "read_json" + ~format, + ~package, + ~fun, + "kml", + "sf", + "read_sf", + "geojson", + "sf", + "read_sf", + "gpkg", + "sf", + "read_sf", + "gdb", + "sf", + "read_sf", + "fgdb", + "sf", + "read_sf", + "shp", + "sf", + "read_sf", + "csv", + "readr", + "read_csv", + "txt", + "readr", + "read_tsv", + "tsv", + "readr", + "read_tsv", + "xlsx", + "readxl", + "read_xlsx", + "xls", + "readxl", + "read_xls", + "json", + "jsonlite", + "read_json" ) } diff --git a/R/utils-classes.R b/R/utils-classes.R index f3c89863..f873d54a 100644 --- a/R/utils-classes.R +++ b/R/utils-classes.R @@ -12,23 +12,23 @@ ## Add "bcdc_promise" class as.bcdc_promise <- function(res) { - structure(res, - class = c("bcdc_promise", setdiff(class(res), "bcdc_promise")) - ) + structure(res, class = c("bcdc_promise", setdiff(class(res), "bcdc_promise"))) } as.bcdc_sf <- function(x, query_list, url, full_url) { - structure(x, - class = c("bcdc_sf", setdiff(class(x), "bcdc_sf")), - query_list = query_list, - url = url, full_url = full_url, time_downloaded = Sys.time()) + structure( + x, + class = c("bcdc_sf", setdiff(class(x), "bcdc_sf")), + query_list = query_list, + url = url, + full_url = full_url, + time_downloaded = Sys.time() + ) } as.bcdc_query <- function(x) { - structure(x, - class = c("bcdc_query", setdiff(class(x), "bcdc_query")) - ) + structure(x, class = c("bcdc_query", setdiff(class(x), "bcdc_query"))) } @@ -36,13 +36,12 @@ as.bcdc_query <- function(x) { #' @export print.bcdc_promise <- function(x, ...) { - x$query_list$CQL_FILTER <- finalize_cql(x$query_list$CQL_FILTER) if (is.null(x$query_list$count)) { query_list <- c(x$query_list, count = 6) ## only add if not there. } else { - query_list <- x$query_list + query_list <- x$query_list } cli <- x$cli @@ -64,16 +63,23 @@ print.bcdc_promise <- function(x, ...) { # Check if this was called using a whse name directly without going # through a catalogue record so don't have this info - name <- ifelse(is_record(x$record), - paste0("'", x[["record"]][["name"]], "'"), - paste0("'", x[["query_list"]][["typeNames"]], "'")) + name <- ifelse( + is_record(x$record), + paste0("'", x[["record"]][["name"]], "'"), + paste0("'", x[["query_list"]][["typeNames"]], "'") + ) cat_line_wrap(glue::glue("Querying {col_red(name)} record")) - cat_bullet(strwrap(glue::glue("Using {col_blue('collect()')} on this object will return {col_green(number_of_records)} features ", - "and {col_green(fields)} fields"))) - if (number_of_records > chunk_size) { # this triggers pagination - cat_bullet(strwrap(glue::glue("Accessing this record requires pagination and will make {col_green(ceiling(number_of_records/chunk_size))} separate requests to the WFS. ", - "See ?bcdc_options"))) + cat_bullet(strwrap(glue::glue( + "Using {col_blue('collect()')} on this object will return {col_green(number_of_records)} features ", + "and {col_green(fields)} fields" + ))) + if (number_of_records > chunk_size) { + # this triggers pagination + cat_bullet(strwrap(glue::glue( + "Accessing this record requires pagination and will make {col_green(ceiling(number_of_records/chunk_size))} separate requests to the WFS. ", + "See ?bcdc_options" + ))) } cat_bullet(strwrap("At most six rows of the record are printed here")) @@ -84,24 +90,53 @@ print.bcdc_promise <- function(x, ...) { #' @export print.bcdc_record <- function(x, ...) { - cat_line_wrap(cli::col_blue(cli::style_bold("B.C. Data Catalogue Record: ")), x$title) - cat_line_wrap(cli::col_blue(cli::style_italic("Name: ")), x$name, " (ID: ", x$id, ")") - cat_line_wrap(cli::col_blue(cli::style_italic("Permalink: ")), paste0(catalogue_base_url(), "dataset/", x$id)) + cat_line_wrap( + cli::col_blue(cli::style_bold("B.C. Data Catalogue Record: ")), + x$title + ) + cat_line_wrap( + cli::col_blue(cli::style_italic("Name: ")), + x$name, + " (ID: ", + x$id, + ")" + ) + cat_line_wrap( + cli::col_blue(cli::style_italic("Permalink: ")), + paste0(catalogue_base_url(), "dataset/", x$id) + ) cat_line_wrap(cli::col_blue(cli::style_italic("Licence: ")), x$license_title) cat_line_wrap(cli::col_blue(cli::style_italic("Description: ")), x$notes) - tidy_resources <- bcdc_tidy_resources(x) avail_res <- tidy_resources[tidy_resources$bcdata_available, , drop = FALSE] - cat_line_wrap(cli::col_blue(cli::style_italic("Available Resources (", nrow(avail_res), "):"))) - cli::cat_line(" ", seq_len(nrow(avail_res)), ". ", avail_res$name, " (", avail_res$format, ")") + cat_line_wrap(cli::col_blue(cli::style_italic( + "Available Resources (", + nrow(avail_res), + "):" + ))) + cli::cat_line( + " ", + seq_len(nrow(avail_res)), + ". ", + avail_res$name, + " (", + avail_res$format, + ")" + ) - cat_line_wrap(cli::col_blue(cli::style_italic("Access the full 'Resources' data frame using: ")), - cli::col_red("bcdc_tidy_resources('", x$id, "')")) + cat_line_wrap( + cli::col_blue(cli::style_italic( + "Access the full 'Resources' data frame using: " + )), + cli::col_red("bcdc_tidy_resources('", x$id, "')") + ) if ("wms" %in% formats_from_record(x)) { - cat_line_wrap(cli::col_blue(cli::style_italic("Query and filter this data using: ")), - cli::col_red("bcdc_query_geodata('", x$id, "')")) + cat_line_wrap( + cli::col_blue(cli::style_italic("Query and filter this data using: ")), + cli::col_red("bcdc_query_geodata('", x$id, "')") + ) } invisible(x) @@ -114,26 +149,36 @@ record_print_helper <- function(r, n, print_avail = FALSE) { if (r$format != "wms") cat_line_wrap("url: ", r$url, indent = 3) cat_line_wrap("resource: ", r$id, indent = 3) if (print_avail) { - cat_line_wrap("available in R via bcdata: ", - if (r$format == "zip") { - "Will attempt - unknown format (zipped)" - } else { - r$bcdata_available - }) + cat_line_wrap( + "available in R via bcdata: ", + if (r$format == "zip") { + "Will attempt - unknown format (zipped)" + } else { + r$bcdata_available + } + ) } if (r$bcdata_available) - cat_line_wrap("code: ", "bcdc_get_data(record = '", r$package_id, - "', resource = '",r$id,"')", indent = 3) + cat_line_wrap( + "code: ", + "bcdc_get_data(record = '", + r$package_id, + "', resource = '", + r$id, + "')", + indent = 3 + ) cat_line() } #' @export print.bcdc_recordlist <- function(x, ...) { - len <- length(x) if (len == 0L) { - cat_line_wrap("Your search returned no results. Please try a different query.") + cat_line_wrap( + "Your search returned no results. Please try a different query." + ) return(x) } @@ -141,46 +186,64 @@ print.bcdc_recordlist <- function(x, ...) { n_print <- min(50, len) cat_line_wrap(cli::col_blue("Number of records: ", len)) if (n_print < len) { - cat_line_wrap(cli::col_blue("Showing the top 50 results. You can assign the output of bcdc_search, to an object and subset with `[` to see other results in the set.")) - cat_line("") - } + cat_line_wrap(cli::col_blue( + "Showing the top 50 results. You can assign the output of bcdc_search, to an object and subset with `[` to see other results in the set." + )) + cat_line("") + } cat_line_wrap("Titles:") x <- purrr::set_names(x, NULL) - purrr::imap(unclass(x)[1:n_print], ~ { - - if (!nrow(bcdc_tidy_resources(x[[.y]]))) { - cat_line_wrap(.y, ": ",purrr::pluck(.x, "title")) - cat_line_wrap("This record has no resources. bcdata will not be able to access any data.", col = "red") - } else { - cat_line_wrap(.y, ": ",purrr::pluck(.x, "title"), - " (", paste0(unique(formats_from_record(.x)), collapse = ", "), - ")") + purrr::imap( + unclass(x)[1:n_print], + ~ { + if (!nrow(bcdc_tidy_resources(x[[.y]]))) { + cat_line_wrap(.y, ": ", purrr::pluck(.x, "title")) + cat_line_wrap( + "This record has no resources. bcdata will not be able to access any data.", + col = "red" + ) + } else { + cat_line_wrap( + .y, + ": ", + purrr::pluck(.x, "title"), + " (", + paste0(unique(formats_from_record(.x)), collapse = ", "), + ")" + ) + } + + cat_line_wrap("ID: ", purrr::pluck(.x, "id"), indent = 1, exdent = 2) + cat_line_wrap("Name: ", purrr::pluck(.x, "name"), indent = 1, exdent = 2) } - - cat_line_wrap("ID: ", purrr::pluck(.x, "id"), indent = 1, exdent = 2) - cat_line_wrap("Name: ", purrr::pluck(.x, "name"), indent = 1, exdent = 2) - }) + ) cat_line() - cat_line_wrap("Access a single record by calling `bcdc_get_record(ID)` - with the ID from the desired record.") + cat_line_wrap( + "Access a single record by calling `bcdc_get_record(ID)` + with the ID from the desired record." + ) invisible(x) } #' @export print.bcdc_group <- function(x, ...) { + cat_line_wrap( + cli::col_blue( + cli::style_italic( + "Group Description: " + ) + ), + unique(attr(x, "description")) + ) - cat_line_wrap(cli::col_blue( - cli::style_italic( - "Group Description: " - ) - ), unique(attr(x, "description"))) - - - - cat_line_wrap(cli::col_blue( - cli::style_italic("Number of datasets: ")), nrow(x)) + cat_line_wrap( + cli::col_blue( + cli::style_italic("Number of datasets: ") + ), + nrow(x) + ) print(tibble::as_tibble(x)) } @@ -188,7 +251,6 @@ print.bcdc_group <- function(x, ...) { #' @export print.bcdc_query <- function(x, ...) { - cat_line("") if (length(x$url) > 1) { for (i in seq_along(x$url)) { @@ -207,7 +269,6 @@ print.bcdc_query <- function(x, ...) { # dplyr methods ----------------------------------------------------------- - #' Filter a query from bcdc_query_geodata() #' #' Filter a query from Web Feature Service using dplyr @@ -260,20 +321,28 @@ print.bcdc_query <- function(x, ...) { #' } #' @export filter.bcdc_promise <- function(.data, ...) { - - current_cql = cql_translate(..., .colnames = .data$cols_df$col_name %||% character(0)) + current_cql = cql_translate( + ..., + .colnames = .data$cols_df$col_name %||% character(0) + ) ## Change CQL query on the fly if geom is not GEOMETRY current_cql = specify_geom_name(.data$cols_df, current_cql) # Add cql filter statement to any existing cql filter statements. # ensure .data$query_list$CQL_FILTER is class sql even if NULL, so # dispatches on sql class and dbplyr::c.sql method is used - .data$query_list$CQL_FILTER <- c(dbplyr::sql(.data$query_list$CQL_FILTER), - current_cql, - drop_null = TRUE) + .data$query_list$CQL_FILTER <- c( + dbplyr::sql(.data$query_list$CQL_FILTER), + current_cql, + drop_null = TRUE + ) - as.bcdc_promise(list(query_list = .data$query_list, cli = .data$cli, - record = .data$record, cols_df = .data$cols_df)) + as.bcdc_promise(list( + query_list = .data$query_list, + cli = .data$cli, + record = .data$record, + cols_df = .data$cols_df + )) } #' Select columns from bcdc_query_geodata() call @@ -315,8 +384,7 @@ filter.bcdc_promise <- function(.data, ...) { #' #' #'@export -select.bcdc_promise <- function(.data, ...){ - +select.bcdc_promise <- function(.data, ...) { ## Eventually have to migrate to tidyselect::eval_select ## https://community.rstudio.com/t/evaluating-using-rlang-when-supplying-a-vector/44693/10 cols_to_select <- tidyselect::vars_select(.data$cols_df$col_name, ...) @@ -324,13 +392,20 @@ select.bcdc_promise <- function(.data, ...){ ## id is always added in. web request doesn't like asking for it twice cols_to_select <- remove_id_col(cols_to_select) ## Always add back in the geom - cols_to_select <- paste(geom_col_name(.data$cols_df), paste0(cols_to_select, collapse = ","), sep = ",") + cols_to_select <- paste( + geom_col_name(.data$cols_df), + paste0(cols_to_select, collapse = ","), + sep = "," + ) query_list <- c(.data$query_list, propertyName = cols_to_select) - as.bcdc_promise(list(query_list = query_list, cli = .data$cli, - record = .data$record, cols_df = .data$cols_df)) - + as.bcdc_promise(list( + query_list = query_list, + cli = .data$cli, + record = .data$record, + cols_df = .data$cols_df + )) } #' @importFrom utils head @@ -376,7 +451,6 @@ names.bcdc_promise <- function(x) { geom_idx <- which(cols == "geometry") cols[c(seq_along(cols)[-geom_idx], geom_idx)] - } @@ -400,12 +474,16 @@ names.bcdc_promise <- function(x) { #' } #' #'@export -mutate.bcdc_promise <- function(.data, ...){ +mutate.bcdc_promise <- function(.data, ...) { dots <- rlang::exprs(...) - stop(glue::glue( - "You must type collect() before using mutate() on a WFS. \nAfter using collect() add this mutate call:: - mutate({dots}) "), call. = FALSE) + stop( + glue::glue( + "You must type collect() before using mutate() on a WFS. \nAfter using collect() add this mutate call:: + mutate({dots}) " + ), + call. = FALSE + ) } @@ -433,8 +511,7 @@ mutate.bcdc_promise <- function(.data, ...){ #' ) #' } #' -collect.bcdc_promise <- function(x, ...){ - +collect.bcdc_promise <- function(x, ...) { x$query_list$CQL_FILTER <- finalize_cql(x$query_list$CQL_FILTER) query_list <- x$query_list @@ -445,16 +522,24 @@ collect.bcdc_promise <- function(x, ...){ chunk_size <- check_chunk_limit() if (number_of_records <= chunk_size) { - cc <- tryCatch(cli$post(body = query_list, encode = "form"), - error = function(e) { - stop("There was an issue processing this request. - Try reducing the size of the object you are trying to retrieve.", call. = FALSE)}) + cc <- tryCatch( + cli$post(body = query_list, encode = "form"), + error = function(e) { + stop( + "There was an issue processing this request. + Try reducing the size of the object you are trying to retrieve.", + call. = FALSE + ) + } + ) catch_wfs_error(cc) url <- cc$url full_url <- cli$url_fetch(query = query_list) } else { - message(glue::glue("This object has {number_of_records} records and requires {ceiling(number_of_records/chunk_size)} paginated requests to complete.")) + message(glue::glue( + "This object has {number_of_records} records and requires {ceiling(number_of_records/chunk_size)} paginated requests to complete." + )) sorting_col <- pagination_sort_col(x$cols_df) query_list <- c(query_list, sortby = sorting_col) @@ -472,10 +557,13 @@ collect.bcdc_promise <- function(x, ...){ message("Retrieving data") - tryCatch(cc$post(body = query_list, encode = "form"), - error = function(e) { - stop("There was an issue processing this request. - Try reducing the size of the object you are trying to retrieve.", call. = FALSE)}) + tryCatch(cc$post(body = query_list, encode = "form"), error = function(e) { + stop( + "There was an issue processing this request. + Try reducing the size of the object you are trying to retrieve.", + call. = FALSE + ) + }) url <- cc$url full_url <- cc$url_fetch(query = query_list) @@ -485,8 +573,12 @@ collect.bcdc_promise <- function(x, ...){ txt <- cc$parse("UTF-8") - as.bcdc_sf(bcdc_read_sf(txt), query_list = query_list, url = url, - full_url = full_url) + as.bcdc_sf( + bcdc_read_sf(txt), + query_list = query_list, + url = url, + full_url = full_url + ) } @@ -514,7 +606,6 @@ as_tibble.bcdc_promise <- collect.bcdc_promise #' } #' show_query.bcdc_promise <- function(x, ...) { - y <- list() y$base_url <- x$cli$url y$query_list <- x$query_list @@ -522,11 +613,9 @@ show_query.bcdc_promise <- function(x, ...) { y$full_url <- x$cli$url_fetch(query = y$query_list) as.bcdc_query(y) - } - #' @describeIn show_query show_query.bcdc_promise #' #' @export @@ -542,7 +631,6 @@ show_query.bcdc_promise <- function(x, ...) { #' ) #' } show_query.bcdc_sf <- function(x, ...) { - y <- list() y$url <- attr(x, "url") y$query_list <- attr(x, "query_list") @@ -557,7 +645,14 @@ finalize_cql <- function(x, con = wfs_con) { dbplyr::sql_vector(x, collapse = " AND ", con = con) } -cat_line_wrap <- function(..., indent = 0, exdent = 1, col = NULL, background_col = NULL, file = stdout()) { +cat_line_wrap <- function( + ..., + indent = 0, + exdent = 1, + col = NULL, + background_col = NULL, + file = stdout() +) { txt <- strwrap(paste0(..., collapse = ""), indent = indent, exdent = exdent) cat_line(txt, col = col, background_col = background_col, file = file) } diff --git a/R/utils-filter.R b/R/utils-filter.R index 0594999c..569fa5c1 100644 --- a/R/utils-filter.R +++ b/R/utils-filter.R @@ -1,11 +1,11 @@ # Copyright 2019 Province of British Columbia -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. diff --git a/R/utils-is.R b/R/utils-is.R index 36e21af9..876e0dc7 100644 --- a/R/utils-is.R +++ b/R/utils-is.R @@ -20,7 +20,6 @@ is_emptyish <- function(x) { is_whse_object_name <- function(x) { - ## detect object is a record and then just return FALSE if (inherits(x, "bcdc_record")) { return(FALSE) diff --git a/R/utils-select.R b/R/utils-select.R index eff3a7c8..a0da9069 100644 --- a/R/utils-select.R +++ b/R/utils-select.R @@ -1,11 +1,11 @@ # Copyright 2019 Province of British Columbia -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. diff --git a/R/utils.R b/R/utils.R index e8098881..32c5d0c3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -11,13 +11,17 @@ # See the License for the specific language governing permissions and limitations under the License. catalogue_base_url <- function() { - getOption("bcdata.catalogue_gui_url", - default = "https://catalogue.data.gov.bc.ca/") + getOption( + "bcdata.catalogue_gui_url", + default = "https://catalogue.data.gov.bc.ca/" + ) } catalogue_base_api_url <- function() { - getOption("bcdata.catalogue_api_url", - default = "https://catalogue.data.gov.bc.ca/api/3") + getOption( + "bcdata.catalogue_api_url", + default = "https://catalogue.data.gov.bc.ca/api/3" + ) } wfs_base_url <- function(host = bcdc_web_service_host()) { @@ -32,7 +36,7 @@ bcdc_web_service_host <- function() { getOption("bcdata.web_service_host", default = "https://openmaps.gov.bc.ca") } -bcdata_user_agent <- function(){ +bcdata_user_agent <- function() { "https://github.com/bcgov/bcdata" } @@ -56,13 +60,12 @@ make_url <- function(..., trailing_slash = FALSE) { url } -bcdc_number_wfs_records <- function(query_list, client){ - +bcdc_number_wfs_records <- function(query_list, client) { if (!is.null(query_list$count)) { return(query_list$count) } - if(!is.null(query_list$propertyName)){ + if (!is.null(query_list$propertyName)) { query_list$propertyName <- NULL } @@ -74,10 +77,9 @@ bcdc_number_wfs_records <- function(query_list, client){ ## resultType is only returned as XML. ## regex to extract the number as.numeric(sub(".*numberMatched=\"([0-9]{1,20})\".*", "\\1", txt_max)) - } -specify_geom_name <- function(cols_df, CQL_statement){ +specify_geom_name <- function(cols_df, CQL_statement) { # Find the geometry field and get the name of the field geom_col <- geom_col_name(cols_df) @@ -85,25 +87,21 @@ specify_geom_name <- function(cols_df, CQL_statement){ dbplyr::sql(glue::glue(CQL_statement, geom_name = geom_col)) } -bcdc_read_sf <- function(x, ...){ - - if(length(x) == 1){ - +bcdc_read_sf <- function(x, ...) { + if (length(x) == 1) { return(sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...)) - - } else{ + } else { # tests that cover this are skipped due to large size # nocov start ## Parse the Paginated response message("Parsing data") - sf_responses <- lapply(x, function(x) {sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...)}) + sf_responses <- lapply(x, function(x) { + sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...) + }) do.call(rbind, sf_responses) # nocov end } - - - } slug_from_url <- function(x) { @@ -111,7 +109,7 @@ slug_from_url <- function(x) { x } -formats_supported <- function(){ +formats_supported <- function() { c(bcdc_read_functions()[["format"]], "zip") } @@ -161,26 +159,27 @@ geom_col_name <- function(x) { x[x$remote_col_type == geom_type, , drop = FALSE]$col_name } -remove_id_col <- function(x){ -setdiff(x, "id") +remove_id_col <- function(x) { + setdiff(x, "id") } #' @param x a resource_df from formatted record #' @noRd wfs_available <- function(x) { - x$location %in% c("bcgwdatastore", "bcgeographicwarehouse") & + x$location %in% + c("bcgwdatastore", "bcgeographicwarehouse") & x$format == "wms" } #' @param x a resource_df from formatted record #' @noRd other_format_available <- function(x) { - x$ext %in% formats_supported() & + x$ext %in% + formats_supported() & !x$location %in% c("bcgwdatastore", "bcgeographicwarehouse") } -wfs_to_r_col_type <- function(col){ - +wfs_to_r_col_type <- function(col) { dplyr::case_when( col == "xsd:string" ~ "character", col == "xsd:date" ~ "date", @@ -192,21 +191,20 @@ wfs_to_r_col_type <- function(col){ } ##from a record -formats_from_record <- function(x, trim = TRUE){ - +formats_from_record <- function(x, trim = TRUE) { resource_df <- dplyr::tibble( - name = purrr::map_chr(x$resources, "name"), - url = purrr::map_chr(x$resources, safe_file_ext), - format = purrr::map_chr(x$resources, "format") - ) + name = purrr::map_chr(x$resources, "name"), + url = purrr::map_chr(x$resources, safe_file_ext), + format = purrr::map_chr(x$resources, "format") + ) x <- formats_from_resource(resource_df) - if(trim) return(x[x != ""]) + if (trim) return(x[x != ""]) x } -formats_from_resource <- function(x){ +formats_from_resource <- function(x) { dplyr::case_when( x$format == x$url ~ x$format, x$format == "wms" ~ "wms", @@ -246,8 +244,7 @@ get_record_warn_once <- function(...) { } - -clean_wfs <- function(x){ +clean_wfs <- function(x) { dplyr::case_when( x == "WMS getCapabilities request" ~ "WFS request (Spatial Data)", x == "wms" ~ "wfs", @@ -255,19 +252,26 @@ clean_wfs <- function(x){ ) } -read_from_url <- function(resource, ...){ - if (nrow(resource) > 1) stop("more than one resource specified", call. = FALSE) +read_from_url <- function(resource, ...) { + if (nrow(resource) > 1) + stop("more than one resource specified", call. = FALSE) file_url <- resource$url reported_format <- safe_file_ext(resource) if (!reported_format %in% formats_supported()) { - stop("Reading ", reported_format, " files is not currently supported in bcdata.") + stop( + "Reading ", + reported_format, + " files is not currently supported in bcdata." + ) } auth <- grepl("(cat(alogue)?|pub)\\.data\\.gov\\.bc\\.ca", file_url) cli <- bcdc_http_client(file_url, auth = auth) ## Establish where to download file - tmp <- tempfile(tmpdir = unique_temp_dir(), - fileext = paste0(".", tools::file_ext(file_url))) + tmp <- tempfile( + tmpdir = unique_temp_dir(), + fileext = paste0(".", tools::file_ext(file_url)) + ) on.exit(unlink(tmp)) r <- cli$get(disk = tmp) @@ -283,23 +287,32 @@ read_from_url <- function(resource, ...){ # This assumes that the function we are using to read the data takes the # data as the first argument - will need revisiting if we find a situation # where that's not the case - message("Reading the data using the ", fun$fun, " function from the ", - fun$package, " package.") + message( + "Reading the data using the ", + fun$fun, + " function from the ", + fun$package, + " package." + ) handle_excel(tmp, ...) -tryCatch( - do.call(fun$fun, list(tmp, ...)), - error = function(e) { - stop("Reading the data set failed with the following error message:\n\n ", e, - "\nThe file can be found here:\n '", - tmp, "'\nif you would like to try to read it manually.\n", - call. = FALSE) - } -) + tryCatch( + do.call(fun$fun, list(tmp, ...)), + error = function(e) { + stop( + "Reading the data set failed with the following error message:\n\n ", + e, + "\nThe file can be found here:\n '", + tmp, + "'\nif you would like to try to read it manually.\n", + call. = FALSE + ) + } + ) } -resource_to_tibble <- function(x){ +resource_to_tibble <- function(x) { res_df <- dplyr::tibble( name = safe_map_chr(x, "name"), url = safe_map_chr(x, "url"), @@ -309,11 +322,13 @@ resource_to_tibble <- function(x){ package_id = safe_map_chr(x, "package_id"), location = simplify_string(safe_map_chr(x, "resource_storage_location")) ) - - dplyr::mutate(res_df, + + dplyr::mutate( + res_df, wfs_available = wfs_available(res_df), - bcdata_available = wfs_available | other_format_available(res_df)) - } + bcdata_available = wfs_available | other_format_available(res_df) + ) +} #' @importFrom rlang "%||%" safe_map_chr <- function(x, name) { @@ -355,15 +370,15 @@ handle_zip <- function(x) { files <- list_supported_files(dir) # check if it's a shapefile -if (length(files) > 1L) { - stop( - "More than one supported file in zip file. It has been downloaded and ", - "extracted to '", - dir, - "', where you can access its contents manually.", - call. = FALSE - ) -} + if (length(files) > 1L) { + stop( + "More than one supported file in zip file. It has been downloaded and ", + "extracted to '", + dir, + "', where you can access its contents manually.", + call. = FALSE + ) + } files } @@ -375,10 +390,18 @@ handle_excel <- function(tmp, ...) { sheets <- readxl::excel_sheets(tmp) if (length(sheets) > 1L) { - message(paste0("\nThis .", tools::file_ext(tmp), " resource contains the following sheets: \n", - paste0(" '", sheets,"'", collapse = "\n"))) + message(paste0( + "\nThis .", + tools::file_ext(tmp), + " resource contains the following sheets: \n", + paste0(" '", sheets, "'", collapse = "\n") + )) if (!methods::hasArg("sheet")) { - message("Defaulting to the '", sheets[1], "' sheet. See ?bcdc_get_data for examples on how to specify a sheet.\n") + message( + "Defaulting to the '", + sheets[1], + "' sheet. See ?bcdc_get_data for examples on how to specify a sheet.\n" + ) } } } @@ -399,8 +422,8 @@ list_supported_files <- function(dir) { if (!length(list.dirs(dir, recursive = FALSE))) { stop("No supported files found", call. = FALSE) } - files <- list.files(dir, full.names = TRUE, recursive = TRUE) - supported <- is_filetype(files, formats_supported()) + files <- list.files(dir, full.names = TRUE, recursive = TRUE) + supported <- is_filetype(files, formats_supported()) } files[supported] @@ -424,24 +447,35 @@ catch_wfs_error <- function(catalogue_response) { msg <- paste0( msg, - cli::rule(line = "bar4", line_col = 'red'),"\n", + cli::rule(line = "bar4", line_col = 'red'), + "\n", "Request:", - "\n URL: ", catalogue_response$request$url$url, - "\n POST fields:\n ", rawToChar(catalogue_response$request$options$postfields), + "\n URL: ", + catalogue_response$request$url$url, + "\n POST fields:\n ", + rawToChar(catalogue_response$request$options$postfields), "\n" ) for (i in seq_along(request_res)) { msg <- paste0( - msg, " ", names(request_res)[i], ": ", - request_res[i], "\n" + msg, + " ", + names(request_res)[i], + ": ", + request_res[i], + "\n" ) } msg <- paste0(msg, "Response:\n") for (i in seq_along(response_res)) { msg <- paste0( - msg, " ", names(response_res)[i], ": ", - response_res[i], "\n" + msg, + " ", + names(response_res)[i], + ": ", + response_res[i], + "\n" ) } } diff --git a/air.toml b/air.toml new file mode 100644 index 00000000..e69de29b diff --git a/bcdata.Rproj b/bcdata.Rproj index cba1b6b7..1fb18070 100644 --- a/bcdata.Rproj +++ b/bcdata.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: eaf93104-2d1c-45fb-bc62-d9d0a06f4907 RestoreWorkspace: No SaveWorkspace: No diff --git a/inst/sticker/make_sticker.R b/inst/sticker/make_sticker.R index 632e9035..2ac3c0d3 100644 --- a/inst/sticker/make_sticker.R +++ b/inst/sticker/make_sticker.R @@ -40,7 +40,6 @@ bc <- bc_bound() %>% ms_simplify(keep = .1) # theme_transparent() + # coord_sf(datum = NULL) - ## schools schools <- bcdc_query_geodata("schools-k-12-with-francophone-indicators") %>% collect() @@ -66,9 +65,10 @@ p <- ggplot() + coord_sf(datum = NULL) ## fonts -font_path <- switch (Sys.info()['sysname'], - Darwin = "/Library/Fonts/Microsoft/Century Gothic", - Windows = "C:/WINDOWS/FONTS/GOTHIC.TTF" +font_path <- switch( + Sys.info()['sysname'], + Darwin = "/Library/Fonts/Microsoft/Century Gothic", + Windows = "C:/WINDOWS/FONTS/GOTHIC.TTF" ) sysfonts::font_add("Century Gothic", font_path) @@ -76,13 +76,21 @@ sysfonts::font_add("Century Gothic", font_path) ## hex sticker write_sticker <- function(p, format) { - sticker(p, package = "bcdata", - p_size = 5, # This seems to behave very differently on a Mac vs PC - p_y = 1.6, p_color = "#F6A97A", p_family = "Century Gothic", - s_x = 1, s_y = .9, - s_width = 1.5, s_height = 1.5, - h_fill = "#29303a", h_color = "#F6A97A", - filename = file.path(paste0("inst/sticker/bcdata.", format))) + sticker( + p, + package = "bcdata", + p_size = 5, # This seems to behave very differently on a Mac vs PC + p_y = 1.6, + p_color = "#F6A97A", + p_family = "Century Gothic", + s_x = 1, + s_y = .9, + s_width = 1.5, + s_height = 1.5, + h_fill = "#29303a", + h_color = "#F6A97A", + filename = file.path(paste0("inst/sticker/bcdata.", format)) + ) } write_sticker(p, "png") @@ -90,4 +98,3 @@ write_sticker(p, "svg") # Run: usethis::use_logo("inst/sticker/bcdata.png") - diff --git a/scratch/multipoint.R b/scratch/multipoint.R index 10d937bb..e14139de 100644 --- a/scratch/multipoint.R +++ b/scratch/multipoint.R @@ -20,16 +20,18 @@ mp2 # inner parentheses added # Issue a request to geoserver using the multipoint without parentheses around # points -a <- GET("https://openmaps.gov.bc.ca/geo/pub/wfs", - query = list( - SERVICE = "WFS", - VERSION = "2.0.0", - REQUEST = "GetFeature", - outputFormat = "application/json", - typeNames = "WHSE_BASEMAPPING.GBA_LOCAL_REG_GREENSPACES_SP", - SRSNAME = "EPSG:3005", - CQL_FILTER = paste0("INTERSECTS(SHAPE, ", mp, ")") - )) +a <- GET( + "https://openmaps.gov.bc.ca/geo/pub/wfs", + query = list( + SERVICE = "WFS", + VERSION = "2.0.0", + REQUEST = "GetFeature", + outputFormat = "application/json", + typeNames = "WHSE_BASEMAPPING.GBA_LOCAL_REG_GREENSPACES_SP", + SRSNAME = "EPSG:3005", + CQL_FILTER = paste0("INTERSECTS(SHAPE, ", mp, ")") + ) +) URLdecode(a$url) a$status_code @@ -37,16 +39,18 @@ content(a, as = "text") # Issue a request to geoserver using the multipoint *with* parentheses around # points -b <- httr::GET("https://openmaps.gov.bc.ca/geo/pub/wfs", - query = list( - SERVICE = "WFS", - VERSION = "2.0.0", - REQUEST = "GetFeature", - outputFormat = "application/json", - typeNames = "WHSE_BASEMAPPING.GBA_LOCAL_REG_GREENSPACES_SP", - SRSNAME = "EPSG:3005", - CQL_FILTER = paste0("INTERSECTS(SHAPE, ", mp2, ")") - )) +b <- httr::GET( + "https://openmaps.gov.bc.ca/geo/pub/wfs", + query = list( + SERVICE = "WFS", + VERSION = "2.0.0", + REQUEST = "GetFeature", + outputFormat = "application/json", + typeNames = "WHSE_BASEMAPPING.GBA_LOCAL_REG_GREENSPACES_SP", + SRSNAME = "EPSG:3005", + CQL_FILTER = paste0("INTERSECTS(SHAPE, ", mp2, ")") + ) +) URLdecode(b$url) b$status_code diff --git a/scratch/scratch.R b/scratch/scratch.R index bc48c06e..ac68cf17 100644 --- a/scratch/scratch.R +++ b/scratch/scratch.R @@ -1,4 +1,3 @@ - foo <- bcdc_search("forest", res_format = "wms") bcdc_search_facets("type") @@ -15,7 +14,11 @@ rd <- bcdc_map("tantalis-regional-districts") hyd <- bcdc_map("hydrology-hydrometric-watershed-boundaries") -obs_wells <- bcdc_map("ground-water-wells", - query = "OBSERVATION_WELL_NUMBER IS NOT NULL") -obs_wells <- bcdc_map("ground-water-wells", - query = "OBSERVATION_WELL_NUMBER=108") +obs_wells <- bcdc_map( + "ground-water-wells", + query = "OBSERVATION_WELL_NUMBER IS NOT NULL" +) +obs_wells <- bcdc_map( + "ground-water-wells", + query = "OBSERVATION_WELL_NUMBER=108" +) diff --git a/scratch/scratch_dem.R b/scratch/scratch_dem.R index eb2272a5..93a9c7c1 100644 --- a/scratch/scratch_dem.R +++ b/scratch/scratch_dem.R @@ -10,25 +10,22 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - - bcdc_get_dem <- function(bbox) { - if (inherits(bbox, "bbox")) { bbox <- paste(bbox, collapse = ",") } query_list <- list( - service="WCS", - version="1.0.0", - request="GetCoverage", - coverage="pub:bc_elevation_25m_bcalb", - bbox=bbox, - CRS="EPSG:3005", + service = "WCS", + version = "1.0.0", + request = "GetCoverage", + coverage = "pub:bc_elevation_25m_bcalb", + bbox = bbox, + CRS = "EPSG:3005", RESPONSE_CRS = "EPSG:3005", - Format="GeoTIFF", - resx=25, - resy=25 + Format = "GeoTIFF", + resx = 25, + resy = 25 ) ## Drop any NULLS from the list @@ -37,8 +34,6 @@ bcdc_get_dem <- function(bbox) { ## GET and parse data to sf object cli <- bcdc_http_client(url = "https://delivery.openmaps.gov.bc.ca/om/wcs") - - tiff_file <- tempfile(fileext = ".tif") res <- cli$get(query = query_list, disk = tiff_file) close(file(tiff_file)) diff --git a/scratch/scratch_test_iterating.R b/scratch/scratch_test_iterating.R index 2dca9340..bf651c7a 100644 --- a/scratch/scratch_test_iterating.R +++ b/scratch/scratch_test_iterating.R @@ -4,20 +4,26 @@ for (nme in c("mean", "median", "max", "min")) { } - - -single_arg_functions <- c("EQUALS","DISJOINT","INTERSECTS", - "TOUCHES", "CROSSES", "WITHIN", - "CONTAINS", "OVERLAPS") - -for(nme in single_arg_functions[6]){ +single_arg_functions <- c( + "EQUALS", + "DISJOINT", + "INTERSECTS", + "TOUCHES", + "CROSSES", + "WITHIN", + "CONTAINS", + "OVERLAPS" +) + +for (nme in single_arg_functions[6]) { fun <- match.fun(nme) - local <- bcdc_query_geodata("regional-districts-legally-defined-administrative-areas-of-bc") %>% + local <- bcdc_query_geodata( + "regional-districts-legally-defined-administrative-areas-of-bc" + ) %>% filter(ADMIN_AREA_NAME == "Cariboo Regional District") %>% collect() - remote <- bcdc_query_geodata("bc-airports") %>% filter(fun(local)) %>% collect() diff --git a/tests/testthat/helper-bcdata.R b/tests/testthat/helper-bcdata.R index a2daf956..f1d0449a 100644 --- a/tests/testthat/helper-bcdata.R +++ b/tests/testthat/helper-bcdata.R @@ -22,4 +22,4 @@ skip_if_no_capabilities <- function() { return() } testthat::skip("GetCapabilities request is broken") -} \ No newline at end of file +} diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index d0cc519f..3e1158d1 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -24,7 +24,7 @@ options("silence_named_get_record_warning" = TRUE) # options("bcdata.catalogue_gui_url" = "https://beta-catalogue.data.gov.bc.ca/") point_record <- '76b1b7a3-2112-4444-857a-afccf7b20da8' -point_resource <- "4d0377d9-e8a1-429b-824f-0ce8f363512c" +point_resource <- "4d0377d9-e8a1-429b-824f-0ce8f363512c" polygon_record <- 'd1aff64e-dbfe-45a6-af97-582b7f6418b9' lines_record <- '92344413-8035-4c08-b996-65a9b3f62fca' bcgw_point_record <- 'WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW' diff --git a/tests/testthat/test-bcdc-get-citation.R b/tests/testthat/test-bcdc-get-citation.R index b6caa089..52434c61 100644 --- a/tests/testthat/test-bcdc-get-citation.R +++ b/tests/testthat/test-bcdc-get-citation.R @@ -10,7 +10,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("bcdc_get_citation take a character and returns a bibentry",{ +test_that("bcdc_get_citation take a character and returns a bibentry", { skip_if_net_down() skip_on_cran() rec <- bcdc_get_record(point_record) diff --git a/tests/testthat/test-browse.R b/tests/testthat/test-browse.R index 4668676f..e65f19a0 100644 --- a/tests/testthat/test-browse.R +++ b/tests/testthat/test-browse.R @@ -14,10 +14,14 @@ test_that("bcdc_browse returns the correct url", { skip_if_net_down() skip_on_cran() airports_url <- bcdc_browse("bc-airports") - expect_identical(airports_url, paste0(catalogue_base_url(), "dataset/bc-airports")) + expect_identical( + airports_url, + paste0(catalogue_base_url(), "dataset/bc-airports") + ) catalogue_url <- bcdc_browse() expect_identical(catalogue_url, catalogue_base_url()) - expect_error(bcdc_browse("no-record-here"), - "The specified record does not exist in the catalogue") - + expect_error( + bcdc_browse("no-record-here"), + "The specified record does not exist in the catalogue" + ) }) diff --git a/tests/testthat/test-cql-string.R b/tests/testthat/test-cql-string.R index effeccc5..0383557f 100644 --- a/tests/testthat/test-cql-string.R +++ b/tests/testthat/test-cql-string.R @@ -12,9 +12,9 @@ suppressPackageStartupMessages(library(sf, quietly = TRUE)) -the_geom <- st_sf(st_sfc(st_point(c(1,1)))) +the_geom <- st_sf(st_sfc(st_point(c(1, 1)))) -test_that("bcdc_cql_string fails when an invalid arguments are given",{ +test_that("bcdc_cql_string fails when an invalid arguments are given", { expect_error(bcdc_cql_string(the_geom, "FOO")) expect_error(bcdc_cql_string(quakes, "DWITHIN")) }) @@ -31,14 +31,21 @@ test_that("CQL function works", { }) test_that("All cql geom predicate functions work", { - single_arg_functions <- c("EQUALS","DISJOINT","INTERSECTS", - "TOUCHES", "CROSSES", "WITHIN", - "CONTAINS", "OVERLAPS") + single_arg_functions <- c( + "EQUALS", + "DISJOINT", + "INTERSECTS", + "TOUCHES", + "CROSSES", + "WITHIN", + "CONTAINS", + "OVERLAPS" + ) for (f in single_arg_functions) { expect_equal( do.call(f, list(the_geom)), CQL(paste0(f, "({geom_name}, POINT (1 1))")) - ) + ) } expect_equal( DWITHIN(the_geom, 1), #default units meters @@ -57,15 +64,15 @@ test_that("All cql geom predicate functions work", { CQL("RELATE({geom_name}, POINT (1 1), *********)") ) expect_equal( - BBOX(c(1,2,1,2)), + BBOX(c(1, 2, 1, 2)), CQL("BBOX({geom_name}, 1, 2, 1, 2)") ) expect_equal( - BBOX(c(1,2,1,2), crs = 'EPSG:4326'), + BBOX(c(1, 2, 1, 2), crs = 'EPSG:4326'), CQL("BBOX({geom_name}, 1, 2, 1, 2, 'EPSG:4326')") ) expect_equal( - BBOX(c(1,2,1,2), crs = 4326), + BBOX(c(1, 2, 1, 2), crs = 4326), CQL("BBOX({geom_name}, 1, 2, 1, 2, 'EPSG:4326')") ) }) @@ -78,21 +85,30 @@ test_that("CQL functions fail correctly", { expect_error(RELATE(the_geom, "********"), "pattern") # 8 characters expect_error(RELATE(the_geom, "********5"), "pattern") # invalid character expect_error(RELATE(the_geom, rep("TTTTTTTTT", 2)), "pattern") # > length 1 - expect_error(BBOX(c(1,2,3)), "numeric vector") - expect_error(BBOX(c("1","2","3", "4")), "numeric vector") - expect_error(BBOX(c(1,2,3,4), crs = c("EPSG:4326", "EPSG:3005")), - "must be a character string") + expect_error(BBOX(c(1, 2, 3)), "numeric vector") + expect_error(BBOX(c("1", "2", "3", "4")), "numeric vector") + expect_error( + BBOX(c(1, 2, 3, 4), crs = c("EPSG:4326", "EPSG:3005")), + "must be a character string" + ) }) test_that("unsupported aggregation functions fail correctly", { - expect_error(filter(structure(list(cols_df = list(col_name = "x")), class = "bcdc_promise"), mean(x) > 5), - "not supported by this database") + expect_error( + filter( + structure(list(cols_df = list(col_name = "x")), class = "bcdc_promise"), + mean(x) > 5 + ), + "not supported by this database" + ) }) -test_that("passing an non-existent object to a geom predicate",{ +test_that("passing an non-existent object to a geom predicate", { skip_if_net_down() skip_on_cran() - expect_error(bcdc_query_geodata("6a2fea1b-0cc4-4fc2-8017-eaf755d516da") %>% - filter(INTERSECTS(districts)), - 'not found') + expect_error( + bcdc_query_geodata("6a2fea1b-0cc4-4fc2-8017-eaf755d516da") %>% + filter(INTERSECTS(districts)), + 'not found' + ) }) diff --git a/tests/testthat/test-describe-feature.R b/tests/testthat/test-describe-feature.R index fc22d28b..49198240 100644 --- a/tests/testthat/test-describe-feature.R +++ b/tests/testthat/test-describe-feature.R @@ -10,25 +10,39 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("Test that bcdc_describe feature returns the correct columns",{ +test_that("Test that bcdc_describe feature returns the correct columns", { skip_on_cran() skip_if_net_down() airport_feature <- bcdc_describe_feature("bc-airports") - expect_identical(names(airport_feature), c("col_name", "sticky", "remote_col_type","local_col_type", "column_comments")) + expect_identical( + names(airport_feature), + c( + "col_name", + "sticky", + "remote_col_type", + "local_col_type", + "column_comments" + ) + ) }) test_that("columns are the same as the query", { skip_on_cran() skip_if_net_down() - query <- bcdc_query_geodata("regional-districts-legally-defined-administrative-areas-of-bc") %>% + query <- bcdc_query_geodata( + "regional-districts-legally-defined-administrative-areas-of-bc" + ) %>% filter(ADMIN_AREA_NAME == "Cariboo Regional District") %>% ## just to make the query smaller collect() - description <- bcdc_describe_feature("regional-districts-legally-defined-administrative-areas-of-bc") + description <- bcdc_describe_feature( + "regional-districts-legally-defined-administrative-areas-of-bc" + ) - expect_identical(sort(setdiff(names(query), "geometry")), - sort(setdiff(unique(description$col_name), "SHAPE")) + expect_identical( + sort(setdiff(names(query), "geometry")), + sort(setdiff(unique(description$col_name), "SHAPE")) ) }) @@ -37,15 +51,35 @@ test_that("bcdc_describe_feature accepts a bcdc_record object", { skip_if_net_down() airports <- bcdc_get_record('76b1b7a3-2112-4444-857a-afccf7b20da8') airport_feature <- bcdc_describe_feature(airports) - expect_identical(names(airport_feature), c("col_name", "sticky", "remote_col_type","local_col_type", "column_comments")) + expect_identical( + names(airport_feature), + c( + "col_name", + "sticky", + "remote_col_type", + "local_col_type", + "column_comments" + ) + ) }) -test_that("bcdc_describe_feature accepts BCGW name",{ +test_that("bcdc_describe_feature accepts BCGW name", { skip_on_cran() skip_if_net_down() skip_if_no_capabilities() - airport_feature <- bcdc_describe_feature("WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW") - expect_identical(names(airport_feature), c("col_name", "sticky", "remote_col_type","local_col_type", "column_comments")) + airport_feature <- bcdc_describe_feature( + "WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW" + ) + expect_identical( + names(airport_feature), + c( + "col_name", + "sticky", + "remote_col_type", + "local_col_type", + "column_comments" + ) + ) }) test_that("bcdc_describe_feature fails on unsupported classes", { @@ -59,8 +93,10 @@ test_that("bcdc_describe_feature fails with non-wfs record", { skip_if_net_down() skip_on_cran() skip_if_no_capabilities() - expect_error(bcdc_describe_feature("dba6c78a-1bc1-4d4f-b75c-96b5b0e7fd30"), - "No WFS resource available for this data set") + expect_error( + bcdc_describe_feature("dba6c78a-1bc1-4d4f-b75c-96b5b0e7fd30"), + "No WFS resource available for this data set" + ) }) test_that("bcdc_get_wfs_records works", { diff --git a/tests/testthat/test-edge-cases.R b/tests/testthat/test-edge-cases.R index c87ec52a..d75f9a77 100644 --- a/tests/testthat/test-edge-cases.R +++ b/tests/testthat/test-edge-cases.R @@ -1,7 +1,7 @@ test_that("recods with wms but inconsistent layer_name, object_name fields work", { skip_if_net_down() skip_on_cran() - # https://github.com/bcgov/bcdata/issues/138 + # https://github.com/bcgov/bcdata/issues/138 # layer_name = RSLT_PLANTING_ALL_RSLT_CF # object_name = WHSE_FOREST_VEGETATION.RSLT_PLANTING_SVW # wms uses object_name @@ -11,7 +11,10 @@ test_that("recods with wms but inconsistent layer_name, object_name fields work" # layer_name = WHSE_ADMIN_BOUNDARIES.ADM_NR_DISTRICTS_SPG # wms uses layer_name (generalized) expect_message( - expect_s3_class(bcdc_query_geodata("natural-resource-nr-district"), "bcdc_promise"), + expect_s3_class( + bcdc_query_geodata("natural-resource-nr-district"), + "bcdc_promise" + ), "You are accessing a simplified view of the data" ) }) diff --git a/tests/testthat/test-geom-operators.R b/tests/testthat/test-geom-operators.R index 85ffceb2..023b7056 100644 --- a/tests/testthat/test-geom-operators.R +++ b/tests/testthat/test-geom-operators.R @@ -11,7 +11,9 @@ # See the License for the specific language governing permissions and limitations under the License. if (has_internet() && identical(Sys.getenv("NOT_CRAN"), "true")) { - local <- bcdc_query_geodata("regional-districts-legally-defined-administrative-areas-of-bc") %>% + local <- bcdc_query_geodata( + "regional-districts-legally-defined-administrative-areas-of-bc" + ) %>% filter(ADMIN_AREA_NAME == "Cariboo Regional District") %>% collect() } diff --git a/tests/testthat/test-get-data.R b/tests/testthat/test-get-data.R index 16b2799b..404ba472 100644 --- a/tests/testthat/test-get-data.R +++ b/tests/testthat/test-get-data.R @@ -41,7 +41,9 @@ test_that("bcdc_get_data works with slug and full url with corresponding resourc ) expect_s3_class( ret3 <- bcdc_get_data( - glue::glue("{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8"), + glue::glue( + "{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8" + ), resource = "4d0377d9-e8a1-429b-824f-0ce8f363512c" ), "sf" @@ -55,7 +57,9 @@ test_that("bcdc_get_data works with slug and full url with corresponding resourc ) expect_s3_class( ret5 <- bcdc_get_data( - glue::glue("{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8/resource/4d0377d9-e8a1-429b-824f-0ce8f363512c") + glue::glue( + "{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8/resource/4d0377d9-e8a1-429b-824f-0ce8f363512c" + ) ), "sf" ) @@ -97,7 +101,7 @@ test_that("bcdc_get_data works with an xls when specifying a specific resource", ) }) -test_that("bcdc_get_data will return non-wms resources",{ +test_that("bcdc_get_data will return non-wms resources", { skip_if_net_down() skip_on_cran() expect_s3_class( @@ -141,19 +145,25 @@ test_that("unknown single file (shp) inside zip", { test_that("fails when resource doesn't exist", { skip_if_net_down() skip_on_cran() - expect_error(bcdc_get_data( - "300c0980-b5e3-4202-b0da-d816f14fadad", - resource = "not-a-real-resource" - ), "The specified resource does not exist in this record") + expect_error( + bcdc_get_data( + "300c0980-b5e3-4202-b0da-d816f14fadad", + resource = "not-a-real-resource" + ), + "The specified resource does not exist in this record" + ) }) test_that("fails when multiple files in a zip", { skip_if_net_down() skip_on_cran() - expect_error(bcdc_get_data( - "300c0980-b5e3-4202-b0da-d816f14fadad", - resource = "c212a8a7-c625-4464-b9c8-4527c843f52f" - ), "More than one supported file in zip file") + expect_error( + bcdc_get_data( + "300c0980-b5e3-4202-b0da-d816f14fadad", + resource = "c212a8a7-c625-4464-b9c8-4527c843f52f" + ), + "More than one supported file in zip file" + ) }) test_that("fails informatively when can't read a file", { @@ -165,8 +175,9 @@ test_that("fails informatively when can't read a file", { record = '523dce9d-b464-44a5-b733-2022e94546c3', resource = '4cc98644-f6eb-410b-9df0-f9b2beac9717' ) - ), - "Reading the data set failed with the following error message:") + ), + "Reading the data set failed with the following error message:" + ) }) test_that("bcdc_get_data can return the wms resource when it is specified by resource", { @@ -182,14 +193,14 @@ test_that("bcdc_get_data can return the wms resource when it is specified by res }) -test_that("a wms record with only one resource works with only the record id",{ +test_that("a wms record with only one resource works with only the record id", { skip_if_net_down() skip_on_cran() expect_s3_class( bcdc_get_data("bc-college-region-boundaries"), "sf" ) - }) +}) test_that("bcdc_get_data works with a bcdc_record object", { skip_if_net_down() diff --git a/tests/testthat/test-get_record.R b/tests/testthat/test-get_record.R index 7c94fe5d..4ea08bda 100644 --- a/tests/testthat/test-get_record.R +++ b/tests/testthat/test-get_record.R @@ -25,7 +25,9 @@ test_that("bcdc_get_record works with slug and full url", { ) expect_s3_class( ret3 <- bcdc_get_record( - glue::glue("{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8") + glue::glue( + "{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8" + ) ), "bcdc_record" ) @@ -59,9 +61,14 @@ test_that("bcdc_list_group_records works", { skip_on_cran() skip_if_net_down() - expect_s3_class(bcdc_list_group_records('environmental-reporting-bc'), "bcdc_group") - expect_s3_class(bcdc_list_group_records('environmental-reporting-bc'), "tbl_df") - + expect_s3_class( + bcdc_list_group_records('environmental-reporting-bc'), + "bcdc_group" + ) + expect_s3_class( + bcdc_list_group_records('environmental-reporting-bc'), + "tbl_df" + ) }) test_that("bcdc_list_groups", { @@ -75,9 +82,11 @@ test_that("bcdc_list_organization_records works", { skip_on_cran() skip_if_net_down() - expect_s3_class(bcdc_list_organization_records('bc-stats'), "bcdc_organization") + expect_s3_class( + bcdc_list_organization_records('bc-stats'), + "bcdc_organization" + ) expect_s3_class(bcdc_list_organization_records('bc-stats'), "tbl_df") - }) test_that("bcdc_list_organizations", { @@ -99,32 +108,38 @@ test_that("bcdc_search works", { skip_on_cran() skip_if_net_down() expect_s3_class(bcdc_search("forest"), "bcdc_recordlist") - expect_s3_class(bcdc_search("regional district", res_format = "fgdb"), - "bcdc_recordlist") + expect_s3_class( + bcdc_search("regional district", res_format = "fgdb"), + "bcdc_recordlist" + ) expect_error( bcdc_search(organization = "foo"), "foo is not a valid value for organization" ) }) -test_that("a record with bcgeographicwarehouse AND wms is return by bcdc_get_record",{ +test_that("a record with bcgeographicwarehouse AND wms is return by bcdc_get_record", { skip_on_cran() skip_if_net_down() sr <- bcdc_get_record('95da1091-7e8c-4aa6-9c1b-5ab159ea7b42') d <- sr$resource_df - expect_true(d$bcdata_available[d$location == "bcgeographicwarehouse" & d$format == "wms"]) + expect_true(d$bcdata_available[ + d$location == "bcgeographicwarehouse" & d$format == "wms" + ]) }) -test_that("a record with bcgeographicwarehouse AND wms is return by bcdc_get_record",{ +test_that("a record with bcgeographicwarehouse AND wms is return by bcdc_get_record", { skip_on_cran() skip_if_net_down() sr <- bcdc_get_record('76b1b7a3-2112-4444-857a-afccf7b20da8') d <- sr$resource_df - expect_false(all(d$bcdata_available[d$location == "bcgeographicwarehouse" & d$format != "wms"])) - }) + expect_false(all(d$bcdata_available[ + d$location == "bcgeographicwarehouse" & d$format != "wms" + ])) +}) -test_that("a data frame with 8 columns of expected types is returned by bcdc_tidy_resources",{ +test_that("a data frame with 8 columns of expected types is returned by bcdc_tidy_resources", { skip_if_net_down() skip_on_cran() sr <- bcdc_get_record('76b1b7a3-2112-4444-857a-afccf7b20da8') @@ -141,7 +156,10 @@ test_that("a data frame with 8 columns of expected types is returned by bcdc_tid expect_type(d$wfs_available, "logical") expect_type(d$bcdata_available, "logical") expect_equal(d, bcdc_tidy_resources('76b1b7a3-2112-4444-857a-afccf7b20da8')) - expect_error(bcdc_tidy_resources(list()), "No bcdc_tidy_resources method for an object of class") + expect_error( + bcdc_tidy_resources(list()), + "No bcdc_tidy_resources method for an object of class" + ) expect_error( bcdc_tidy_resources("WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW"), "No bcdc_tidy_resources method for a BCGW object name" diff --git a/tests/testthat/test-options.R b/tests/testthat/test-options.R index 72007c1b..e0d13b06 100644 --- a/tests/testthat/test-options.R +++ b/tests/testthat/test-options.R @@ -10,14 +10,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("bcdc_options() returns a tibble",{ +test_that("bcdc_options() returns a tibble", { skip_if_net_down() skip_on_cran() opts <- bcdc_options() expect_s3_class(opts, "tbl_df") }) -test_that("bcdata.chunk_limit",{ +test_that("bcdata.chunk_limit", { skip_if_net_down() skip_on_cran() withr::with_options(list(bcdata.chunk_limit = 100000), { @@ -29,7 +29,7 @@ test_that("bcdata.chunk_limit",{ }) }) -test_that("bcdata.max_package_search_limit works",{ +test_that("bcdata.max_package_search_limit works", { skip_if_net_down() skip_on_cran() withr::with_options(list(bcdata.max_package_search_limit = 10), { @@ -38,7 +38,7 @@ test_that("bcdata.max_package_search_limit works",{ }) }) -test_that("bcdata.max_package_search_facet_limit works",{ +test_that("bcdata.max_package_search_facet_limit works", { skip_if_net_down() skip_on_cran() withr::with_options(list(bcdata.max_package_search_facet_limit = 10), { @@ -47,7 +47,7 @@ test_that("bcdata.max_package_search_facet_limit works",{ }) }) -test_that("bcdata.max_group_package_show_limit works",{ +test_that("bcdata.max_group_package_show_limit works", { skip_if_net_down() skip_on_cran() withr::with_options(list(bcdata.max_group_package_show_limit = 10), { @@ -68,7 +68,7 @@ test_that("bcdata.single_download_limit is deprecated but works", { ) }) -test_that("bcdata.single_download_limit can be changed",{ +test_that("bcdata.single_download_limit can be changed", { # This can be removed when bcdata.single_download_limit is removed skip_if_net_down() skip_on_cran() @@ -76,7 +76,7 @@ test_that("bcdata.single_download_limit can be changed",{ expect_equal(getOption("bcdata.single_download_limit"), 13) }) -test_that("bcdc_single_download_limit returns a number",{ +test_that("bcdc_single_download_limit returns a number", { skip_on_cran() skip_if_net_down() lt <- bcdc_single_download_limit() diff --git a/tests/testthat/test-print-methods.R b/tests/testthat/test-print-methods.R index 01bf4dac..7d3f2680 100644 --- a/tests/testthat/test-print-methods.R +++ b/tests/testthat/test-print-methods.R @@ -10,28 +10,34 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("bcdc_promise print methods work",{ +test_that("bcdc_promise print methods work", { skip_on_cran() skip_if_net_down() - promise_print <- capture_output(bcdc_query_geodata('76b1b7a3-2112-4444-857a-afccf7b20da8'), print = TRUE) + promise_print <- capture_output( + bcdc_query_geodata('76b1b7a3-2112-4444-857a-afccf7b20da8'), + print = TRUE + ) expect_true(nzchar(promise_print)) }) -test_that("bcdc_record print methods work",{ +test_that("bcdc_record print methods work", { skip_on_cran() skip_if_net_down() - record_print <- capture_output(bcdc_get_record('76b1b7a3-2112-4444-857a-afccf7b20da8'), print = TRUE) + record_print <- capture_output( + bcdc_get_record('76b1b7a3-2112-4444-857a-afccf7b20da8'), + print = TRUE + ) expect_true(nzchar(record_print)) }) -test_that("bcdc_recordlist print methods work",{ +test_that("bcdc_recordlist print methods work", { skip_on_cran() skip_if_net_down() recordlist_print <- capture_output(bcdc_search("bears"), print = TRUE) expect_true(nzchar(recordlist_print)) }) -test_that("show query works for bcdc_promise object",{ +test_that("show query works for bcdc_promise object", { skip_on_cran() skip_if_net_down() prom_obj <- bcdc_query_geodata('76b1b7a3-2112-4444-857a-afccf7b20da8') @@ -39,7 +45,7 @@ test_that("show query works for bcdc_promise object",{ }) -test_that("show query works for bcdc_sf object",{ +test_that("show query works for bcdc_sf object", { skip_on_cran() skip_if_net_down() sf_obj <- collect(bcdc_query_geodata('76b1b7a3-2112-4444-857a-afccf7b20da8')) @@ -49,7 +55,9 @@ test_that("show query works for bcdc_sf object",{ test_that("record with a zip file prints correctly", { skip_on_cran() skip_if_net_down() - output <- capture_output(bcdc_get_record("bc-grizzly-bear-habitat-classification-and-rating"), - print = TRUE) + output <- capture_output( + bcdc_get_record("bc-grizzly-bear-habitat-classification-and-rating"), + print = TRUE + ) expect_true(any(grepl("zip", output))) }) diff --git a/tests/testthat/test-query-geodata-base-methods.R b/tests/testthat/test-query-geodata-base-methods.R index 37b5bc41..981bc76d 100644 --- a/tests/testthat/test-query-geodata-base-methods.R +++ b/tests/testthat/test-query-geodata-base-methods.R @@ -1,4 +1,3 @@ - test_that("head works", { skip_if_net_down() skip_on_cran() @@ -8,14 +7,14 @@ test_that("head works", { collected <- collect(promise) expect_equal(nrow(collected), 6L) d2 <- bcdc_query_geodata(point_record) %>% - head(n = 3) %>% - collect() + head(n = 3) %>% + collect() expect_equal(nrow(d2), 3L) col <- pagination_sort_col(bcdc_describe_feature(point_record)) full_airport <- bcdc_get_data(point_record, resource = point_resource) expect_equal( d2[[col]], - head(full_airport[order(full_airport[[col]]),], 3L)[[col]] + head(full_airport[order(full_airport[[col]]), ], 3L)[[col]] ) }) @@ -35,12 +34,12 @@ test_that("tail works", { full_airport <- bcdc_get_data(point_record, resource = point_resource) expect_equal( d2[[col]], - tail(full_airport[order(full_airport[[col]]),], 3L)[[col]] + tail(full_airport[order(full_airport[[col]]), ], 3L)[[col]] ) }) -test_that("head/tail works with a record that would otherwise require pagination",{ +test_that("head/tail works with a record that would otherwise require pagination", { skip_if_net_down() skip_on_cran() dh <- bcdc_query_geodata('2af1388e-d5f7-46dc-a6e2-f85415ddbd1c') %>% diff --git a/tests/testthat/test-query-geodata-collect.R b/tests/testthat/test-query-geodata-collect.R index 8c94e4c8..4effcf6a 100644 --- a/tests/testthat/test-query-geodata-collect.R +++ b/tests/testthat/test-query-geodata-collect.R @@ -32,7 +32,9 @@ test_that("bcdc_query_geodata succeeds with a records over 10000 rows", { skip("Skipping the BEC test, though available for testing") expect_s3_class( collect( - bcdc_query_geodata("terrestrial-protected-areas-representation-by-biogeoclimatic-unit") + bcdc_query_geodata( + "terrestrial-protected-areas-representation-by-biogeoclimatic-unit" + ) ), "bcdc_sf" ) @@ -45,7 +47,7 @@ test_that("bcdc_query_geodata works with slug and full url using collect", { expect_s3_class( ret1 <- bcdc_query_geodata( glue::glue("{catalogue_base_url()}/dataset/bc-airports") - ) %>% + ) %>% collect(), "sf" ) @@ -55,13 +57,16 @@ test_that("bcdc_query_geodata works with slug and full url using collect", { ) expect_s3_class( ret3 <- bcdc_query_geodata( - glue::glue("{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8") - ) - %>% collect(), + glue::glue( + "{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8" + ) + ) %>% + collect(), "sf" ) expect_s3_class( - ret4 <- bcdc_query_geodata("76b1b7a3-2112-4444-857a-afccf7b20da8") %>% collect(), + ret4 <- bcdc_query_geodata("76b1b7a3-2112-4444-857a-afccf7b20da8") %>% + collect(), "sf" ) expect_s3_class( @@ -69,8 +74,8 @@ test_that("bcdc_query_geodata works with slug and full url using collect", { glue::glue( "{catalogue_base_url()}/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8/resource/4d0377d9-e8a1-429b-824f-0ce8f363512c" ) - ) - %>% collect(), + ) %>% + collect(), "sf" ) @@ -86,7 +91,9 @@ test_that("bcdc_query_geodata works with spatial data that have SHAPE for the ge ## bcdc_browse("bc-wildfire-fire-perimeters-historical") skip_on_cran() skip_if_net_down() - crd <- bcdc_query_geodata("regional-districts-legally-defined-administrative-areas-of-bc") %>% + crd <- bcdc_query_geodata( + "regional-districts-legally-defined-administrative-areas-of-bc" + ) %>% filter(ADMIN_AREA_NAME == "Cariboo Regional District") %>% collect() @@ -119,8 +126,15 @@ test_that("bcdc_sf objects has attributes", { expect_identical( names(attributes(sf_obj)), c( - "names", "row.names", "class", "sf_column", "agr", "query_list", - "url", "full_url", "time_downloaded" + "names", + "row.names", + "class", + "sf_column", + "agr", + "query_list", + "url", + "full_url", + "time_downloaded" ) ) expect_true(nzchar(attributes(sf_obj)$url)) diff --git a/tests/testthat/test-query-geodata-filter.R b/tests/testthat/test-query-geodata-filter.R index cd70e646..ac392865 100644 --- a/tests/testthat/test-query-geodata-filter.R +++ b/tests/testthat/test-query-geodata-filter.R @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and limitations under the License. library(sf, quietly = TRUE) -test_that("bcdc_query_geodata accepts R expressions to refine data call",{ +test_that("bcdc_query_geodata accepts R expressions to refine data call", { skip_on_cran() skip_if_net_down() one_feature <- bcdc_query_geodata(point_record) %>% @@ -22,7 +22,7 @@ test_that("bcdc_query_geodata accepts R expressions to refine data call",{ expect_equal(nrow(one_feature), 1) }) -test_that("bcdc_query_geodata accepts R expressions to refine data call",{ +test_that("bcdc_query_geodata accepts R expressions to refine data call", { skip_on_cran() skip_if_net_down() one_feature <- bcdc_query_geodata(point_record) %>% @@ -33,7 +33,7 @@ test_that("bcdc_query_geodata accepts R expressions to refine data call",{ expect_equal(nrow(one_feature), 1) }) -test_that("operators work with different remote geom col names",{ +test_that("operators work with different remote geom col names", { skip_on_cran() skip_if_net_down() @@ -43,7 +43,9 @@ test_that("operators work with different remote geom col names",{ collect() ## REMOTE "GEOMETRY" - em_program <- bcdc_query_geodata("employment-program-of-british-columbia-regional-boundaries") %>% + em_program <- bcdc_query_geodata( + "employment-program-of-british-columbia-regional-boundaries" + ) %>% filter(INTERSECTS(crd)) %>% collect() expect_s3_class(em_program, "sf") @@ -57,102 +59,161 @@ test_that("operators work with different remote geom col names",{ ) expect_s3_class(crd_fires, "sf") expect_equal(attr(crd_fires, "sf_column"), "geometry") - }) test_that("Different combinations of predicates work", { - the_bbox <- st_sfc(st_polygon( - list(structure(c(1670288.515, 1719022.009, - 1719022.009, 1670288.515, 1670288.515, 667643.77, 667643.77, - 745981.738, 745981.738, 667643.77), .Dim = c(5L, 2L)))), - crs = 3005) + the_bbox <- st_sfc( + st_polygon( + list(structure( + c( + 1670288.515, + 1719022.009, + 1719022.009, + 1670288.515, + 1670288.515, + 667643.77, + 667643.77, + 745981.738, + 745981.738, + 667643.77 + ), + .Dim = c(5L, 2L) + )) + ), + crs = 3005 + ) # with raw CQL - expect_equal(as.character(cql_translate(CQL('"POP_2000" < 2000'), - .colnames = "POP_2000")), - "(\"POP_2000\" < 2000)") + expect_equal( + as.character(cql_translate( + CQL('"POP_2000" < 2000'), + .colnames = "POP_2000" + )), + "(\"POP_2000\" < 2000)" + ) # just with spatial predicate - expect_equal(as.character(cql_translate(WITHIN(the_bbox))), - "(WITHIN({geom_name}, POLYGON ((1670289 667643.8, 1719022 667643.8, 1719022 745981.7, 1670289 745981.7, 1670289 667643.8))))") + expect_equal( + as.character(cql_translate(WITHIN(the_bbox))), + "(WITHIN({geom_name}, POLYGON ((1670289 667643.8, 1719022 667643.8, 1719022 745981.7, 1670289 745981.7, 1670289 667643.8))))" + ) # spatial predicate combined with regular comparison using comma and_statement <- "((WITHIN({geom_name}, POLYGON ((1670289 667643.8, 1719022 667643.8, 1719022 745981.7, 1670289 745981.7, 1670289 667643.8)))) AND (\"POP_2000\" < 2000))" - expect_equal(as.character(cql_translate(WITHIN(the_bbox), POP_2000 < 2000L, - .colnames = "POP_2000")), - and_statement) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox), + POP_2000 < 2000L, + .colnames = "POP_2000" + )), + and_statement + ) # spatial predicate combined with regular comparison as a named object using comma pop <- 2000L - expect_equal(as.character(cql_translate(WITHIN(the_bbox), POP_2000 < pop, - .colnames = "POP_2000")), - and_statement) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox), + POP_2000 < pop, + .colnames = "POP_2000" + )), + and_statement + ) and_with_logical <- "(WITHIN({geom_name}, POLYGON ((1670289 667643.8, 1719022 667643.8, 1719022 745981.7, 1670289 745981.7, 1670289 667643.8))) AND \"POP_2000\" < 2000)" # spatial predicate combined with regular comparison as a named object using # explicit & - expect_equal(as.character(cql_translate(WITHIN(the_bbox) & POP_2000 < pop, - .colnames = "POP_2000")), - and_with_logical) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox) & POP_2000 < pop, + .colnames = "POP_2000" + )), + and_with_logical + ) # spatial predicate combined with regular comparison as a named object using # explicit | or_statement <- "(WITHIN({geom_name}, POLYGON ((1670289 667643.8, 1719022 667643.8, 1719022 745981.7, 1670289 745981.7, 1670289 667643.8))) OR \"POP_2000\" < 2000)" - expect_equal(as.character(cql_translate(WITHIN(the_bbox) | POP_2000 < pop, - .colnames = "POP_2000")), - or_statement) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox) | POP_2000 < pop, + .colnames = "POP_2000" + )), + or_statement + ) # spatial predicate combined with CQL using comma - expect_equal(as.character(cql_translate(WITHIN(the_bbox), - CQL("\"POP_2000\" < 2000"), - .colnames = "POP_2000")), - and_statement) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox), + CQL("\"POP_2000\" < 2000"), + .colnames = "POP_2000" + )), + and_statement + ) # spatial predicate combined with CQL using explicit & - expect_equal(as.character(cql_translate(WITHIN(the_bbox) & - CQL("\"POP_2000\" < 2000"), - .colnames = "POP_2000")), - and_with_logical) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox) & + CQL("\"POP_2000\" < 2000"), + .colnames = "POP_2000" + )), + and_with_logical + ) # spatial predicate combined with CQL using explicit & - expect_equal(as.character(cql_translate(WITHIN(the_bbox) | - CQL("\"POP_2000\" < 2000"), - .colnames = "POP_2000")), - or_statement) + expect_equal( + as.character(cql_translate( + WITHIN(the_bbox) | + CQL("\"POP_2000\" < 2000"), + .colnames = "POP_2000" + )), + or_statement + ) }) test_that("subsetting works locally", { x <- c("a", "b") y <- data.frame(id = x, stringsAsFactors = FALSE) - expect_equal(as.character(cql_translate(foo == x[1], .colnames = "foo")), - "(\"foo\" = 'a')") - expect_equal(as.character(cql_translate(foo %in% local(y$id), .colnames = "foo")), - "(\"foo\" IN ('a', 'b'))") - expect_equal(as.character(cql_translate(foo %in% y[["id"]], .colnames = "foo")), - "(\"foo\" IN ('a', 'b'))") - expect_equal(as.character(cql_translate(foo == local(y$id[2]), .colnames = "foo")), - "(\"foo\" = 'b')") + expect_equal( + as.character(cql_translate(foo == x[1], .colnames = "foo")), + "(\"foo\" = 'a')" + ) + expect_equal( + as.character(cql_translate(foo %in% local(y$id), .colnames = "foo")), + "(\"foo\" IN ('a', 'b'))" + ) + expect_equal( + as.character(cql_translate(foo %in% y[["id"]], .colnames = "foo")), + "(\"foo\" IN ('a', 'b'))" + ) + expect_equal( + as.character(cql_translate(foo == local(y$id[2]), .colnames = "foo")), + "(\"foo\" = 'b')" + ) }) -test_that("large vectors supplied to filter succeeds",{ +test_that("large vectors supplied to filter succeeds", { skip_on_cran() skip_if_net_down() pori <- bcdc_query_geodata(lines_record) %>% filter(WATERSHED_GROUP_CODE %in% "PORI") %>% collect() - expect_s3_class(bcdc_query_geodata(lines_record) %>% - filter(WATERSHED_KEY %in% pori$WATERSHED_KEY), - "bcdc_promise") - + expect_s3_class( + bcdc_query_geodata(lines_record) %>% + filter(WATERSHED_KEY %in% pori$WATERSHED_KEY), + "bcdc_promise" + ) }) -test_that("multiple filter statements are additive",{ +test_that("multiple filter statements are additive", { skip_on_cran() skip_if_net_down() airports <- bcdc_query_geodata(point_record) - heliports_in_victoria <- airports %>% + heliports_in_victoria <- airports %>% filter(PHYSICAL_ADDRESS == "Victoria, BC") %>% filter(DESCRIPTION == "heliport") %>% collect() @@ -166,11 +227,13 @@ test_that("multiple filter statements are additive",{ filter(PHYSICAL_ADDRESS == "Victoria, BC") %>% filter(DESCRIPTION == "heliport") - expect_identical(finalize_cql(heliports_one_line$query_list$CQL_FILTER), - finalize_cql(heliports_two_line$query_list$CQL_FILTER)) + expect_identical( + finalize_cql(heliports_one_line$query_list$CQL_FILTER), + finalize_cql(heliports_two_line$query_list$CQL_FILTER) + ) }) -test_that("multiple filter statements are additive with geometric operators",{ +test_that("multiple filter statements are additive with geometric operators", { skip_on_cran() skip_if_net_down() ## LOCAL @@ -181,55 +244,81 @@ test_that("multiple filter statements are additive with geometric operators",{ st_as_sfc() ## REMOTE "GEOMETRY" - em_program <- bcdc_query_geodata("employment-program-of-british-columbia-regional-boundaries") %>% + em_program <- bcdc_query_geodata( + "employment-program-of-british-columbia-regional-boundaries" + ) %>% filter(ELMSD_REGION_BOUNDARY_NAME == "Interior") %>% filter(INTERSECTS(crd)) cql_query <- "((\"ELMSD_REGION_BOUNDARY_NAME\" = 'Interior') AND (INTERSECTS(GEOMETRY, POLYGON ((956376 653960.8, 1397042 653960.8, 1397042 949343.3, 956376 949343.3, 956376 653960.8)))))" - expect_equal(as.character(finalize_cql(em_program$query_list$CQL_FILTER)), - cql_query) + expect_equal( + as.character(finalize_cql(em_program$query_list$CQL_FILTER)), + cql_query + ) }) -test_that("an intersect with an object greater than 5E5 bytes automatically gets turned into a bbox",{ +test_that("an intersect with an object greater than 5E5 bytes automatically gets turned into a bbox", { skip_on_cran() skip_if_net_down() regions <- bcdc_query_geodata(polygon_record) %>% - filter(ADMIN_AREA_NAME %in% c("Bulkley Nechako Regional District", "Cariboo Regional District", "Regional District of Fraser-Fort George")) %>% + filter( + ADMIN_AREA_NAME %in% + c( + "Bulkley Nechako Regional District", + "Cariboo Regional District", + "Regional District of Fraser-Fort George" + ) + ) %>% collect() expect_true(utils::object.size(regions) > 5E5) - expect_message(parks <- bcdc_query_geodata(record = "6a2fea1b-0cc4-4fc2-8017-eaf755d516da") %>% - filter(WITHIN(regions)) %>% - collect()) + expect_message( + parks <- bcdc_query_geodata( + record = "6a2fea1b-0cc4-4fc2-8017-eaf755d516da" + ) %>% + filter(WITHIN(regions)) %>% + collect() + ) }) -test_that("an intersect with an object less than 5E5 proceeds",{ +test_that("an intersect with an object less than 5E5 proceeds", { skip_on_cran() skip_if_net_down() - small_districts <- bcdc_query_geodata("78ec5279-4534-49a1-97e8-9d315936f08b") %>% + small_districts <- bcdc_query_geodata( + "78ec5279-4534-49a1-97e8-9d315936f08b" + ) %>% filter(SCHOOL_DISTRICT_NAME %in% c("Prince George")) %>% collect() %>% st_bbox() %>% st_as_sfc() - - expect_s3_class(parks <- bcdc_query_geodata(record = "6a2fea1b-0cc4-4fc2-8017-eaf755d516da") %>% - filter(WITHIN(small_districts)) %>% - collect(), - "bcdc_sf") + expect_s3_class( + parks <- bcdc_query_geodata( + record = "6a2fea1b-0cc4-4fc2-8017-eaf755d516da" + ) %>% + filter(WITHIN(small_districts)) %>% + collect(), + "bcdc_sf" + ) }) test_that("a BCGW name works with filter", { skip_on_cran() skip_if_net_down() - little_box <- st_as_sfc(st_bbox(c(xmin = 506543.662, ymin = 467957.582, - xmax = 1696644.998, ymax = 1589145.873), - crs = 3005)) + little_box <- st_as_sfc(st_bbox( + c( + xmin = 506543.662, + ymin = 467957.582, + xmax = 1696644.998, + ymax = 1589145.873 + ), + crs = 3005 + )) ret <- bcdc_query_geodata("WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW") %>% filter(WITHIN(little_box)) %>% @@ -240,63 +329,104 @@ test_that("a BCGW name works with filter", { test_that("Using BBOX works", { skip_on_cran() skip_if_net_down() - query <- bcdc_query_geodata("WHSE_FOREST_VEGETATION.BEC_BIOGEOCLIMATIC_POLY", crs = 4326) %>% - filter(BBOX(c(1639473.0,528785.2,1665979.9,541201.0), crs = "EPSG:3005")) %>% + query <- bcdc_query_geodata( + "WHSE_FOREST_VEGETATION.BEC_BIOGEOCLIMATIC_POLY", + crs = 4326 + ) %>% + filter(BBOX( + c(1639473.0, 528785.2, 1665979.9, 541201.0), + crs = "EPSG:3005" + )) %>% show_query() - expect_equal(query$query_list$CQL_FILTER, - structure("(BBOX(GEOMETRY, 1639473, 528785.2, 1665979.9, 541201, 'EPSG:3005'))", - class = c("sql", "character"))) + expect_equal( + query$query_list$CQL_FILTER, + structure( + "(BBOX(GEOMETRY, 1639473, 528785.2, 1665979.9, 541201, 'EPSG:3005'))", + class = c("sql", "character") + ) + ) }) test_that("Nesting functions inside a CQL geometry predicate works (#146)", { skip_on_cran() skip_if_net_down() - the_geom <- st_sfc(st_point(c(1164434, 368738)), - st_point(c(1203023, 412959)), - crs = 3005) + the_geom <- st_sfc( + st_point(c(1164434, 368738)), + st_point(c(1203023, 412959)), + crs = 3005 + ) qry <- bcdc_query_geodata("local-and-regional-greenspaces") %>% filter(BBOX(local(st_bbox(the_geom, crs = st_crs(the_geom))))) %>% show_query() - expect_equal(as.character(qry$query_list$CQL_FILTER), - "(BBOX(SHAPE, 1164434, 368738, 1203023, 412959, 'EPSG:3005'))") + expect_equal( + as.character(qry$query_list$CQL_FILTER), + "(BBOX(SHAPE, 1164434, 368738, 1203023, 412959, 'EPSG:3005'))" + ) qry2 <- bcdc_query_geodata("local-and-regional-greenspaces") %>% - filter(DWITHIN(local(st_buffer(the_geom, 10000, nQuadSegs = 2)), 100, "meters")) %>% + filter(DWITHIN( + local(st_buffer(the_geom, 10000, nQuadSegs = 2)), + 100, + "meters" + )) %>% show_query() - expect_match(as.character(qry2$query_list$CQL_FILTER), - "\\(DWITHIN\\(SHAPE, MULTIPOLYGON \\(\\(\\([0-9. ,()]+\\)\\)\\), 100, meters\\)\\)") + expect_match( + as.character(qry2$query_list$CQL_FILTER), + "\\(DWITHIN\\(SHAPE, MULTIPOLYGON \\(\\(\\([0-9. ,()]+\\)\\)\\), 100, meters\\)\\)" + ) # Informative error when omit local: - expect_error(suppressWarnings( - bcdc_query_geodata("local-and-regional-greenspaces") %>% - filter(DWITHIN(st_buffer(the_geom, 10000, nQuadSegs = 2), 100, "meters")) + expect_error( + suppressWarnings( + bcdc_query_geodata("local-and-regional-greenspaces") %>% + filter(DWITHIN( + st_buffer(the_geom, 10000, nQuadSegs = 2), + 100, + "meters" + )) ), - "Cannot translate") + "Cannot translate" + ) }) test_that("works with dates", { skip_if_net_down() skip_on_cran() - expect_s3_class(bcdc_query_geodata('historical-orders-and-alerts') %>% - filter(EVENT_START_DATE < "2017-05-01") %>% - collect(), "bcdc_sf") - expect_s3_class(bcdc_query_geodata('historical-orders-and-alerts') %>% - filter(EVENT_START_DATE < as.Date("2017-05-01")) %>% - collect(), "bcdc_sf") - expect_s3_class(bcdc_query_geodata('historical-orders-and-alerts') %>% - filter(EVENT_START_DATE < as.POSIXct("2017-05-01")) %>% - collect(), "bcdc_sf") + expect_s3_class( + bcdc_query_geodata('historical-orders-and-alerts') %>% + filter(EVENT_START_DATE < "2017-05-01") %>% + collect(), + "bcdc_sf" + ) + expect_s3_class( + bcdc_query_geodata('historical-orders-and-alerts') %>% + filter(EVENT_START_DATE < as.Date("2017-05-01")) %>% + collect(), + "bcdc_sf" + ) + expect_s3_class( + bcdc_query_geodata('historical-orders-and-alerts') %>% + filter(EVENT_START_DATE < as.POSIXct("2017-05-01")) %>% + collect(), + "bcdc_sf" + ) dt <- as.Date("2017-05-01") - expect_s3_class(bcdc_query_geodata('historical-orders-and-alerts') %>% - filter(EVENT_START_DATE < dt) %>% - collect(), "bcdc_sf") + expect_s3_class( + bcdc_query_geodata('historical-orders-and-alerts') %>% + filter(EVENT_START_DATE < dt) %>% + collect(), + "bcdc_sf" + ) pt <- as.Date("2017-05-01") - expect_s3_class(bcdc_query_geodata('historical-orders-and-alerts') %>% - filter(EVENT_START_DATE < pt) %>% - collect(), "bcdc_sf") + expect_s3_class( + bcdc_query_geodata('historical-orders-and-alerts') %>% + filter(EVENT_START_DATE < pt) %>% + collect(), + "bcdc_sf" + ) }) test_that("works with various as.x functions", { @@ -306,10 +436,12 @@ test_that("works with various as.x functions", { bcdc_query_geodata(point_record) %>% filter(NUMBER_OF_RUNWAYS == as.numeric("3")) %>% collect(), - "bcdc_sf") + "bcdc_sf" + ) expect_s3_class( bcdc_query_geodata(point_record) %>% filter(DESCRIPTION == as.character("seaplane anchorage")) %>% collect(), - "bcdc_sf") + "bcdc_sf" + ) }) diff --git a/tests/testthat/test-query-geodata-mutate.R b/tests/testthat/test-query-geodata-mutate.R index f82a8e57..4a08bc8a 100644 --- a/tests/testthat/test-query-geodata-mutate.R +++ b/tests/testthat/test-query-geodata-mutate.R @@ -10,7 +10,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("mutate fails on a bcdata promise object",{ +test_that("mutate fails on a bcdata promise object", { skip_on_cran() skip_if_net_down() expect_error( diff --git a/tests/testthat/test-query-geodata-select.R b/tests/testthat/test-query-geodata-select.R index 5b3b395d..e6fda9de 100644 --- a/tests/testthat/test-query-geodata-select.R +++ b/tests/testthat/test-query-geodata-select.R @@ -10,7 +10,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. -test_that("select doesn't remove the geometry column",{ +test_that("select doesn't remove the geometry column", { skip_if_net_down() skip_on_cran() feat <- bcdc_query_geodata(point_record) %>% @@ -20,7 +20,7 @@ test_that("select doesn't remove the geometry column",{ expect_s3_class(feat, "bcdc_sf") }) -test_that("select works when selecting a column that isn't sticky",{ +test_that("select works when selecting a column that isn't sticky", { skip_if_net_down() skip_on_cran() feat <- bcdc_query_geodata(point_record) %>% @@ -32,14 +32,15 @@ test_that("select works when selecting a column that isn't sticky",{ }) -test_that("select reduces the number of columns when a sticky ",{ +test_that("select reduces the number of columns when a sticky ", { skip_if_net_down() skip_on_cran() feature_spec <- bcdc_describe_feature(point_record) ## Columns that can selected, while manually including GEOMETRY col sticky_cols <- c( - feature_spec[feature_spec$sticky,]$col_name, - "geometry") + feature_spec[feature_spec$sticky, ]$col_name, + "geometry" + ) sub_cols <- bcdc_query_geodata(point_record) %>% select(BUSINESS_CATEGORY_CLASS) %>% @@ -51,19 +52,21 @@ test_that("select reduces the number of columns when a sticky ",{ test_that("select works with BCGW name", { skip_on_cran() skip_if_net_down() - expect_s3_class(bcdc_query_geodata(bcgw_point_record) %>% - select(AIRPORT_NAME, DESCRIPTION) %>% - collect(), "sf") + expect_s3_class( + bcdc_query_geodata(bcgw_point_record) %>% + select(AIRPORT_NAME, DESCRIPTION) %>% + collect(), + "sf" + ) }) -test_that("select accept dplyr like column specifications",{ +test_that("select accept dplyr like column specifications", { skip_if_net_down() skip_on_cran() - layer <- bcdc_query_geodata(polygon_record) - wrong_fields <- c('ADMIN_AREA_NAME', 'dummy_col') - correct_fields <- c('ADMIN_AREA_NAME', 'OIC_MO_YEAR') - + layer <- bcdc_query_geodata(polygon_record) + wrong_fields <- c('ADMIN_AREA_NAME', 'dummy_col') + correct_fields <- c('ADMIN_AREA_NAME', 'OIC_MO_YEAR') ## Most basic select expect_s3_class(select(layer, ADMIN_AREA_NAME, OIC_MO_YEAR), "bcdc_promise") @@ -75,6 +78,9 @@ test_that("select accept dplyr like column specifications",{ ## Some weird mix expect_s3_class(select(layer, 'ADMIN_AREA_NAME', OIC_MO_YEAR), "bcdc_promise") ## Another weird mix - expect_s3_class(select(layer, c('ADMIN_AREA_NAME','OIC_MO_YEAR') , OIC_MO_NUMBER), "bcdc_promise") + expect_s3_class( + select(layer, c('ADMIN_AREA_NAME', 'OIC_MO_YEAR'), OIC_MO_NUMBER), + "bcdc_promise" + ) expect_s3_class(select(layer, 1:5), "bcdc_promise") }) diff --git a/tests/testthat/test-query-geodata.R b/tests/testthat/test-query-geodata.R index 27189bcf..6b664187 100644 --- a/tests/testthat/test-query-geodata.R +++ b/tests/testthat/test-query-geodata.R @@ -23,11 +23,13 @@ test_that("bcdc_query_geodata returns an bcdc_promise object for a valid id OR b expect_equal(bc_airports, bc_airports2) # neither character nor bcdc_record - expect_error(bcdc_query_geodata(1L), - "No bcdc_query_geodata method for an object of class integer") + expect_error( + bcdc_query_geodata(1L), + "No bcdc_query_geodata method for an object of class integer" + ) }) -test_that("bcdc_query_geodata returns an object with a query, a cli, the catalogue object, and a df of column names",{ +test_that("bcdc_query_geodata returns an object with a query, a cli, the catalogue object, and a df of column names", { skip_if_net_down() skip_on_cran() bc_airports <- bcdc_query_geodata("bc-airports") @@ -38,7 +40,7 @@ test_that("bcdc_query_geodata returns an object with a query, a cli, the catalog }) -test_that("bcdc_query_geodata returns an object with bcdc_promise class when using filter",{ +test_that("bcdc_query_geodata returns an object with bcdc_promise class when using filter", { skip_on_cran() skip_if_net_down() bc_eml <- bcdc_query_geodata("bc-environmental-monitoring-locations") %>% @@ -47,7 +49,7 @@ test_that("bcdc_query_geodata returns an object with bcdc_promise class when usi }) -test_that("bcdc_query_geodata returns an object with bcdc_promise class on record under 10000",{ +test_that("bcdc_query_geodata returns an object with bcdc_promise class on record under 10000", { skip_on_cran() skip_if_net_down() airports <- bcdc_query_geodata("bc-airports") @@ -57,13 +59,20 @@ test_that("bcdc_query_geodata returns an object with bcdc_promise class on recor test_that("bcdc_query_geodata fails when >1 record", { skip_if_net_down() skip_on_cran() - expect_error(bcdc_query_geodata(c("bc-airports", "bc-environmental-monitoring-locations")), - "Only one record my be queried at a time") + expect_error( + bcdc_query_geodata(c( + "bc-airports", + "bc-environmental-monitoring-locations" + )), + "Only one record my be queried at a time" + ) }) test_that("bcdc_query_geodata fails when no wfs available", { skip_if_net_down() skip_on_cran() - expect_error(bcdc_query_geodata("dba6c78a-1bc1-4d4f-b75c-96b5b0e7fd30"), - "No Web Feature Service resource available") + expect_error( + bcdc_query_geodata("dba6c78a-1bc1-4d4f-b75c-96b5b0e7fd30"), + "No Web Feature Service resource available" + ) }) diff --git a/tests/testthat/test-search.R b/tests/testthat/test-search.R index 86191e4e..b49b4f8c 100644 --- a/tests/testthat/test-search.R +++ b/tests/testthat/test-search.R @@ -57,7 +57,9 @@ test_that('bcdc_list_group_records works', { skip_if_net_down() census <- bcdc_list_group_records("census-profiles") grps <- bcdc_search_facets("groups") - census_count <- grps |> dplyr::filter(name == "census-profiles") |> dplyr::pull(count) + census_count <- grps |> + dplyr::filter(name == "census-profiles") |> + dplyr::pull(count) expect_s3_class(census, "data.frame") expect_gte(nrow(census), 1) expect_equal(nrow(census), census_count) @@ -68,7 +70,9 @@ test_that('bcdc_list_organization_records works', { skip_if_net_down() bcstats <- bcdc_list_organization_records("bc-stats") orgs <- bcdc_search_facets("organization") - bcstats_count <- orgs |> dplyr::filter(name == "bc-stats") |> dplyr::pull(count) + bcstats_count <- orgs |> + dplyr::filter(name == "bc-stats") |> + dplyr::pull(count) expect_s3_class(bcstats, "data.frame") expect_gte(nrow(bcstats), 1) expect_equal(nrow(bcstats), bcstats_count) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 244df875..b4a04ad8 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -33,15 +33,14 @@ test_that("pagination_sort_col works", { col_name = c("foo", "OBJECTID", "OBJECT_ID", "SEQUENCE_ID", "FEATURE_ID"), stringsAsFactors = FALSE ) - expect_equal(pagination_sort_col(cols_df), - "OBJECTID") - expect_equal(pagination_sort_col(cols_df[-2, , drop = FALSE]), - "OBJECT_ID") - expect_equal(pagination_sort_col(cols_df[c(-2, -3), , drop = FALSE]), - "SEQUENCE_ID") + expect_equal(pagination_sort_col(cols_df), "OBJECTID") + expect_equal(pagination_sort_col(cols_df[-2, , drop = FALSE]), "OBJECT_ID") + expect_equal( + pagination_sort_col(cols_df[c(-2, -3), , drop = FALSE]), + "SEQUENCE_ID" + ) expect_warning( - expect_equal(pagination_sort_col(cols_df[1, , drop = FALSE]), - "foo") + expect_equal(pagination_sort_col(cols_df[1, , drop = FALSE]), "foo") ) }) @@ -72,10 +71,14 @@ test_that("bcdc_get_capabilities works", { }) test_that("make_url works", { - expect_equal(make_url("https://foo.bar", "blah", "/buzz/", "/home.html"), - "https://foo.bar/blah/buzz/home.html") - expect_equal(make_url("https://foo.bar/", "blah/", "/buzz/", trailing_slash = TRUE), - "https://foo.bar/blah/buzz/") + expect_equal( + make_url("https://foo.bar", "blah", "/buzz/", "/home.html"), + "https://foo.bar/blah/buzz/home.html" + ) + expect_equal( + make_url("https://foo.bar/", "blah/", "/buzz/", trailing_slash = TRUE), + "https://foo.bar/blah/buzz/" + ) }) test_that("names_to_lazy_tbl works", { diff --git a/vignettes/precompile.R b/vignettes/precompile.R index c0bb7211..ae30c993 100644 --- a/vignettes/precompile.R +++ b/vignettes/precompile.R @@ -10,13 +10,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and limitations under the License. - - - # Precompile vignettes precompile <- function(vignette_to_run = NULL) { - orig_files <- file.path(list.files(path = "vignettes/", pattern = "*\\.Rmd\\.orig", full.names = TRUE)) + orig_files <- file.path(list.files( + path = "vignettes/", + pattern = "*\\.Rmd\\.orig", + full.names = TRUE + )) if (!is.null(vignette_to_run)) { orig_files <- orig_files[basename(orig_files) %in% vignette_to_run] @@ -25,13 +26,15 @@ precompile <- function(vignette_to_run = NULL) { } # Convert *.orig to *.Rmd ------------------------------------------------- - purrr::walk(orig_files, ~knitr::knit(.x, tools::file_path_sans_ext(.x))) + purrr::walk(orig_files, ~ knitr::knit(.x, tools::file_path_sans_ext(.x))) # Move .png files into correct directory so they render ------------------- images <- file.path(list.files(".", pattern = 'vignette-fig.*\\.png$')) - success <- file.copy(from = images, - to = file.path("vignettes", images), - overwrite = TRUE) + success <- file.copy( + from = images, + to = file.path("vignettes", images), + overwrite = TRUE + ) # Clean up if successful -------------------------------------------------- if (!all(success)) { @@ -45,4 +48,4 @@ precompile <- function(vignette_to_run = NULL) { precompile() ## Or just one -#precompile("bcdata.Rmd.orig") \ No newline at end of file +#precompile("bcdata.Rmd.orig")