-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgh_fill.R
More file actions
28 lines (20 loc) · 889 Bytes
/
gh_fill.R
File metadata and controls
28 lines (20 loc) · 889 Bytes
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
#' Fill geohash prefix with members
#'
#' @param geohashes Character vector of input geohashes. They must all be of same precision
#' @param precision Positive integer scalar controlling the 'zoom level' – how many characters should be used in the output.
#' @return Character vector of geohashes corresponding to the input.
#' @export
gh_fill <- function(geohashes, precision) {
if (uniqueN(nchar(geohashes)) > 1) {
stop("Input Geohashes must all have the same precision level.")
}
if (sum(grepl("['ailo]", geohashes)) > 0) {
stop("Invalid Geohash; Valid characters: [0123456789bcdefghjkmnpqrstuvwxyz]")
}
new_levels <- precision - nchar(geohashes[1])
base32 <-
unlist(strsplit("0123456789bcdefghjkmnpqrstuvwxyz", split = ""))
grid <-
do.call(data.table::CJ, append(list(geohashes), replicate(new_levels, base32, FALSE)))
do.call(paste0, grid)
}