Skip to content

Commit

Permalink
fixes #1408
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 25, 2024
1 parent 7b18131 commit d557805
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
11 changes: 7 additions & 4 deletions R/tiles.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@

setMethod("makeTiles", signature(x="SpatRaster"),
function(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, overwrite=FALSE, ...) {
function(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, buffer=0, overwrite=FALSE, ...) {
filename <- trimws(filename[1])
filename <- filename[!is.na(filename)]
if (filename == "") error("makeTiles", "filename cannot be empty")
opt <- spatOptions(filename="", overwrite=overwrite, ...)
if (inherits(y, "SpatRaster")) {
ff <- x@cpp$make_tiles(y@cpp, extend[1], na.rm[1], filename, opt)
ff <- x@cpp$make_tiles(y@cpp, extend[1], buffer, na.rm[1], filename, opt)
} else if (inherits(y, "SpatVector")) {
if (!all(buffer == 0)) {
warn("makeTiles", "argument 'buffer' is ignored if y is a SpatVector")
}
ff <- x@cpp$make_tiles_vect(y@cpp, extend[1], na.rm[1], filename, opt)
} else if (is.numeric(y)) {
if (length(y) > 2) {
error("makeTiles", "expected one or two numbers")
}
y <- rep_len(y, 2)
y <- aggregate(rast(x), y)
ff <- x@cpp$make_tiles(y@cpp, extend[1], na.rm[1], filename, opt)
ff <- x@cpp$make_tiles(y@cpp, extend[1], buffer, na.rm[1], filename, opt)
} else {
error("makeTiles", "y must be a SpatRaster or SpatVector")
error("makeTiles", "y must be numeric or a SpatRaster or SpatVector")
}
messages(x, "makeTiles")
ff
Expand Down
3 changes: 2 additions & 1 deletion man/makeTiles.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Divide a SpatRaster into "tiles". The cells of another SpatRaster (normally with
}

\usage{
\S4method{makeTiles}{SpatRaster}(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, overwrite=FALSE, ...)
\S4method{makeTiles}{SpatRaster}(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, buffer=0, overwrite=FALSE, ...)
}

\arguments{
Expand All @@ -24,6 +24,7 @@ Divide a SpatRaster into "tiles". The cells of another SpatRaster (normally with
\item{filename}{character. Output filename template. Filenames will be altered by adding the tile number for each tile}
\item{extend}{logical. If \code{TRUE}, the extent of \code{y} is expanded to assure that it covers all of \code{x}}
\item{na.rm}{logical. If \code{TRUE}, tiles with only missing values are ignored}
\item{buffer}{integer. The number of cells to add around each tile. Ignored if \code{y} is a SpatVector. Can be a single number, or two numbers to specify a separate buffer for the rows and for the columns. This allows for creating overlapping tiles that can be used for computing spatial context dependent values with e.g. \code{\link{focal}}. The expansion is only inside \code{x}, no rows or columns outside of \code{x} are added}
\item{overwrite}{logical. If \code{TRUE}, existing tiles are overwritten; otherwise they are skipped (without error or warning)}
\item{...}{additional arguments for writing files as in \code{\link{writeRaster}}}
}
Expand Down
26 changes: 25 additions & 1 deletion src/raster_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,34 @@ SpatExtent SpatRaster::ext_from_cell(double cell) {
}


std::vector<std::string> SpatRaster::make_tiles(SpatRaster x, bool expand, bool narm, std::string filename, SpatOptions &opt) {
std::vector<std::string> SpatRaster::make_tiles(SpatRaster x, bool expand, std::vector<int> buffer, bool narm, std::string filename, SpatOptions &opt) {

std::vector<std::string> ff;
if (!hasValues()) {
setError("input raster has no values");
return ff;
}


x = x.geometry(1, false, false, false);
SpatExtent e = getExtent();

recycle(buffer, 2);
// if ((buffer[0] < 0) | (buffer[1] < 0)) {
// setError("buffer cannot be negative");
// return ff;
// }
std::vector<double> ebuf = {buffer[0] * xres(), buffer[1] * yres()};

/*
if ((buffer[0] > 0) || (buffer[1] > 0)) {
e.xmin = e.xmin - ebuf[0];
e.xmax = e.xmax + ebuf[0];
e.ymin = e.ymin - ebuf[1];
e.ymax = e.ymax + ebuf[1];
}
*/

SpatOptions ops(opt);
if (expand) {
x = x.extend(e, "out", NAN, ops);
Expand All @@ -145,6 +164,11 @@ std::vector<std::string> SpatRaster::make_tiles(SpatRaster x, bool expand, bool
continue;
}
SpatExtent exi = x.ext_from_cell(i);
exi.xmin = exi.xmin - ebuf[0];
exi.xmax = exi.xmax + ebuf[0];
exi.ymin = exi.ymin - ebuf[1];
exi.ymax = exi.ymax + ebuf[1];

opt.set_filenames({fout});
SpatRaster out = crop(exi, "near", false, opt);
if (out.hasError()) {
Expand Down
3 changes: 2 additions & 1 deletion src/spatRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ class SpatRaster {
SpatExtent ext_from_rc(int_64 r1, int_64 r2, int_64 c1, int_64 c2);
SpatExtent ext_from_cell(double cell);

std::vector<std::string> make_tiles(SpatRaster x, bool expand, bool narm, std::string filename, SpatOptions &opt);
std::vector<std::string> make_tiles(SpatRaster x, bool expand, std::vector<int> buffer, bool narm, std::string filename, SpatOptions &opt);

std::vector<std::string> make_tiles_vect(SpatVector x, bool expand, bool narm, std::string filename, SpatOptions &opt);

SpatRaster mask(SpatRaster &x, bool inverse, double maskvalue, double updatevalue, SpatOptions &opt);
Expand Down

0 comments on commit d557805

Please sign in to comment.