Skip to content

Commit 9cac146

Browse files
committed
for #1929
1 parent 7cb6e5e commit 9cac146

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

R/distance.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# License GPL v3
55

66
setMethod("buffer", signature(x="SpatRaster"),
7-
function(x, width, background=0, filename="", ...) {
7+
function(x, width, background=0, include=TRUE, filename="", ...) {
88
opt <- spatOptions(filename, ...)
9-
x@pntr <- x@pntr$buffer(width, background, opt)
9+
x@pntr <- x@pntr$buffer(width, background, include, opt)
1010
messages(x, "buffer")
1111
}
1212
)

man/buffer.Rd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Note that the distance unit of the buffer \code{width} parameter is meters if th
1616
}
1717

1818
\usage{
19-
\S4method{buffer}{SpatRaster}(x, width, background=0, filename="", ...)
19+
\S4method{buffer}{SpatRaster}(x, width, background=0, include=TRUE, filename="", ...)
2020

2121
\S4method{buffer}{SpatVector}(x, width, quadsegs=10, capstyle="round",
2222
joinstyle="round", mitrelimit=NA, singlesided=FALSE)
@@ -28,6 +28,7 @@ Note that the distance unit of the buffer \code{width} parameter is meters if th
2828
\item{filename}{character. Output filename}
2929
\item{...}{additional arguments for writing files as in \code{\link{writeRaster}}}
3030
\item{background}{numeric. value to assign to cells outside the buffer. If this value is zero or FALSE, a boolean SpatRaster is returned}
31+
\item{include}{logical. If \code{TRUE} the raster cells that are not \code{NA} are included in the buffer. Otherwise these cells get the background value}
3132
\item{quadsegs}{positive integer. Number of line segments to use to draw a quart circle}
3233
\item{capstyle}{character. One of "round", "square" or "flat". Ignored if \code{is.lonlat(x)}}
3334
\item{joinstyle}{character. One of "round", "mitre" or "bevel". Ignored if \code{is.lonlat(x)}}

src/distRaster.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ SpatRaster SpatRaster::edges(bool classes, std::string type, unsigned directions
18781878

18791879

18801880

1881-
SpatRaster SpatRaster::buffer(double d, double background, SpatOptions &opt) {
1881+
SpatRaster SpatRaster::buffer(double d, double background, bool include, SpatOptions &opt) {
18821882

18831883
SpatRaster out = geometry(1);
18841884
if (!hasValues()) {
@@ -1908,7 +1908,7 @@ SpatRaster SpatRaster::buffer(double d, double background, SpatOptions &opt) {
19081908
std::vector<size_t> lyr = {i};
19091909
SpatRaster r = subset(lyr, ops);
19101910
ops.names = {nms[i]};
1911-
r = r.buffer(d, background, ops);
1911+
r = r.buffer(d, background, include, ops);
19121912
out.source[i] = r.source[0];
19131913
}
19141914
if (!opt.get_filename().empty()) {
@@ -1929,6 +1929,9 @@ SpatRaster SpatRaster::buffer(double d, double background, SpatOptions &opt) {
19291929
} else {
19301930
out = proximity(NAN, NAN, false, "", true, d, true, opt);
19311931
}
1932+
if (!include) {
1933+
out = out.mask(*this, true, NAN, NAN, opt);
1934+
}
19321935
} else {
19331936
SpatRaster e = edges(false, "inner", 8, NAN, ops);
19341937
SpatVector p = e.as_points(false, true, false, ops);
@@ -1938,7 +1941,11 @@ SpatRaster SpatRaster::buffer(double d, double background, SpatOptions &opt) {
19381941
if (background == 0) {
19391942
out.setValueType(3);
19401943
}
1941-
out = out.mask(*this, true, NAN, 1, opt);
1944+
if (include) {
1945+
out = out.mask(*this, true, NAN, 1, opt);
1946+
} else {
1947+
out = out.mask(*this, true, NAN, NAN, opt);
1948+
}
19421949
//out = out.disdir_vector_rasterize(p, false, true, false, false, NAN, NAN, "m", ops);
19431950
//out = out.arith(d, "<=", false, opt);
19441951
}

src/spatRaster.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ class SpatRaster {
662662
std::vector<double> bilinearCells(const std::vector<double> &x, const std::vector<double> &y);
663663
void fourCellsFromXY(std::vector<double> &out, const std::vector<double> &x, const std::vector<double> &y);
664664

665-
SpatRaster buffer(double d, double background, SpatOptions &opt);
665+
SpatRaster buffer(double d, double background, bool include, SpatOptions &opt);
666666
SpatRaster clamp(std::vector<double> low, std::vector<double> high, bool usevalue, SpatOptions &opt);
667667
SpatRaster clamp_raster(SpatRaster &x, SpatRaster &y, std::vector<double> low, std::vector<double> high, bool usevalue, SpatOptions &opt);
668668
SpatRaster clamp_ts(bool min, bool max, SpatOptions &opt);

0 commit comments

Comments
 (0)