diff --git a/NEWS.md b/NEWS.md index 55e67a4d..89dcb0b1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * Add `head` and `tail` methods for `bcdc.promise` objects. Thanks to @hgriesbauer for the suggestion! (#182, #186) * Provide `as_tibble` as an alias for `collect` in line with `dbplyr` behaviour (#166) * When reading in excel files, `bcdc_get_data` now outputs a messages indicating the presence and names of any sheets (#190) +* `bcdc_get_data()` & `bcdc_query_geodata()` will now work with full B.C. data catalogue url including resource (#125, #196) ### BUG FIXES * Fix `select`, `filter` and `mutate` roxygen so that bcdata specific documentation to these methods is available diff --git a/R/bcdc-web-services.R b/R/bcdc-web-services.R index d9788660..7cd6c0c9 100644 --- a/R/bcdc-web-services.R +++ b/R/bcdc-web-services.R @@ -104,6 +104,12 @@ bcdc_query_geodata.character <- function(record, crs = 3005) { ) } + if (grepl("/resource/", record)) { + # A full url was passed including record and resource compenents. + # Grab the resource id and strip it off the url + record <- gsub("/resource/.+", "", record) + } + obj <- bcdc_get_record(record) bcdc_query_geodata(obj, crs) diff --git a/R/get_data.R b/R/get_data.R index cc1f2e77..f37aef26 100644 --- a/R/get_data.R +++ b/R/get_data.R @@ -94,6 +94,13 @@ bcdc_get_data.character <- function(record, resource = NULL, verbose = TRUE, ... return(collect(query)) } + if (grepl("/resource/", record)) { + # A full url was passed including record and resource compenents. + # Grab the resource id and strip it off the url + resource <- slug_from_url(record) + record <- gsub("/resource/.+", "", record) + } + x <- slug_from_url(record) x <- bcdc_get_record(x) diff --git a/tests/testthat/test-get-data.R b/tests/testthat/test-get-data.R index 943123ba..3be33e21 100644 --- a/tests/testthat/test-get-data.R +++ b/tests/testthat/test-get-data.R @@ -33,9 +33,13 @@ test_that("bcdc_get_data works with slug and full url with corresponding resourc "sf") expect_is(ret4 <- bcdc_get_data("76b1b7a3-2112-4444-857a-afccf7b20da8", resource = "4d0377d9-e8a1-429b-824f-0ce8f363512c"), "sf") - ## Must be a better way to test if these objects are equal - expect_true(all(unlist(lapply(list(ret1, ret2, ret3, ret4), nrow)))) - expect_true(all(unlist(lapply(list(ret1, ret2, ret3, ret4), ncol)))) + expect_is(ret5 <- bcdc_get_data("https://catalogue.data.gov.bc.ca/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8/resource/4d0377d9-e8a1-429b-824f-0ce8f363512c"), + "sf") + + for (x in list(ret2, ret3, ret4, ret5)) { + expect_equal(dim(x), dim(ret1)) + expect_equal(names(x), names(ret1)) + } }) diff --git a/tests/testthat/test-query-geodata-collect.R b/tests/testthat/test-query-geodata-collect.R index fc9b1c21..e5a241d0 100644 --- a/tests/testthat/test-query-geodata-collect.R +++ b/tests/testthat/test-query-geodata-collect.R @@ -48,9 +48,14 @@ test_that("bcdc_query_geodata works with slug and full url using collect", { "sf") expect_is(ret4 <- bcdc_query_geodata("76b1b7a3-2112-4444-857a-afccf7b20da8") %>% collect(), "sf") - ## Must be a better way to test if these objects are equal - expect_true(all(unlist(lapply(list(ret1, ret2, ret3, ret4), nrow)))) - expect_true(all(unlist(lapply(list(ret1, ret2, ret3, ret4), ncol)))) + expect_is(ret5 <- bcdc_query_geodata("https://catalogue.data.gov.bc.ca/dataset/76b1b7a3-2112-4444-857a-afccf7b20da8/resource/4d0377d9-e8a1-429b-824f-0ce8f363512c") + %>% collect(), + "sf") + + for (x in list(ret2, ret3, ret4, ret5)) { + expect_equal(dim(x), dim(ret1)) + expect_equal(names(x), names(ret1)) + } })