-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoords_to_position.R
202 lines (178 loc) · 5.47 KB
/
coords_to_position.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#' @title
#' Convert Coordinates to Grid Positions
#' @param x
#' One of: Numeric vector, `data.frame`, or `matrix`.
#' If a numeric vector, then it corresponds to X coordinates.
#' @param y
#' Numeric vector corresponding to Y coordinates.
#' @param ... Unused.
#' @param extent
#' Named vector with names `xmax`, `xmin`, `ymax`, `ymin`.
#' Corresponds to the bounding box of the given coordinates.
#' If `extent` is `NULL`, then the bounding box is found from the
#' given coordinates.
#' @param n
#' Exponent to the dimensions of the underlying grid. The Hilbert
#' Curve indices are based on a `2^n x 2^n` grid. This number
#' must be less than 15 due to the 32-bit implementation of R.
#' @param coords
#' Column names or indices of a `data.frame`/`matrix` that
#' contain the coordinates.
#' @param attach
#' If `TRUE`, adds the position as new columns to the given
#' `data.frame`/`matrix`. This will *replace* the coordinate columns.
#' @return A `data.frame` containing the positions as `integer`
#' columns `x` and `y`, or the original object
#' (`data.frame` or `matrix`) with the coordinates
#' replaced with the grid positions. When `n` is greater than 15,
#' the positions are of type `bit64::integer64`.
#' @rdname coords_to_position
#' @export
coords_to_position <- function(x, ..., n = 10L, extent = NULL) {
if (n < 16L) {
UseMethod("coords_to_position")
} else {
if (!requireNamespace("bit64", quietly = TRUE)) {
stop("`bit64` is required to use exponents greater than 15.")
}
UseMethod("coords_to_position64")
}
}
#' @rdname coords_to_position
#' @export
coords_to_position.data.frame <- function(
x, ..., n, extent, coords = c(1, 2), attach = TRUE
) {
if (is.null(extent)) extent <- .extent(x[[coords[1]]], x[[coords[2]]])
.Class <- class(x[[coords[1]]])
positions <- NextMethod(
"coords_to_position",
x = x[[coords[1]]],
y = x[[coords[2]]],
...,
n = n,
extent = extent
)
if (attach) {
x[[coords[1]]] <- positions[[1]]
x[[coords[2]]] <- positions[[2]]
return(x)
}
positions
}
#' @rdname coords_to_position
#' @export
coords_to_position.matrix <- function(
x, ..., n, extent, coords = c(1, 2), attach = TRUE
) {
if (is.null(extent)) extent <- .extent(x[, coords[1]], x[, coords[2]])
.Class <- class(x[, coords[1]])
positions <- NextMethod(
"coords_to_position",
x = x[, coords[1]],
y = x[, coords[2]],
...,
n = n,
extent = extent
)
if (attach) {
x[, coords[1]] <- positions[, 1]
x[, coords[2]] <- positions[, 2]
return(x)
}
positions
}
#' @rdname coords_to_position
#' @export
coords_to_position.numeric <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
HILBERT_coords_to_xy_(n, x, y, extent)
}
#' @rdname coords_to_position
#' @export
coords_to_position.double <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
HILBERT_coords_to_xy_(n, x, y, extent)
}
#' @rdname coords_to_position
#' @export
coords_to_position.integer <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
HILBERT_coords_to_xy_(n, x, y, extent)
}
#' @rdname coords_to_position
#' @export
coords_to_position64 <- function(x, ..., n = 10L, extent = NULL) {
UseMethod("coords_to_position64")
}
#' @rdname coords_to_position
#' @export
coords_to_position64.data.frame <- function(
x, ..., n, extent, coords = c(1, 2), attach = TRUE
) {
if (is.null(extent)) extent <- .extent(x[[coords[1]]], x[[coords[2]]])
.Class <- class(x[[coords[1]]])
positions <- NextMethod(
"coords_to_position64",
x = x[[coords[1]]],
y = x[[coords[2]]],
...,
n = n,
extent = extent
)
if (attach) {
x[[coords[1]]] <- positions[[1]]
x[[coords[2]]] <- positions[[2]]
return(x)
}
positions
}
#' @rdname coords_to_position
#' @export
coords_to_position64.matrix <- function(
x, ..., n, extent, coords = c(1, 2), attach = TRUE
) {
if (is.null(extent)) extent <- .extent(x[, coords[1]], x[, coords[2]])
.Class <- class(x[, coords[1]])
positions <- NextMethod(
"coords_to_position64",
x = x[, coords[1]],
y = x[, coords[2]],
...,
n = n,
extent = extent
)
if (attach) {
x[, coords[1]] <- positions[, 1]
x[, coords[2]] <- positions[, 2]
return(x)
}
positions
}
#' @rdname coords_to_position
#' @export
coords_to_position64.numeric <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
pos <- HILBERT_coords_to_xy_64_(n, x, y, extent)
pos[[1]] <- bit64::as.integer64(pos[[1]])
pos[[2]] <- bit64::as.integer64(pos[[2]])
pos
}
#' @rdname coords_to_position
#' @export
coords_to_position64.double <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
pos <- HILBERT_coords_to_xy_64_(n, x, y, extent)
pos[[1]] <- bit64::as.integer64(pos[[1]])
pos[[2]] <- bit64::as.integer64(pos[[2]])
pos
}
#' @rdname coords_to_position
#' @export
coords_to_position64.integer <- function(x, y, ..., n, extent) {
if (is.null(extent)) extent <- .extent(x, y)
pos <- HILBERT_coords_to_xy_64_(n, x, y, extent)
pos[[1]] <- bit64::as.integer64(pos[[1]])
pos[[2]] <- bit64::as.integer64(pos[[2]])
pos
}