Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcdc_get_data and bcdc_query_geodata with full url including resource #196

Merged
merged 2 commits into from
May 22, 2020
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions R/bcdc-web-services.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-get-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})


Expand Down
11 changes: 8 additions & 3 deletions tests/testthat/test-query-geodata-collect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})


Expand Down