From 1d8b22aa0d01ff344e5a7a5a7256e29ea2c5d97a Mon Sep 17 00:00:00 2001 From: rhijmans Date: Tue, 27 Jun 2023 15:04:30 -0700 Subject: [PATCH] compareGeom method --- R/values.R | 25 +++++++++++++++++++++++++ man/compareGeom.Rd | 10 +++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/R/values.R b/R/values.R index a2b7c4ede..06fea1f6d 100644 --- a/R/values.R +++ b/R/values.R @@ -403,6 +403,31 @@ setMethod("compareGeom", signature(x="SpatRaster", y="SpatRaster"), ) +setMethod("compareGeom", signature(x="SpatRaster", y="list"), + function(x, y, ..., lyrs=FALSE, crs=TRUE, warncrs=FALSE, ext=TRUE, rowcol=TRUE, res=FALSE, stopOnError=TRUE, messages=FALSE) { + isr <- sapply(y, inherits, "SpatRaster") + if (!all(isr)) { + y <- y[isr] + if (length(y) == 0) error("compareGeom", "none of the elements of y is a SpatRaster") + n <- sum(!isr) + if (n > 1) { + warn("compareGeom", paste(n, "elements of y are not a SpatRaster")) + } else { + warn("compareGeom", paste("1 element of y is not a SpatRaster")) + } + } + out <- sapply(y, compareGeom, y=x, lyrs=lyrs, crs=crs, warncrs=warncrs, ext=ext, rowcol=rowcol, res=res, stopOnError=stopOnError, messages=messages) + if (!all(isr)) { + res <- rep(NA, length(isr)) + res[isr] <- out + return(res) + } + out + } +) + + + setMethod("compareGeom", signature(x="SpatVector", y="SpatVector"), function(x, y, tolerance=0) { diff --git a/man/compareGeom.Rd b/man/compareGeom.Rd index 5ad704106..1bba7c571 100644 --- a/man/compareGeom.Rd +++ b/man/compareGeom.Rd @@ -2,13 +2,17 @@ \alias{compareGeom} \alias{compareGeom,SpatRaster,SpatRaster-method} +\alias{compareGeom,SpatRaster,list-method} + \alias{compareGeom,SpatVector,SpatVector-method} \alias{compareGeom,SpatVector,missing-method} \title{Compare geometries of SpatRasters} \description{ -Evaluate whether two SpatRasters have the same extent, number of rows and columns, projection, resolution, and origin (or a subset of these comparisons). Or whether two SpatVectors have the same geometries, or whether a SpatVector has duplicated geometries. +Evaluate whether two SpatRasters have the same extent, number of rows and columns, projection, resolution, and origin (or a subset of these comparisons). + +Or evaluate whether two SpatVectors have the same geometries, or whether a SpatVector has duplicated geometries. } \usage{ @@ -21,8 +25,8 @@ Evaluate whether two SpatRasters have the same extent, number of rows and column } \arguments{ - \item{x}{SpatRaster} - \item{y}{SpatRaster} + \item{x}{SpatRaster or SpatVector} + \item{y}{Same as \code{x}. If \code{x} is a SpatRaster, \code{y} can also be a list of SpatRasters. If code{x} is a SpatVector, \code{y} can be missing} \item{...}{Additional SpatRasters} \item{lyrs}{logical. If \code{TRUE}, the number of layers is compared} \item{crs}{logical. If \code{TRUE}, coordinate reference systems are compared}