Skip to content

Commit

Permalink
identical
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 19, 2023
1 parent b81daf2 commit 96c9fda
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
useDynLib(terra, .registration=TRUE)
import(methods, Rcpp)
exportClasses(SpatExtent, SpatRaster, SpatRasterDataset, SpatRasterCollection, SpatVector, SpatVectorProxy, SpatVectorCollection)
exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, area, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, extractRange, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin,readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize)
exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, area, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, extractRange, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, identical, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin,readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize)

S3method(cbind, SpatVector)
S3method(rbind, SpatVector)
Expand Down
2 changes: 2 additions & 0 deletions R/Agenerics.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if (!isGeneric("#")) { setGeneric("#", function(x, ...) standardGeneric("#")) }
if (!isGeneric("prcomp")) {setGeneric("prcomp", function(x, ...) standardGeneric("princomp"))}


if (!isGeneric("k_means")) {setGeneric("k_means", function(x, ...) standardGeneric("k_means"))}
if (!isGeneric("princomp")) {setGeneric("princomp", function(x, ...) standardGeneric("princomp"))}
Expand Down
9 changes: 9 additions & 0 deletions R/princomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ setMethod("princomp", signature(x="SpatRaster"),
}
)



setMethod("prcomp", signature(x="SpatRaster"),
function(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL, rank. = NULL, maxcell=Inf, ...) {
x = na.omit(spatSample(x, maxcell, "regular"))
prcomp(x, retx=retx, center=center, scale.=scale., tol=tol, rank.=rank., ...)
}
)

26 changes: 23 additions & 3 deletions R/values.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,21 @@ setMethod("compareGeom", signature(x="SpatVector", y="SpatVector"),


setMethod("all.equal", signature(target="SpatRaster", current="SpatRaster"),
function(target, current, maxcell=10000, ...) {
function(target, current, maxcell=100000, ...) {
a <- base::all.equal(rast(target), rast(current))
maxcell <- round(maxcell)
if (isTRUE(a) && maxcell > 0) {
hvT <- hasValues(target)
hvC <- hasValues(current)
if (hvT && hvC) {
s1 <- spatSample(current, maxcell, "regular")
s2 <- spatSample(target, maxcell, "regular")
if (maxcell < ncell(target)) {
warn(paste("all.equal", "taking a sample of ", maxcell, "cells"))
s1 <- spatSample(current, maxcell, "regular")
s2 <- spatSample(target, maxcell, "regular")
} else {
s1 <- values(current)
s2 <- values(target)
}
a <- all.equal(s1, s2, ...)
} else if (hvT || hvC) {
if (hvT) {
Expand All @@ -501,6 +508,19 @@ setMethod("all.equal", signature(target="SpatRaster", current="SpatRaster"),




setMethod("identical", signature(x="SpatRaster", y="SpatRaster"),
function(x, y) {
a <- isTRUE(all.equal(x, y, maxcell=0))
if (a && hasValues(x)) {
v <- unique(unlist(unique(x - y) ))
a <- identical(v, 0)
}
a
}
)


setMethod("values", signature("SpatVector"),
function(x, ...) {
as.data.frame(x, ...)
Expand Down
6 changes: 3 additions & 3 deletions man/all.equal.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ The sample size used can be increased with the \code{maxcell} argument. You can
}

\usage{
\S4method{all.equal}{SpatRaster,SpatRaster}(target, current, maxcell=10000, ...)
\S4method{all.equal}{SpatRaster,SpatRaster}(target, current, maxcell=100000, ...)
}

\arguments{
\item{target}{SpatRaster}
\item{current}{SpatRaster}
\item{maxcell}{positive integer. The size of the regular sample used to compare cell values}
\item{...}{additional arguments passed to all.equal.numeric to compare cell values}
\item{...}{additional arguments passed to \code{\link{all.equal.numeric}} to compare cell values}
}

\seealso{\code{\link{compareGeom}}}
\seealso{\code{\link{identical}}, \code{\link{compareGeom}}}

\value{
Either \code{TRUE} or a character vector describing the differences between target and current.
Expand Down
42 changes: 42 additions & 0 deletions man/identical.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
\name{identical}

\docType{methods}

\alias{identical}
\alias{identical,SpatRaster,SpatRaster-method}

\title{Compare two SpatRasters for equality}

\description{
Compare two SpatRasters for equality.

First the attributes of the objects are compared. If these are the same, a the raster cells are compared as well. This can be time consuming, and you may prefer to use a sample instead with \code{\link{all.equal}}
}

\usage{
\S4method{identical}{SpatRaster,SpatRaster}(x, y, maxcell=100000, ...)
}

\arguments{
\item{x}{SpatRaster}
\item{y}{SpatRaster}
}

\seealso{\code{\link{all.equal}}, \code{\link{compareGeom}}}

\value{
single logical value
}

\examples{
x <- sqrt(1:100)
mat <- matrix(x, 10, 10)
r1 <- rast(nrows=10, ncols=10, xmin=0, vals = x)
r2 <- rast(nrows=10, ncols=10, xmin=0, vals = t(mat))

identical(r1, r2)
identical(r1, r1*1)
identical(rast(r1), rast(r2))
}

\keyword{spatial}
1 change: 1 addition & 0 deletions src/Makevars.ucrt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ winlibs:

clean:
rm -f $(SHLIB) $(OBJECTS)

.PHONY: all winlibs clean

0 comments on commit 96c9fda

Please sign in to comment.