forked from gertstulp/ggplotgui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
space_detective.R
61 lines (45 loc) · 2.18 KB
/
space_detective.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
#'
#' the space detective
#'
#' Wrapper for \code{\link{classify_xy}}. Runs \code{\link{classify_xy}}, counts number of hits in each dimension, then decides what to depending on those two numbers.
#'
#' @param list Metajam output for a single data entity
#'
#' @return A character vector whose two elements are named "lat_col" and "lon_col", indicating the names of columns containing lat and lon information respectively, or an error message if not detected or if we aren't equipped to deal with the number of hits we got.
#'
#' @export
space_detective <- function(list) {
x_detect <- classify_xy(list, "x")
y_detect <- classify_xy(list, "y")
# count hits, assuming all logical. TODO: what to do with possible hits
x_hits <- sum(as.logical(unlist(x_detect)), na.rm = T)
y_hits <-
sum(as.logical(unlist(y_detect)), na.rm = T)
# no hits at all
if (x_hits == 0 & y_hits == 0) {
return(
"Space detective was not able to detect columns containing spatial information. Spatial plots will not be generated."
)
}
# equal hits in both dimensions
if (x_hits == y_hits) {
# single pair
if (x_hits == 1 & y_hits == 1) {
# get target column names
x_col <- list[["attribute_metadata"]][which(as.logical(x_detect)), "attributeName"]
y_col <- list[["attribute_metadata"]][which(as.logical(y_detect)), "attributeName"]
message(
paste("Space detective found a single pair of columns containing spatial information. \n Latitude column: ", y_col, "\n Longitude column: ", x_col, "\n")
)
cols <- c(y_col, x_col)
names(cols) <- c("lat_col", "lon_col")
return(cols)
} else {
# equal hits and larger than 1
message("Space detective found equal and larger than 1 numbers of x and y columns. What to do with this information pending. Meanwhile, spatial plots will not be generated.")
}
} else {
# unequal number of hits
message("Space detective found unequal numbers of x and y columns. What to do with this information pending. Meanwhile, spatial plots will not be generated.")
}
}