From d2a587754e054dfc58486b777b92557c7afa7394 Mon Sep 17 00:00:00 2001 From: Ben Tupper Date: Wed, 15 May 2024 16:47:59 -0400 Subject: [PATCH] add normalize argument to st_crop --- R/stars.R | 3 ++- R/subset.R | 4 +++- man/st_cells.Rd | 3 ++- man/st_crop.Rd | 5 ++++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/R/stars.R b/R/stars.R index 8560536b5..51e71c1aa 100644 --- a/R/stars.R +++ b/R/stars.R @@ -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 diff --git a/R/subset.R b/R/subset.R index 654fcc4e6..82120947a 100644 --- a/R/subset.R +++ b/R/subset.R @@ -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. @@ -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) @@ -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 } diff --git a/man/st_cells.Rd b/man/st_cells.Rd index e69e52145..cc3a8480e 100644 --- a/man/st_cells.Rd +++ b/man/st_cells.Rd @@ -12,7 +12,8 @@ st_cells(x, sf) \item{sf}{object of class \code{sf} or \code{sfc}} } \description{ -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}} } \examples{ set.seed(1345) diff --git a/man/st_crop.Rd b/man/st_crop.Rd index 4c178dd3e..167b3e6f2 100644 --- a/man/st_crop.Rd +++ b/man/st_crop.Rd @@ -21,7 +21,8 @@ ..., 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 ) } \arguments{ @@ -38,6 +39,8 @@ \item{collect}{logical; if \code{TRUE}, repeat cropping on \code{stars} object, i.e. after data has been read} \item{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} + +\item{normalize}{logical; if \code{TRUE} then pass the cropped object to \code{\link{st_normalize}} before returning.} } \description{ crop a stars object