|
1 | | -#' @noRd |
| 1 | +#' Load tree data from a file or database connection |
| 2 | +#' |
| 3 | +#' `load_tree_data()` fetches tree records from a data source, most commonly |
| 4 | +#' a comma-separated values (CSV) file, a SQLite database file (.db, .sqlite, |
| 5 | +#' .gpkg), or a PostgreSQL database connection. Other data sources are also |
| 6 | +#' possible. File-based sources can be read from compressed archives without |
| 7 | +#' prior extraction (e.g., .zip), and network-hosted files can be read directly |
| 8 | +#' without prior download (see Details). |
| 9 | +#' |
| 10 | +#' @details |
| 11 | +#' A data source is typically specified as one of the following: **WIP** |
| 12 | +#' |
| 13 | +#' @param src A character string specifying the data source as a file name or |
| 14 | +#' database connection string (see Details). |
| 15 | +#' @param table Optional character string giving the name of a table in `src` |
| 16 | +#' from which tree records will be fetched. Generally needed with RDBMS sources |
| 17 | +#' containing multiple tables, as opposed to a single-table source such as a |
| 18 | +#' CSV file. |
| 19 | +#' @param columns Optional character vector specifying a subset of column names |
| 20 | +#' in the source table to include in the result set. |
| 21 | +#' @param sql Optional character string containing a SQL SELECT statement to |
| 22 | +#' execute on `src` (instead of selecting all records potentially from a subset |
| 23 | +#' of columns, i.e., mutually exclusive with `table` and/or `columns`). |
| 24 | +#' @return |
| 25 | +#' A data frame containing the tree records fetched from `src`. |
| 26 | +#' |
2 | 27 | #' @export |
3 | | -load_tree_data <- function(src, table = NULL, columns = NULL, sql = NULL) { |
| 28 | +load_tree_data <- function(src, table = NULL, columns = DEFAULT_TREE_COLUMNS, |
| 29 | + sql = NULL) { |
4 | 30 |
|
5 | | - default_cols <- c("PLT_CN", "SUBP", "TREE", "AZIMUTH", "DIST", "STATUSCD", |
6 | | - "SPCD", "DIA", "HT", "ACTUALHT", "CCLCD", "TPA_UNADJ") |
| 31 | + if (missing(src) || is.null(src)) |
| 32 | + stop("'src' is required") |
7 | 33 |
|
8 | | - if (is.null(columns)) |
9 | | - columns <- default_cols |
| 34 | + if (!(is.character(src) && length(src) == 1)) |
| 35 | + stop("'src' must be a single character string") |
10 | 36 |
|
11 | | - if (!is.null(sql) && !is.null(table)) |
| 37 | + if (!is.null(table) && !is.null(sql)) |
12 | 38 | stop("'table' and 'sql' are mutually exclusive", call. = FALSE) |
13 | 39 |
|
| 40 | + if (!is.null(table) && !(is.character(table) && length(table == 1))) |
| 41 | + stop("'table' must be a single character string") |
| 42 | + |
| 43 | + if (!is.null(columns) && !is.character(columns)) |
| 44 | + stop("'columns' must be a character vector") |
| 45 | + |
| 46 | + if (!is.null(sql) && !(is.character(sql) && length(sql == 1))) |
| 47 | + stop("'sql' must be a single character string") |
| 48 | + |
14 | 49 | ds <- NULL |
15 | 50 | if (is.null(table) && is.null(sql)) { |
16 | 51 | ds <- try(methods::new(gdalraster::GDALVector, src), silent = TRUE) |
17 | 52 | } else if (!is.null(table)) { |
18 | | - ds <- try(methods::new(gdalraster::GDALVector, src, table), silent = TRUE) |
| 53 | + ds <- try(methods::new(gdalraster::GDALVector, src, table), |
| 54 | + silent = TRUE) |
19 | 55 | } else if (!is.null(sql)) { |
20 | 56 | ds <- try(methods::new(gdalraster::GDALVector, src, sql), silent = TRUE) |
21 | 57 | } |
22 | 58 |
|
23 | 59 | if (!methods::is(ds, "Rcpp_GDALVector")) |
24 | 60 | stop("failed to establish a connection to 'src'", call. = FALSE) |
25 | 61 |
|
26 | | - if (!is.null(table) && !is.null(columns) && columns != "") |
| 62 | + if (!is.null(columns) && columns[1] != "") |
27 | 63 | ds$setSelectedFields(columns) |
28 | 64 |
|
29 | | - |
| 65 | + cli::cli_progress_step("Fetching tree records") |
| 66 | + d <- ds$fetch(-1) |
30 | 67 |
|
31 | 68 | ds$close() |
| 69 | + return(d) |
32 | 70 | } |
0 commit comments