-
Notifications
You must be signed in to change notification settings - Fork 9
/
uas_setflds.R
94 lines (74 loc) · 2.87 KB
/
uas_setflds.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
#' Manage supplemental metadata fields
#'
#' Set and view a default set of supplemental metadata fields
#'
#' These functions allow you to define a default set of supplemental metadata fields that can be assigned
#' image collections either as a function argument, or a small text file placed
#' in the directory with the images. Note in this context 'supplemental metadata' refers to bits of information
#' that can not be automatically extracted from the images themselves (e.g., the name of the project
#' or pilot.)
#'
#' Creating a default set of supplemental metadata fields does not make them mandatory, nor does
#' it limit the metadata fields you can use. The default set serves as a template when
#' a new metadata.txt file is created.
#'
#' @seealso \code{\link{uas_getcache}}, \code{\link{uas_info}}
#' @describeIn uas_setflds Set default fields for image collection metadata.
#'
#' @param flds Character vector of metadata fields
#' @param reset Use a default set of metadata fields
#' @param quiet Suppress messages
#' @importFrom crayon yellow
#' @export
uas_setflds <- function(flds=NULL, reset=FALSE, quiet=FALSE) {
## If no fields were passed, use the default
if (is.null(flds) || reset) {
flds <- uas_flds_oem()
}
cache_dir <- uas_getcache(quiet = TRUE)
## If cache_dir is NA (never set), try to create it
if (is.na(cache_dir)) {
cache_dir <- uas_getcache(default = TRUE, quiet = TRUE)
## If cache_dir still doesn't exist, return an error
if (is.na(cache_dir)) {
stop("Can't find or create a cache directory. Use uas_setcache().")
}
}
## If cache_dir still doesn't exist, return an error
if (!file.exists(cache_dir)) {
stop("Can't find or create a cache directory. Use uas_setcache().")
}
save(flds, file = file.path(cache_dir, "flds_default.RData"))
if (!quiet) message(yellow(" - Default metadata fields set to:", paste(flds, collapse = ", ")))
invisible(flds)
}
#' @describeIn uas_setflds Get default supplemental metadata field names.
#' @export
uas_getflds <- function() {
flds_oem <- uas_flds_oem()
## Get the cache directory
cache_dir <- uas_getcache(quiet = TRUE)
## If cache_dir is NA (never set), try to create it
if (is.na(cache_dir)) {
cache_dir <- uas_getcache(default = TRUE, quiet = TRUE)
}
if (is.na(cache_dir)) {
## Unable to create a directory
flds_oem
} else {
## Look for a file called flds_default.RData in cache_dir
metadata_fn <- file.path(cache_dir, "flds_default.RData")
if (file.exists(metadata_fn)) {
#cat("Found the meatafile file in cache dir \n")
load(metadata_fn)
flds
} else {
flds_oem
}
}
}
#' @describeIn uas_setflds Get a standard set of image collection metedata fields
#' @export
uas_flds_oem <- function() {
c("name_short", "name_long", "description", "proj", "loc", "pilot", "contact", "uav", "data_url", "tags", "notes")
}