Skip to content

Commit

Permalink
Merge pull request #686 from BigelowLab/main
Browse files Browse the repository at this point in the history
add normalize argument to st_crop
  • Loading branch information
edzer authored May 16, 2024
2 parents 8636c20 + d2a5877 commit 1dbd5d2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ st_cells_from_xy = function(x, xy) {

#' return the cell index corresponding to the location of a set of points
#'
#' return the cell index corresponding to the location of a set of points
#' If the object has been cropped without normalization, then the indices return
#' are relative to the original uncropped extent. See \code{\link{st_crop}}
#' @param x object of class \code{stars}
#' @param sf object of class \code{sf} or \code{sfc}
#' @examples
Expand Down
4 changes: 3 additions & 1 deletion R/subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ st_intersects.bbox = function(x, y, ...) { # FIXME: segmentize first if geograph
#' @param as_points logical; only relevant if \code{y} is of class \code{sf} or \code{sfc}: if \code{FALSE}, treat \code{x} as a set of points, else as a set of small polygons. Default: \code{TRUE} if \code{y} is two-dimensional, else \code{FALSE}; see Details
#' @param ... ignored
#' @param crop logical; if \code{TRUE}, the spatial extent of the returned object is cropped to still cover \code{obj}, if \code{FALSE}, the extent remains the same but cells outside \code{y} are given \code{NA} values.
#' @param normalize logical; if \code{TRUE} then pass the cropped object to \code{\link{st_normalize}} before returning.
#' @details for raster \code{x}, \code{st_crop} selects cells that intersect with \code{y}.
#' For intersection, are raster cells interpreted as points or as small polygons?
#' If \code{y} is of class \code{stars}, \code{x} raster cells are interpreted as points; if \code{y} is of class \code{bbox}, \code{x} cells are interpreted as cells (small polygons). Otherwise, if \code{as_points} is not given, cells are interpreted as points if \code{y} has a two-dimensional geometry.
Expand Down Expand Up @@ -250,7 +251,7 @@ st_intersects.bbox = function(x, y, ...) { # FIXME: segmentize first if geograph
#' image(l7[bb,,,1], add = TRUE, col = sf.colors())
#' plot(st_as_sfc(bb), add = TRUE, border = 'green', lwd = 2)
st_crop.stars = function(x, y, ..., crop = TRUE, epsilon = sqrt(.Machine$double.eps),
as_points = all(st_dimension(y) == 2, na.rm = TRUE)) {
as_points = all(st_dimension(y) == 2, na.rm = TRUE), normalize = FALSE) {
x = st_upfront(x) # put spatial dimensions up front; https://github.com/r-spatial/stars/issues/457
d = dim(x)
dm = st_dimensions(x)
Expand Down Expand Up @@ -307,6 +308,7 @@ st_crop.stars = function(x, y, ..., crop = TRUE, epsilon = sqrt(.Machine$double.
for (i in seq_along(x))
x[[i]][mask] = NA
}
if (normalize[1]) x = st_normalize(x)
x
}

Expand Down
3 changes: 2 additions & 1 deletion man/st_cells.Rd

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

5 changes: 4 additions & 1 deletion man/st_crop.Rd

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

0 comments on commit 1dbd5d2

Please sign in to comment.