From 40864002156f1cfa674fdd36b373fecfdd4b6549 Mon Sep 17 00:00:00 2001 From: edzer Date: Fri, 11 Oct 2024 09:14:01 +0200 Subject: [PATCH 1/6] namespace --- R/raster.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/raster.R b/R/raster.R index 8a887bea..f3dc878f 100644 --- a/R/raster.R +++ b/R/raster.R @@ -358,7 +358,7 @@ st_as_raster = function(x, class, ...) { if (is.character(z)) { names(b) = z } else if (inherits(z, c("POSIXt", "Date"))) { - time(b) = z + terra::time(b) = z } else { names(b) = paste0(third, z) } From 3b57c75887568ef20f6f7043748de9d605ba4b98 Mon Sep 17 00:00:00 2001 From: edzer Date: Fri, 11 Oct 2024 09:14:24 +0200 Subject: [PATCH 2/6] link --- man/read_stars.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/read_stars.Rd b/man/read_stars.Rd index bdc6e5c0..8f040ebb 100644 --- a/man/read_stars.Rd +++ b/man/read_stars.Rd @@ -70,7 +70,7 @@ In case \code{.x} contains multiple files, they will all be read and combined wi \code{RasterIO} is a list with zero or more of the following named arguments: \code{nXOff}, \code{nYOff} (both 1-based: the first row/col has offset value 1), \code{nXSize}, \code{nYSize}, \code{nBufXSize}, \code{nBufYSize}, \code{bands}, \code{resample}. -See \url{https://gdal.org/doxygen/classGDALDataset.html} for their meaning; +See \url{https://gdal.org/en/latest/doxygen/classGDALDataset.html} for their meaning; \code{bands} is an integer vector containing the band numbers to be read (1-based: first band is 1). Note that if \code{nBufXSize} or \code{nBufYSize} are specified for downsampling an image, resulting in an adjusted geotransform. \code{resample} reflects the resampling method and From 7ecf082d97dd864d7d8cae048043cc92241a68dd Mon Sep 17 00:00:00 2001 From: edzer Date: Fri, 11 Oct 2024 09:20:40 +0200 Subject: [PATCH 3/6] fixes #718 --- NEWS.md | 2 ++ R/ops.R | 27 +++++++++++++++++++-------- R/read.R | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index b49e6784..e01c6838 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ # version 0.6-7 + +* `Ops.stars()` (math ops) now also recycle arrays in the first argument; #718 * `c.stars()` verifies semantic equivalence of objects' CRS; #703 diff --git a/R/ops.R b/R/ops.R index fe8ae017..01cb4ba7 100644 --- a/R/ops.R +++ b/R/ops.R @@ -34,11 +34,22 @@ first_dimensions_match = function(e1, e2) { #' to permutate dimensions first. Ops.stars <- function(e1, e2) { if (!missing(e2)) { - if (inherits(e1, "stars") && inherits(e2, "stars") && !first_dimensions_match(e1, e2)) - stop("(first) dimensions of e1 and e2 do not match") - if (!inherits(e2, "stars")) + if (inherits(e1, "stars") && inherits(e2, "stars")) { + if (!first_dimensions_match(e1, e2)) + stop("(first) dimensions of e1 and e2 do not match") + dim_final = if (prod(dim(e1)) < prod(dim(e2))) + st_dimensions(e2) + else + st_dimensions(e1) + } else if (inherits(e1, "stars")) { + dim_final = st_dimensions(e1) + } else if (inherits(e2, "stars")) { + dim_final = st_dimensions(e2) + } + if (!inherits(e2, c("stars", "units"))) e1 = drop_units(e1) - } + } else + dim_final = st_dimensions(e1) ret = if (missing(e2)) lapply(e1, .Generic) else if (!inherits(e2, "stars")) @@ -48,16 +59,16 @@ Ops.stars <- function(e1, e2) { if (!is.null(dim(e1)) && !isTRUE(all.equal(dim(e1), dim(e2), check.attributes = FALSE))) { stopifnot(length(e2) == 1) - lapply(e1, .Generic, e2 = structure(e2[[1]], dim = NULL)) + lapply(lapply(e1, structure, dim=NULL), .Generic, e2 = structure(e2[[1]], dim = NULL)) } else mapply(.Generic, e1, e2, SIMPLIFY = FALSE) } if (any(sapply(ret, function(x) is.null(dim(x))))) # happens if e1[[1]] is a factor; #304 - ret = lapply(ret, function(x) { dim(x) = dim(e1); x }) + ret = lapply(ret, function(x) { dim(x) = dim(dim_final); x }) if (! inherits(e1, "stars")) - st_as_stars(setNames(ret, names(e2)), dimensions = st_dimensions(e2)) + st_as_stars(setNames(ret, names(e2)), dimensions = dim_final) else - st_as_stars(ret, dimensions = st_dimensions(e1)) + st_as_stars(ret, dimensions = dim_final) } #' Mathematical operations for stars objects diff --git a/R/read.R b/R/read.R index ed42ad82..12f5b239 100644 --- a/R/read.R +++ b/R/read.R @@ -83,7 +83,7 @@ geoloc_is_2D = function(geolocation, driver) { # the thing has 2-D x and y array #' \code{RasterIO} is a list with zero or more of the following named arguments: #' \code{nXOff}, \code{nYOff} (both 1-based: the first row/col has offset value 1), #' \code{nXSize}, \code{nYSize}, \code{nBufXSize}, \code{nBufYSize}, \code{bands}, \code{resample}. -#' See \url{https://gdal.org/doxygen/classGDALDataset.html} for their meaning; +#' See \url{https://gdal.org/en/latest/doxygen/classGDALDataset.html} for their meaning; #' \code{bands} is an integer vector containing the band numbers to be read (1-based: first band is 1). #' Note that if \code{nBufXSize} or \code{nBufYSize} are specified for downsampling an image, #' resulting in an adjusted geotransform. \code{resample} reflects the resampling method and From c5a2fe2c1a98b5815ebcf75812de7244f2e4f880 Mon Sep 17 00:00:00 2001 From: edzer Date: Wed, 16 Oct 2024 22:07:24 +0200 Subject: [PATCH 4/6] use InterpolateAtPoint if GDAL >= 3.10.0 --- DESCRIPTION | 2 +- R/extract.R | 14 +++++++++++--- man/st_extract.Rd | 6 +++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0972d614..a5d2fec7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,7 +35,7 @@ LazyData: true Depends: R (>= 3.3.0), abind, - sf (>= 1.0-15) + sf (>= 1.0-19) Imports: methods, parallel, diff --git a/R/extract.R b/R/extract.R index 476268d0..28eb1e3f 100644 --- a/R/extract.R +++ b/R/extract.R @@ -12,6 +12,8 @@ st_extract = function(x, ...) UseMethod("st_extract") #' @param time_column character or integer; name or index of a column with time or date values that will be matched to values of the first temporal dimension (matching classes \code{POSIXct}, \code{POSIXt}, \code{Date}, or \code{PCICt}), in \code{x}, after which this dimension is reduced. This is useful to extract data cube values along a trajectory; see https://github.com/r-spatial/stars/issues/352 . #' @param interpolate_time logical; should time be interpolated? if FALSE, time instances are matched using the coinciding or the last preceding time in the data cube. #' @param FUN function used to aggregate pixel values when geometries of \code{at} intersect with more than one pixel +#' @param resampling character; resampling method; for method cubic or cubicspline, +#' `stars_proxy` objects should be used and GDAL should have version >= 3.10.0 #' @param ... passed on to \link{aggregate.stars} when geometries are not exclusively POINT geometries #' @returns if \code{at} is of class \code{matrix}, a matrix with extracted values is returned; #' otherwise: if \code{x} has more dimensions than only x and y (raster), an @@ -31,9 +33,15 @@ st_extract = function(x, ...) UseMethod("st_extract") #' st_extract(r, st_coordinates(pnt)) # "at" is a matrix: return a matrix st_extract.stars = function(x, at, ..., bilinear = FALSE, time_column = attr(at, "time_column") %||% attr(at, "time_col"), - interpolate_time = bilinear, FUN = mean) { + interpolate_time = bilinear, FUN = mean, + resampling = c("nearest", "bilinear", "cubic", "cubicspline")) { stopifnot(inherits(at, c("sf", "sfc", "matrix"))) + resampling = match.arg(resampling) + if (bilinear) { + stopifnot(resampling %in% c("nearest", "bilinear")) + resampling = "bilinear" + } if (inherits(at, "matrix")) pts = at else { @@ -54,7 +62,7 @@ st_extract.stars = function(x, at, ..., bilinear = FALSE, time_column = pts = st_coordinates(at) } - if (bilinear && !inherits(x, "stars_proxy")) + if (resampling != "nearest" && !inherits(x, "stars_proxy")) x = st_as_stars_proxy(x) min_dist = NULL @@ -63,7 +71,7 @@ st_extract.stars = function(x, at, ..., bilinear = FALSE, time_column = m = if (inherits(x, "stars_proxy")) { try_result = try(x0 <- st_as_stars(x, downsample = dim(x)/2), silent = TRUE) lapply(x, function(y) do.call(abind, lapply(get_names(y), - gdal_extract, pts = pts, bilinear = bilinear))) + gdal_extract, pts, resampling))) } else { x = st_normalize(st_upfront(x)) if (is_curvilinear(x)) { # https://github.com/r-spatial/stars/issues/632 diff --git a/man/st_extract.Rd b/man/st_extract.Rd index 686f6c04..ef71edb9 100644 --- a/man/st_extract.Rd +++ b/man/st_extract.Rd @@ -14,7 +14,8 @@ st_extract(x, ...) bilinear = FALSE, time_column = attr(at, "time_column") \%||\% attr(at, "time_col"), interpolate_time = bilinear, - FUN = mean + FUN = mean, + resampling = c("nearest", "bilinear", "cubic", "cubicspline") ) } \arguments{ @@ -31,6 +32,9 @@ st_extract(x, ...) \item{interpolate_time}{logical; should time be interpolated? if FALSE, time instances are matched using the coinciding or the last preceding time in the data cube.} \item{FUN}{function used to aggregate pixel values when geometries of \code{at} intersect with more than one pixel} + +\item{resampling}{character; resampling method; for method cubic or cubicspline, +`stars_proxy` objects should be used and GDAL should have version >= 3.10.0} } \value{ if \code{at} is of class \code{matrix}, a matrix with extracted values is returned; From 9654824a1b5ceca89134c149604967520e8d4a7b Mon Sep 17 00:00:00 2001 From: edzer Date: Wed, 16 Oct 2024 22:12:01 +0200 Subject: [PATCH 5/6] add sf in Remotes: --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index a5d2fec7..802d09df 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,6 +36,7 @@ Depends: R (>= 3.3.0), abind, sf (>= 1.0-19) +Remotes: r-spatial/sf Imports: methods, parallel, From 809f9f57267d65329f8ef80d9ddec7430f2fb4db Mon Sep 17 00:00:00 2001 From: edzer Date: Thu, 17 Oct 2024 09:04:12 +0200 Subject: [PATCH 6/6] omit macos --- .github/workflows/R-CMD-check.yaml | 2 +- NEWS.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 281cc636..7decd6a1 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -21,7 +21,7 @@ jobs: fail-fast: false matrix: config: - - {os: macos-latest, r: 'release'} + # - {os: macos-latest, r: 'release'} - {os: windows-latest, r: 'release'} - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} - {os: ubuntu-latest, r: 'release'} diff --git a/NEWS.md b/NEWS.md index e01c6838..8bdcca14 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ # version 0.6-7 + +* `st_extract()` if used with GDAL 3.10.0 uses InterpolateAtPoints, allowing for cubic and cubicspline interpolators (requiring sf >= 1.0-19). * `Ops.stars()` (math ops) now also recycle arrays in the first argument; #718