1
2#' Set coordinate values
3#'
4#' @inheritParams wk_handle
5#' @param z,m A vector of Z or M values applied feature-wise and recycled
6#'    along `handleable`. Use `NA` to keep the existing value of a given
7#'    feature.
8#' @param value An [xy()], [xyz()], [xym()], or [xyzm()] of coordinates
9#'   used to replace values in the input. Use `NA` to keep the existing
10#'   value.
11#' @param use_z,use_m Used to declare the output type. Use `TRUE` to
12#'   ensure the output has that dimension, `FALSE` to ensure it does not,
13#'   and `NA` to leave the dimension unchanged.
14#'
15#' @export
16#'
17#' @examples
18#' wk_set_z(wkt("POINT (0 1)"), 2)
19#' wk_set_m(wkt("POINT (0 1)"), 2)
20#' wk_drop_z(wkt("POINT ZM (0 1 2 3)"))
21#' wk_drop_m(wkt("POINT ZM (0 1 2 3)"))
22#'
23wk_set_z <- function(handleable, z, ...) {
24  wk_set_base(handleable, wk_trans_set(xyz(NA, NA, z), use_z = TRUE), ...)
25}
26
27#' @rdname wk_set_z
28#' @export
29wk_set_m <- function(handleable, m, ...) {
30  wk_set_base(handleable, wk_trans_set(xym(NA, NA, m), use_m = TRUE), ...)
31}
32
33#' @rdname wk_set_z
34#' @export
35wk_drop_z <- function(handleable, ...) {
36  wk_set_base(handleable, wk_trans_set(xy(NA, NA), use_z = FALSE), ...)
37}
38
39#' @rdname wk_set_z
40#' @export
41wk_drop_m <- function(handleable, ...) {
42  wk_set_base(handleable, wk_trans_set(xy(NA, NA), use_m = FALSE), ...)
43}
44
45#' @rdname wk_set_z
46#' @export
47wk_trans_set <- function(value, use_z = NA, use_m = NA) {
48  value <- as_xy(value)
49  value <- as_xy(value, dims = c("x", "y", "z", "m"))
50  new_wk_trans(
51    .Call(wk_c_trans_set_new, value, as.logical(use_z)[1], as.logical(use_m)[1]),
52    "wk_trans_set"
53  )
54}
55
56wk_set_base <- function(handleable, trans, ...) {
57  result <- wk_handle(handleable, wk_transform_filter(wk_writer(handleable), trans), ...)
58  wk_set_crs(wk_restore(handleable, result), wk_crs(handleable))
59}
60