Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# bcdata (development version)

* `tail()` on a `bcdc_promise` now requests records in descending order
instead of skipping to the end with `startIndex`, which timed out on large
layers.
* Fixed YAML front matter in the service documentation vignette that newer
versions of pandoc fail to parse, and replaced the deprecated `.Dim`
attribute name with `dim` in a test. (#377)
Expand Down
29 changes: 18 additions & 11 deletions R/utils-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ print.bcdc_promise <- function(x, ...) {
number_of_records <- x$query_list$count
}

parsed <- bcdc_read_sf(cc$parse("UTF-8"))
parsed <- bcdc_read_sf(cc$parse("UTF-8"), sort_by = query_list$sortBy)
fields <- ncol(parsed) - 1

# Check if this was called using a whse name directly without going
Expand Down Expand Up @@ -146,7 +146,9 @@ record_print_helper <- function(r, n, print_avail = FALSE) {
cat_line_wrap(n, ") ", clean_wfs(r$name))
#cat_line_wrap(" description:", r$description)
cat_line_wrap("format: ", clean_wfs(formats_from_resource(r)), indent = 3)
if (r$format != "wms") cat_line_wrap("url: ", r$url, indent = 3)
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(
Expand All @@ -158,7 +160,7 @@ record_print_helper <- function(r, n, print_avail = FALSE) {
}
)
}
if (r$bcdata_available)
if (r$bcdata_available) {
cat_line_wrap(
"code: ",
"bcdc_get_data(record = '",
Expand All @@ -168,6 +170,7 @@ record_print_helper <- function(r, n, print_avail = FALSE) {
"')",
indent = 3
)
}
cat_line()
}

Expand Down Expand Up @@ -321,12 +324,12 @@ print.bcdc_query <- function(x, ...) {
#' }
#' @export
filter.bcdc_promise <- function(.data, ...) {
current_cql = cql_translate(
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)
current_cql <- specify_geom_name(.data$cols_df, current_cql)

# Append the new clause to any existing CQL filter.
.data$query_list$CQL_FILTER <- dbplyr::sql(
Expand Down Expand Up @@ -420,13 +423,13 @@ head.bcdc_promise <- function(x, n = 6L, ...) {
#' @importFrom utils tail
#' @export
tail.bcdc_promise <- function(x, n = 6L, ...) {
number_of_records <- bcdc_number_wfs_records(x$query_list, x$cli)
sorting_col <- pagination_sort_col(x$cols_df)
# Request the first n rows in descending order - avoids a large startIndex,
# which is very slow on large layers. collect() restores ascending order.
x$query_list <- c(
x$query_list,
count = n,
sortBy = sorting_col,
startIndex = number_of_records - n
sortBy = paste(sorting_col, "D")
)
x
}
Expand Down Expand Up @@ -539,7 +542,9 @@ collect.bcdc_promise <- function(x, ...) {
))
sorting_col <- pagination_sort_col(x$cols_df)

query_list <- c(query_list, sortby = sorting_col)
if (is.null(query_list$sortBy)) {
query_list <- c(query_list, sortby = sorting_col)
}

# Create pagination client
cc <- crul::Paginator$new(
Expand Down Expand Up @@ -571,7 +576,7 @@ collect.bcdc_promise <- function(x, ...) {
txt <- cc$parse("UTF-8")

as.bcdc_sf(
bcdc_read_sf(txt),
bcdc_read_sf(txt, sort_by = query_list$sortBy),
query_list = query_list,
url = url,
full_url = full_url
Expand Down Expand Up @@ -638,7 +643,9 @@ show_query.bcdc_sf <- function(x, ...) {

# collapse vector of cql statements into one
finalize_cql <- function(x, con = wfs_con) {
if (is.null(x) || !length(x)) return(NULL)
if (is.null(x) || !length(x)) {
return(NULL)
}
dbplyr::sql_vector(x, collapse = " AND ", con = con)
}

Expand Down
12 changes: 9 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ specify_geom_name <- function(cols_df, CQL_statement) {
dbplyr::sql(glue::glue(CQL_statement, geom_name = geom_col))
}

bcdc_read_sf <- function(x, ...) {
bcdc_read_sf <- function(x, sort_by = NULL, ...) {
if (length(x) == 1) {
return(sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...))
out <- sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...)
} else {
# tests that cover this are skipped due to large size
# nocov start
Expand All @@ -99,9 +99,15 @@ bcdc_read_sf <- function(x, ...) {
sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...)
})

do.call(rbind, sf_responses)
out <- do.call(rbind, sf_responses)
# nocov end
}

# tail() requests rows in descending order; flip them back to ascending
if (!is.null(sort_by) && grepl(" D$", sort_by)) {
out <- out[rev(seq_len(nrow(out))), ]
}
out
}

slug_from_url <- function(x) {
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/_snaps/query-geodata-base-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# tail works

Code
withr::with_options(list(pillar.width = Inf), print(tail(select(
bcdc_query_geodata(point_record), all_of(col)), n = 3)))
Output
Querying 'bc-airports' record
* Using collect() on this object will return 3 features and 9 fields
* At most six rows of the record are printed here
--------------------------------------------------------------------------------
Simple feature collection with 3 features and 9 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 1113277 ymin: 483437.1 xmax: 1470009 ymax: 758363.4
Projected CRS: NAD83 / BC Albers
# A tibble: 3 x 10
id
<chr>
1 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid-<id>
2 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid-<id>
3 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid-<id>
CUSTODIAN_ORG_DESCRIPTION
<chr>
1 "Ministry of Forest, Lands and Natural Resource Operations and Rural Developm~
2 "Ministry of Forest, Lands and Natural Resource Operations and Rural Developm~
3 "Ministry of Forest, Lands and Natural Resource Operations and Rural Developm~
BUSINESS_CATEGORY_CLASS BUSINESS_CATEGORY_DESCRIPTION
<chr> <chr>
1 airTransportation Air Transportation
2 airTransportation Air Transportation
3 airTransportation Air Transportation
OCCUPANT_TYPE_DESCRIPTION SOURCE_DATA_ID SUPPLIED_SOURCE_ID_IND
<chr> <chr> <chr>
1 BC Airports 642 N
2 BC Airports 526 N
3 BC Airports 813 N
AIRPORT_NAME SEQUENCE_ID
<chr> <int>
1 South Cariboo Regional Airport (108 Mile Ranch Airport) 1347
2 Oliver Airport 1349
3 Sister Islets 1351
geometry
<POINT [m]>
1 (1321215 758363.4)
2 (1470009 483437.1)
3 (1113277 497961.1)

23 changes: 21 additions & 2 deletions tests/testthat/test-query-geodata-base-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,38 @@ test_that("tail works", {
d2[[col]],
tail(full_airport[order(full_airport[[col]]), ], 3L)[[col]]
)

expect_snapshot(
withr::with_options(
list(pillar.width = Inf),
print(tail(select(bcdc_query_geodata(point_record), all_of(col)), n = 3))
),
# feature ids, and so column padding, vary between requests
transform = function(x) gsub(" +", " ", gsub("fid-\\S+", "fid-<id>", x))
)

# Force pagination to check sort order holds across pages
withr::local_options(bcdata.chunk_limit = 2)
d3 <- bcdc_query_geodata(point_record) %>%
tail(n = 5) %>%
collect()
expect_equal(
d3[[col]],
tail(full_airport[order(full_airport[[col]]), ], 5L)[[col]]
)
})


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') %>%
dh <- bcdc_query_geodata('43805524-4add-4474-ad53-1a985930f352') %>%
head(3) %>%
collect()

expect_equal(nrow(dh), 3L)

dt <- bcdc_query_geodata('2af1388e-d5f7-46dc-a6e2-f85415ddbd1c') %>%
dt <- bcdc_query_geodata('43805524-4add-4474-ad53-1a985930f352') %>%
tail(3) %>%
collect()

Expand Down
Loading