diff --git a/NEWS.md b/NEWS.md index c1ac151e..631f424d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) diff --git a/R/utils-classes.R b/R/utils-classes.R index 9fafc6c8..f7d2446b 100644 --- a/R/utils-classes.R +++ b/R/utils-classes.R @@ -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 @@ -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( @@ -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 = '", @@ -168,6 +170,7 @@ record_print_helper <- function(r, n, print_avail = FALSE) { "')", indent = 3 ) + } cat_line() } @@ -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( @@ -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 } @@ -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( @@ -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 @@ -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) } diff --git a/R/utils.R b/R/utils.R index 32c5d0c3..e0e5c277 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 @@ -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) { diff --git a/tests/testthat/_snaps/query-geodata-base-methods.md b/tests/testthat/_snaps/query-geodata-base-methods.md new file mode 100644 index 00000000..9d316f3e --- /dev/null +++ b/tests/testthat/_snaps/query-geodata-base-methods.md @@ -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 + + 1 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid- + 2 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid- + 3 WHSE_IMAGERY_AND_BASE_MAPS.GSR_AIRPORTS_SVW.fid- + CUSTODIAN_ORG_DESCRIPTION + + 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 + + 1 airTransportation Air Transportation + 2 airTransportation Air Transportation + 3 airTransportation Air Transportation + OCCUPANT_TYPE_DESCRIPTION SOURCE_DATA_ID SUPPLIED_SOURCE_ID_IND + + 1 BC Airports 642 N + 2 BC Airports 526 N + 3 BC Airports 813 N + AIRPORT_NAME SEQUENCE_ID + + 1 South Cariboo Regional AirportĀ (108 Mile Ranch Airport) 1347 + 2 Oliver Airport 1349 + 3 Sister Islets 1351 + geometry + + 1 (1321215 758363.4) + 2 (1470009 483437.1) + 3 (1113277 497961.1) + diff --git a/tests/testthat/test-query-geodata-base-methods.R b/tests/testthat/test-query-geodata-base-methods.R index 981bc76d..22ed119c 100644 --- a/tests/testthat/test-query-geodata-base-methods.R +++ b/tests/testthat/test-query-geodata-base-methods.R @@ -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-", 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()