-
Notifications
You must be signed in to change notification settings - Fork 1
/
h3-constructors.R
105 lines (88 loc) · 2.68 KB
/
h3-constructors.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
95
96
97
98
99
100
101
102
103
104
105
#' Create H3 Index
#'
#' Create H3 indices from `sfc` objects, vectors of x and y coordinates, or H3 string IDs.
#'
#' @param y a numeric vector of latitudes.
#' @param x for `h3_from_points()` an object of class `sfc_POINT`. For `h3_from_strings()` a character vector of H3 index IDs. For `h3_from_xy()` a numeric vector of longitudes.
#' @param resolution an integer indicating the H3 cell resolution. Must be between 0 and 15 inclusive.
#' @examples
#' h3_from_xy(-90, 120, 5)
#'
#' h3_from_strings("85f29383fffffff")
#'
#' if (requireNamespace("sf")) {
#' # create random points
#' pnts <- sf::st_cast(
#' sf::st_sfc(
#' sf::st_multipoint(matrix(runif(10, max = 90), ncol = 2)),
#' crs = 4326
#' ),
#' "POINT"
#' )
#'
#' # convert to H3 objects
#' h3s <- h3_from_points(pnts, 5)
#'
#' h3_to_vertexes(h3s)
#'
#' h3_to_points(h3s)
#' }
#'
#' @details
#' - `h3_from_points()`: takes an `sfc_POINT` object and creates a vector of `H3` cells
#' - `h3_from_strings()`: converts a character vector of cell indexes to an H3 vector
#' - `h3_from_xy()`: converts vectors of `x` and `y` coordinates to an `H3` vector
#' - `h3_to_points()`: converts an `H3` vector to a either an `sfc_POINT` object or a list of `sfg` `POINT` objects.
#' - `h3_to_vertexes()`: converts an `H3` vector to an `sfc_MULTIPOINT` object or a list of `MULTIPOINT` objects.
#' @export
#' @rdname H3
h3_from_xy <- function(x, y, resolution) {
if (resolution < 0 || resolution > 15)
stop("`resolution` must be an integer in range [0, 15]")
h3_from_xy_(x, y, resolution)
}
#' @export
#' @rdname H3
h3_from_points <- function(x, resolution) {
if (resolution < 0 || resolution > 15)
stop("`resolution` must be an integer in range [0, 15]")
if (!inherits(x, "sfc_POINT"))
stop("`x` must be an object of class `sfc_POINT`")
x_crs <- attr(x, "crs")
x_bbox <- sf::st_bbox(x)
if (is.na(x_crs)) {
if (any(abs(x_bbox[c("xmin", "xmax")]) > 180) || any(abs(x_bbox[c("ymin", "ymax")]) > 90)) {
stop("Input must be in EPSG:4326")
} else {
warning("CRS is missing assuming EPSG:4326")
}
}
h3_from_points_(x, resolution)
}
#' @export
#' @rdname H3
h3_from_strings <- function(x) h3_from_string_(x)
#' @export
#' @rdname H3
h3_to_points <- function(x) {
stopifnot(is_h3(x))
res <- h3_to_points_(x)
# ask user to install sf
rlang::check_installed("sf")
if (requireNamespace("sf")) {
res <- sf::st_sfc(res, crs = 4326)
}
res
}
#' @export
#' @rdname H3
h3_to_vertexes <- function(x) {
stopifnot(is_h3(x))
res <- h3_to_vertexes_(x)
# ask user to install sf
rlang::check_installed("sf")
if (requireNamespace("sf")) {
res <- sf::st_sfc(res, crs = 4326)
}
res
}