Skip to content

Commit

Permalink
fix doc and re-order coordinate columns
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebr committed Mar 5, 2020
1 parent 3a74084 commit 23fefab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 60 deletions.
59 changes: 16 additions & 43 deletions R/make.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,45 +158,26 @@ cast_combine <- function(x, y, .cast, .by_feature = FALSE, ...) {
sf::st_cast(pairs, to = .cast, ...)
}

#' Dump vertex to a nested tibble of points
#'
#' Creates a geometry column containing a tibble where each vertex is a row.
#' `st_dumppoints` is useful for expanding geometries. It is the reverse of a GROUP BY
#' in that it creates new rows. For example it can be used to expand
#' MULTIPOLYGONS into POLYGONS.
#' @param x Geometry `sfc` column
#' @rdname st_coordinates
#' @return A list of tibbles.
#' @seealso [sf::st_cast()], [st_coordinates()], [sf::st_coordinates()]
#' @export
st_dumppoints <- function(x) {
purrr::map(x, .st_dumppoints)
}

#' @importFrom rlang .data
.st_dumppoints <- function(x) {
dplyr::transmute(
.st_coordinates(x),
geom = st_point(.data$.x, .data$.y),
path = row_number()
)
}

#' List of vertex coordinates
#'
#' @rdname st_coordinates
#' @param x Geometry `sfc` column
#' @details `st_coordinates` returns a tibble containing coordinates, also some
#' grouping features. `POINT`: `.path = 1`; `MULTIPOINT`: `.path` orders the
#' points; `LINESTRING`: ``
#' grouping features. `POINT`: `.path = 1`; `MULTIPOINT` and `LINESTRING`: `.path` orders the
#' points; `LINESTRING`, `MULTILINESTRING` and `POLYGON`: `.l_` provides feature differentiation.
#' @seealso [sf::st_cast()], [st_dumppoints()], [sf::st_coordinates()]
#' @return A list of tibbles.
#' @export
st_coordinates <- function(x) {
purrr::map(x, .st_coordinates)
}

#' @importFrom rlang .data
.st_coordinates <- function(.x) {
tibble::as_tibble(sf::st_coordinates(.x)) %>%
purrr::set_names(~tolower(paste0(".", .x))) %>%
dplyr::mutate(.path = dplyr::row_number())
dplyr::mutate(.path = dplyr::row_number()) %>%
dplyr::select(.x, .data$.y, .data$.path, dplyr::everything())
}

#' Dump vertex to a nested tibble of points
Expand All @@ -207,26 +188,18 @@ st_coordinates <- function(x) {
#' MULTIPOLYGONS into POLYGONS.
#' @param x Geometry `sfc` column
#' @return A list of tibbles.
#' @seealso [sf::st_cast()], [st_coordinates()]
#' @seealso [sf::st_cast()], [st_coordinates()], [sf::st_coordinates()]
#' @export

st_dumppoints <- function(x) {
purrr::map(x, .st_dumppoints)
}

#' @importFrom rlang .data
.st_dumppoints <- function(x) {
.y <- NULL
.x <- NULL
.st_coordinates(x) %>%
transmute(geom = st_point(.x, .y), path = row_number())
}


st_coordinates <- function(x) {
purrr::map(x, .st_coordinates)
}

.st_coordinates <- function(.x) {
tibble::as_tibble(sf::st_coordinates(.x)) %>%
purrr::set_names(~tolower(paste0(".", .x))) %>%
dplyr::mutate(.path = dplyr::row_number())
dplyr::transmute(
.st_coordinates(x),
geom = st_point(.data$.x, .data$.y),
path = row_number()
)
}
18 changes: 6 additions & 12 deletions man/st_coordinates.Rd

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

2 changes: 1 addition & 1 deletion man/st_dumppoints.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ context("test-coordinates")

test_that("st_coordinates work", {
x <- st_makeline(c(11, 12), c(13, 14))
y <- tibble::tibble(.x = c(11, 12), .y = c(13, 14), .l1 = 1, .path = 1:2)
y <- tibble::tibble(.x = c(11, 12), .y = c(13, 14), .path = 1:2, .l1 = 1)
expect_identical(st_coordinates(x), list(y))
})
6 changes: 3 additions & 3 deletions tests/testthat/test-make.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ test_that("st_coordinates returns a `.path` for each point", {

expect_equal(names(st_coordinates(x)[[1]]), c(".x", ".y", ".path"))
x <- st_multi(x)
expect_equal(names(st_coordinates(x)[[1]]), c(".x", ".y", ".l1", ".path"))
expect_equal(names(st_coordinates(x)[[1]]), c(".x", ".y", ".path", ".l1"))
x <- st_makeline(x)
expect_equal(names(st_coordinates(x)[[1]]), c(".x", ".y", ".l1", ".path"))
# todo: add other types
expect_equal(names(st_coordinates(x)[[1]]), c(".x", ".y", ".path", ".l1"))
# todo: add other typesD
})

0 comments on commit 23fefab

Please sign in to comment.