Skip to content

Commit

Permalink
zeroasna
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Mar 15, 2021
1 parent b8cd439 commit cd4c35c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


setMethod("patches", signature(x="SpatRaster"),
function(x, directions=4, filename="", ...) {
function(x, directions=4, zeroAsNA=FALSE, filename="", ...) {
opt <- spatOptions(filename, ...)
x@ptr <- x@ptr$patches(directions[1], opt)
x@ptr <- x@ptr$patches(directions[1], zeroAsNA[1], opt)
messages(x, "patches")
}
)
Expand Down
16 changes: 9 additions & 7 deletions man/patches.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
\title{Detect patches (clumps) of cells}

\description{
Detect patches (clumps). Patches are groups of cells that are surrounded by cells that are \code{NA}. To also ignore cells that are zero, first use \code{\link{classify)} (see examples below).
Detect patches (clumps). Patches are groups of cells that are surrounded by cells that are \code{NA}. Set \code{zeroAsNA} to \code{TRUE} to also identify patches separated by cells with values of zero.
}

\usage{
\S4method{patches}{SpatRaster}(x, directions=4, filename="", ...)
\S4method{patches}{SpatRaster}(x, directions=4, zeroAsNA=FALSE, filename="", ...)
}

\arguments{
\item{x}{SpatRaster}
\item{directions}{integer indicating which cells are considered adjacent. Should be 8 (Queen's case) or 4 (Rook's case)}
\item{zeroAsNA}{logical. If \code{TRUE} treat cells that are zero as if they were \code{NA}}
\item{filename}{character. Output filename}
\item{...}{options for writing files as in \code{\link{writeRaster}}}
}
Expand All @@ -41,15 +42,16 @@ r[4, 4] <- 10
r[5, 5:8] <- 12
r[6, 6:9] <- 12

# remove zeros if need be
r <- classify(r, cbind(0, NA))
# remove zeros if need be with zeroAsNA

p4 <- patches(r)
p8 <- patches(r, 8)
p4 <- patches(r, zeroAsNA=TRUE)
p8 <- patches(r, 8, zeroAsNA=TRUE)

# patches for different values
# remove zeros
rr <- classify(r, cbind(0, NA))
# first make layers for each value
s <- separate(r, keep=TRUE, other=NA)
s <- separate(rr, keep=TRUE, other=NA)
p <- patches(s)

}
Expand Down
8 changes: 5 additions & 3 deletions src/clump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void broom_clumps(std::vector<double> &v, std::vector<double>& above, const size



SpatRaster SpatRaster::clumps(int directions, SpatOptions &opt) {
SpatRaster SpatRaster::clumps(int directions, bool zeroAsNA, SpatOptions &opt) {

SpatRaster out = geometry(1);
if (nlyr() > 1) {
Expand All @@ -168,7 +168,7 @@ SpatRaster SpatRaster::clumps(int directions, SpatOptions &opt) {
for (size_t i=0; i<nlyr(); i++) {
std::vector<unsigned> lyr = {(unsigned)i};
SpatRaster x = subset(lyr, ops);
x = x.clumps(directions, ops);
x = x.clumps(directions, zeroAsNA, ops);
out.addSource(x);
}
if (filename != "") {
Expand Down Expand Up @@ -212,13 +212,15 @@ SpatRaster SpatRaster::clumps(int directions, SpatOptions &opt) {
std::vector<std::vector<size_t>> rcl(2);
for (size_t i = 0; i < out.bs.n; i++) {
v = readBlock(out.bs, i);
if (zeroAsNA) {
std::replace(v.begin(), v.end(), 0.0, NAN);
}
broom_clumps(v, above, directions, ncps, out.bs.nrows[i], nc, rcl);
if (!out.writeValues(v, out.bs.row[i], out.bs.nrows[i], 0, nc)) return out;
}
out.writeStop();
readStop();


opt.set_filenames({filename});
if (rcl[0].size() > 0) {
//for (size_t i=0; i<rcl[0].size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/spatRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ class SpatRaster {
SpatRaster disaggregate(std::vector<unsigned> fact, SpatOptions &opt);
SpatRaster distance(SpatOptions &opt);
SpatRaster distance(SpatVector p, SpatOptions &opt);
SpatRaster clumps(int directions, SpatOptions &opt);
SpatRaster clumps(int directions, bool zeroAsNA, SpatOptions &opt);

SpatRaster edges(bool classes, std::string type, unsigned directions, SpatOptions &opt);
SpatRaster extend(SpatExtent e, SpatOptions &opt);
Expand Down

0 comments on commit cd4c35c

Please sign in to comment.