Skip to content

Commit 02e7777

Browse files
committed
load_tree_data() [WIP]
1 parent d08d54a commit 02e7777

4 files changed

Lines changed: 88 additions & 10 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Description: Maps individual tree stem locations on field plots of the Forest
1919
License: MIT + file LICENSE
2020
Depends: R (>= 4.1.0)
2121
Imports:
22+
cli,
2223
gdalraster (>= 2.4.0.9030),
2324
methods,
2425
spatstat.geom

R/load_tree_data.R

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,70 @@
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+
#'
227
#' @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) {
430

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")
733

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")
1036

11-
if (!is.null(sql) && !is.null(table))
37+
if (!is.null(table) && !is.null(sql))
1238
stop("'table' and 'sql' are mutually exclusive", call. = FALSE)
1339

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+
1449
ds <- NULL
1550
if (is.null(table) && is.null(sql)) {
1651
ds <- try(methods::new(gdalraster::GDALVector, src), silent = TRUE)
1752
} 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)
1955
} else if (!is.null(sql)) {
2056
ds <- try(methods::new(gdalraster::GDALVector, src, sql), silent = TRUE)
2157
}
2258

2359
if (!methods::is(ds, "Rcpp_GDALVector"))
2460
stop("failed to establish a connection to 'src'", call. = FALSE)
2561

26-
if (!is.null(table) && !is.null(columns) && columns != "")
62+
if (!is.null(columns) && columns[1] != "")
2763
ds$setSelectedFields(columns)
2864

29-
65+
cli::cli_progress_step("Fetching tree records")
66+
d <- ds$fetch(-1)
3067

3168
ds$close()
69+
return(d)
3270
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ home:
1414
reference:
1515
- title: Functions
1616
- contents:
17+
- load_tree_data
1718
- overlay_crowns
1819
- predict_crwidth
1920
- spatstat_helpers

man/load_tree_data.Rd

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)