Skip to content

Commit

Permalink
address #733
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Feb 5, 2025
1 parent 1199ecc commit 791c0ae
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 48 deletions.
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# version 0.6-9

*
* `st_sfc2xy()` passes on `...` to `st_as_stars()`; #733

# version 0.6-8

* address difftime issue new in R-devel rev 87670
* address `/.difftime` issue new in R-devel rev 87670

* `c.stars()` is more strict when combining time sequences; #703

Expand Down
19 changes: 0 additions & 19 deletions R/rasterize.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,3 @@ st_as_stars.data.frame = function(.x, ..., dims = coords, xy, y_decreasing = TRU
)
st_stars(l, d)
}

#' replace POINT simple feature geometry list with an x y raster
#' @param x object of class \code{stars}, or of class \code{sf}
#' @param ... passed on to \link{as.data.frame.stars}
#' @return object of class \code{stars} with a POINT list replaced by x and y raster dimensions. This only works when the points are distributed over a regular or rectilinear grid.
#' @export
st_sfc2xy = function(x, ...) {
if (inherits(x, "sf"))
x = st_as_stars(x)
i = which_sfc(x)
d = st_dimensions(x)
if (!inherits(d[[i]]$values, "sfc_POINT"))
stop("point geometries expected")
cc = st_coordinates(d[[i]]$values)
df = as.data.frame(x)
df$geometry = NULL
s = st_as_stars(cbind(cc, df))
st_set_crs(s, st_crs(d))
}
37 changes: 34 additions & 3 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ st_as_sfc.stars = function(x, ..., as_points, which = seq_len(prod(dim(x)[1:2]))
sfc
}


#' replace x y raster dimensions with simple feature geometry list (points, or polygons = rasterize)
#' replace x y raster dimensions with simple feature geometry list (points, or polygons = rasterize) and vice versa
#' @param x object of class \code{stars}
#' @param as_points logical; if \code{TRUE}, generate points at cell centers, else generate polygons
#' @param ... arguments passed on to \code{st_as_sfc}
#' @param na.rm logical; omit (remove) cells which are entirely missing valued (across other dimensions)?
#' @return object of class \code{stars} with x and y raster dimensions replaced by a single sfc geometry list column containing either points, or polygons. Adjacent cells with identical values are not merged; see \code{st_rasterize} for this.
#' @details `st_xy2sfc` replaces x y raster dimensions with simple feature geometry list (points, or polygons = rasterize)
#' @return `st_xy2sfc` returns an object of class \code{stars} with x and y raster dimensions replaced by a single sfc geometry list column containing either points, or polygons. Adjacent cells with identical values are not merged; see \code{st_rasterize} for this.
#' @export
st_xy2sfc = function(x, as_points, ..., na.rm = TRUE) {

Expand Down Expand Up @@ -86,6 +86,37 @@ st_xy2sfc = function(x, as_points, ..., na.rm = TRUE) {
st_stars(x, dimensions = d)
}

#' @details `st_sfc2xy` replaces POINT simple feature geometry list with an x y raster
#' @param x object of class \code{stars}, or of class \code{sf}
#' @param ... passed on to \link{st_as_stars.data.frame}
#' @return `st_sfc2xy` returns an object of class \code{stars} with a POINT list replaced by X and Y raster dimensions. This only works when the points are distributed over a regular or rectilinear grid.
#' @export
#' @name st_xy2sfc
#' @examples
#' (reduced_nc = read_ncdf(system.file("nc/reduced.nc", package = "stars")))
#' (x = stars::st_xy2sfc(reduced_nc, as_points = TRUE, na.rm = FALSE))
#' # roundtrip:
#' st_sfc2xy(x, dims=c("X", "Y","zlev","time"))
st_sfc2xy = function(x, ...) {
if (inherits(x, "sf"))
x = st_as_stars(x)
i = which_sfc(x)
if (length(i) == 0)
stop("x does not contain a geometry dimension")
if (length(i) > 1) {
stop("using first geometry dimension only")
i = i[1]
}
d = st_dimensions(x)
if (!inherits(d[[i]]$values, "sfc_POINT"))
stop("point geometries expected")
cc = st_coordinates(d[[i]]$values)
df = as.data.frame(x)
df$geometry = NULL
s = st_as_stars(cbind(cc, df), ...)
st_set_crs(s, st_crs(d))
}

#' @export
st_as_sf.dimensions = function(x, ...) {
ix = which_sfc(x)[1]
Expand Down
19 changes: 0 additions & 19 deletions man/st_sfc2xy.Rd

This file was deleted.

26 changes: 21 additions & 5 deletions man/st_xy2sfc.Rd

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

0 comments on commit 791c0ae

Please sign in to comment.