-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.R
334 lines (269 loc) · 9.41 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright 2018 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.
base_url <- function() "https://catalogue.data.gov.bc.ca/api/3/"
bcdata_user_agent <- function(){
"https://github.com/bcgov/bcdata"
}
compact <- function(l) Filter(Negate(is.null), l)
bcdc_number_wfs_records <- function(query_list, client){
if(!is.null(query_list$propertyName)){
query_list$propertyName <- NULL
}
query_list <- c(resultType = "hits", query_list)
res_max <- client$post(body = query_list, encode = "form")
txt_max <- res_max$parse("UTF-8")
## 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){
# Find the geometry field and get the name of the field
geom_col <- geom_col_name(cols_df)
# substitute the geometry column name into the CQL statement and add sql class
dbplyr::sql(glue::glue(CQL_statement, geom_name = geom_col))
}
bcdc_read_sf <- function(x, ...){
if(length(x) == 1){
return(sf::read_sf(x, stringsAsFactors = FALSE, quiet = TRUE, ...))
} 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, ...)})
do.call(rbind, sf_responses)
# nocov end
}
}
slug_from_url <- function(x) {
if (grepl("^(http|www)", x)) x <- basename(x)
x
}
formats_supported <- function(){
c(bcdc_read_functions()[["format"]], "zip")
}
bcdc_http_client <- function(url = NULL) {
crul::HttpClient$new(url = url,
headers = list(`User-Agent` = bcdata_user_agent()))
}
## Check if there is internet
## h/t to https://github.com/ropensci/handlr/blob/pluralize/tests/testthat/helper-handlr.R
has_internet <- function() {
z <- try(suppressWarnings(readLines('https://www.google.com', n = 1)),
silent = TRUE)
!inherits(z, "try-error")
}
# Need the actual name of the geometry column
geom_col_name <- function(x) {
geom_type <- intersect(x$remote_col_type, gml_types())
x[x$remote_col_type == geom_type, , drop = FALSE]$col_name
}
#' @param x a resource_df from formatted record
#' @noRd
wfs_available <- function(x) {
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$location %in% c("bcgwdatastore", "bcgeographicwarehouse")
}
wfs_to_r_col_type <- function(col){
dplyr::case_when(
col == "xsd:string" ~ "character",
col == "xsd:date" ~ "date",
col == "xsd:decimal" ~ "numeric",
col == "xsd:hexBinary" ~ "numeric",
grepl("^gml:", col) ~ "sfc geometry",
TRUE ~ as.character(col)
)
}
##from a record
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")
)
x <- formats_from_resource(resource_df)
if(trim) return(x[x != ""])
x
}
formats_from_resource <- function(x){
dplyr::case_when(
x$format == x$url ~ x$format,
x$format == "wms" ~ "wms",
!nzchar(x$url) ~ x$format,
!nzchar(safe_file_ext(x)) ~ x$format,
x$url == "zip" ~ paste0(x$format, "(zipped)"),
TRUE ~ safe_file_ext(x)
)
}
safe_file_ext <- function(resource) {
url_format <- tools::file_ext(resource$url)
url_format <- ifelse(url_format == "zip", resource$format, url_format)
url_format
}
gml_types <- function(x) {
c(
"gml:PointPropertyType",
"gml:CurvePropertyType",
"gml:SurfacePropertyType",
"gml:GeometryPropertyType",
"gml:MultiPointPropertyType",
"gml:MultiCurvePropertyType",
"gml:MultiSurfacePropertyType",
"gml:MultiGeometryPropertyType"
)
}
get_record_warn_once <- function(...) {
silence <- isTRUE(getOption("silence_named_get_record_warning"))
warned <- bcdata_env$named_get_record_warned
if (!silence && !warned) {
warning(..., call. = FALSE)
assign("named_get_record_warned", TRUE, envir = bcdata_env)
}
}
clean_wfs <- function(x){
dplyr::case_when(
x == "WMS getCapabilities request" ~ "WFS request (Spatial Data)",
x == "wms" ~ "wfs",
TRUE ~ x
)
}
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.")
}
cli <- bcdc_http_client(file_url)
## Establish where to download file
tmp <- tempfile(tmpdir = unique_temp_dir(),
fileext = paste0(".", tools::file_ext(file_url)))
on.exit(unlink(tmp))
r <- cli$get(disk = tmp)
r$raise_for_status()
tmp <- handle_zip(tmp)
final_format <- tools::file_ext(tmp)
# Match the read function to the file format format and retrieve the function
funs <- bcdc_read_functions()
fun <- funs[funs$format == final_format, ]
# 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.")
tryCatch(do.call(fun$fun, list(tmp, ...)),
error = function(e) {
stop("Could not read data set. The file can be found here:\n '",
tmp, "'\n if you would like to try to read it manually.\n\n",
call. = FALSE)
})
}
resource_to_tibble <- function(x){
dplyr::tibble(
name = purrr::map_chr(x, "name"),
url = purrr::map_chr(x, "url"),
id = purrr::map_chr(x, "id"),
format = purrr::map_chr(x, "format"),
ext = purrr::map_chr(x, safe_file_ext),
package_id = purrr::map_chr(x, "package_id"),
location = simplify_string(purrr::map_chr(x, "resource_storage_location"))
)
}
simplify_string <- function(x) {
tolower(gsub("\\s+", "", x))
}
pagination_sort_col <- function(cols_df) {
cols <- cols_df[["col_name"]]
# use OBJECTID or OBJECT_ID as default sort key, if present
# if not present (several GSR tables), use SEQUENCE_ID
# Then try FEATURE_ID
for (idcol in c("OBJECTID", "OBJECT_ID", "SEQUENCE_ID", "FEATURE_ID")) {
if (idcol %in% cols) return(idcol)
}
#Otherwise use the first column - this is likely pretty fragile
warning("Unable to find a suitable column to sort on for pagination. Using",
" the first column (", cols[1],
"). Please check your data for obvious duplicated or missing rows.",
call. = FALSE)
cols[1]
}
handle_zip <- function(x) {
# Just give file back if it's not zipped
if (!is_filetype(x, "zip")) {
return(x)
}
# decompress into same dir
dir <- dirname(x)
utils::unzip(x, exdir = dir)
unlink(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)
}
files
}
unique_temp_dir <- function(pattern = "bcdata_") {
dir <- tempfile(pattern = pattern)
dir.create(file.path(dirname(dir), basename(dir)))
dir
}
list_supported_files <- function(dir) {
files <- list.files(dir, full.names = TRUE)
supported <- is_filetype(files, formats_supported())
if (!any(supported)) {
# check if any are directories, if not bail, otherwise extract them
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[supported]
}
catch_catalogue_error <- function(catalogue_response) {
msg <- "The BC data catalogue is currently unable to process this request\n"
if (inherits(catalogue_response, "Paginator")) {
statuses <- catalogue_response$status_code()
status_failed <- any(statuses >= 300)
msg <- paste0(msg, paste0(length(statuses), " paginated requests issued"))
} else {
status_failed <- catalogue_response$status_code >= 300
request_res <- catalogue_response$request_headers
response_res <- catalogue_response$response_headers
msg <- paste0(msg, "Catalogue request:\n")
for (i in seq_along(request_res)) {
msg <- paste0(
msg, " ", names(request_res)[i], ": ",
request_res[i], "\n"
)
}
msg <- paste0(msg, "Catalogue response:\n")
for (i in seq_along(response_res)) {
msg <- paste0(
msg, " ", names(response_res)[i], ": ",
response_res[i], "\n"
)
}
}
if (status_failed) {
stop(msg, call. = FALSE)
}
}