Skip to content

Commit e7f1a2a

Browse files
committed
fixes #1928
1 parent 4f3f045 commit e7f1a2a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

R/generics.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,9 +1251,22 @@ setMethod("unique", signature(x="SpatRaster", incomparables="ANY"),
12511251

12521252

12531253
setMethod("unique", signature(x="SpatVector", incomparables="ANY"),
1254-
function(x, incomparables=FALSE, ...) {
1255-
u <- unique(as.data.frame(x, geom="WKT"), incomparables=incomparables, ...)
1256-
vect(u, geom="geometry", crs(x))
1254+
function(x, incomparables=FALSE, geom=TRUE, atts=TRUE, ...) {
1255+
if (geom && atts) {
1256+
u <- unique(as.data.frame(x, geom="WKT"), incomparables=incomparables, ...)
1257+
vect(u, geom="geometry", crs(x))
1258+
} else if (geom) {
1259+
g <- geom(x, hex=TRUE)
1260+
i <- !duplicated(g, ...)
1261+
x[i,]
1262+
} else if (atts) {
1263+
d <- data.frame(x)
1264+
i <- !duplicated(d, ...)
1265+
x[i,]
1266+
} else {
1267+
warn("unique", "geom and atts are both FALSE")
1268+
x
1269+
}
12571270
}
12581271
)
12591272

man/unique.Rd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This method returns the unique values in a SpatRaster, or removes duplicates rec
1717
\usage{
1818
\S4method{unique}{SpatRaster}(x, incomparables=FALSE, digits=NA, na.rm=TRUE, as.raster=FALSE)
1919

20-
\S4method{unique}{SpatVector}(x, incomparables=FALSE, ...)
20+
\S4method{unique}{SpatVector}(x, incomparables=FALSE, geom=TRUE, atts=TRUE, ...)
2121
}
2222

2323
\arguments{
@@ -26,7 +26,9 @@ This method returns the unique values in a SpatRaster, or removes duplicates rec
2626
\item{digits}{integer. The number of digits for rounding the values before finding the unique values. Use \code{NA} means to not do any rounding}
2727
\item{na.rm}{logical. If \code{TRUE}, \code{NaN} is included if there are any missing values}
2828
\item{as.raster}{logical. If \code{TRUE}, a single-layer categorical SpatRaster with the unique values is returned}
29-
\item{...}{additional arguments passed on to \code{\link[base]{unique}}}
29+
\item{...}{additional arguments passed on to \code{\link[base]{unique}} or \code{\link[base]{identical}}}
30+
\item{geom}{logical. If \code{TRUE} the geometries are considered to determine uniqueness}
31+
\item{atts}{logical. If \code{TRUE} the attribute values are considered to determine uniqueness}
3032
}
3133

3234

0 commit comments

Comments
 (0)