Skip to content

Commit

Permalink
add xy & xyz constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Oct 26, 2023
1 parent 0a70a7d commit 42ec294
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 41 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export(update_tile_3d_layer)
export(update_tile_layer)
export(update_trips_layer)
export(view_state)
export(xy)
export(xyz)
importFrom(generics,compile)
importFrom(htmlwidgets,JS)
importFrom(lifecycle,deprecated)
Expand Down
54 changes: 33 additions & 21 deletions R/geometry.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
#' SFC Point
#' Point vectors
#'
#' Create an sfc_point column from coordinate vectors
#'
#' @name sfc_point
#' @param x x coordinate
#' @param y y coordinate
#' @param z z coordinate {optional}
#' @param crs coordinate reference system
#' @description
#' Create point vectors
#'
#' @name xy
#' @inheritParams wk::xyz
#' @examples
#' xy(1:5, 1:5)
#' xyz(1:5, 1:5, 1:5)
#' sfc_point(1:5, 1:5)
#' sfc_point(1:5, 1:5, 1:5)
#' @export
sfc_point <- function(x, y, z = NULL, crs = 4326) {
stopifnot(is.numeric(x), is.numeric(y), length(x) == length(y))
if (!is.null(z)) {
stopifnot(is.numeric(z), length(z) == length(x))
}
NULL

#' @describeIn xy Efficient 2-dimensional point vector
#' @export
xy <- function(x = double(), y = double(), crs = wk::wk_crs_longlat()) {
wk::xy(x, y, crs)
}


#' @describeIn xy Efficient 3-dimensional point vector
#' @export
xyz <- function(x = double(), y = double(), z = double(), crs = wk::wk_crs_longlat()) {
wk::xyz(x, y, z, crs)
}

#' @describeIn xy Simple features point vector
#' @export
sfc_point <- function(x = double(), y = double(), z = NULL, crs = wk::wk_crs_longlat()) {
vec <- wk::wk_handle(
if (is.null(z)) wk::xy(x, y) else wk::xyz(x, y, z),
wk::sfc_writer(FALSE)
)

data <- data.frame(cbind(x, y, z))
sf::st_as_sf(
data,
coords = names(data),
crs = crs,
na.fail = FALSE
) %>%
sf::st_geometry()
wk::wk_set_crs(vec, crs)
}


Expand Down
20 changes: 0 additions & 20 deletions man/sfc_point.Rd

This file was deleted.

35 changes: 35 additions & 0 deletions man/xy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42ec294

Please sign in to comment.