Skip to content

Commit

Permalink
for #1167
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed May 25, 2023
1 parent 24b5503 commit bc44995
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions R/tiles.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

setMethod("makeTiles", signature(x="SpatRaster"),
function(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, ...) {
function(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, overwrite=FALSE, ...) {
filename <- trimws(filename[1])
filename <- filename[!is.na(filename)]
if (filename == "") error("makeTiles", "filename cannot be empty")
opt <- spatOptions(filename="", ...)
opt <- spatOptions(filename="", overwrite=overwrite, ...)
if (inherits(y, "SpatRaster")) {
ff <- x@pnt$make_tiles(y@pnt, extend[1], na.rm[1], filename, opt)
} else if (inherits(y, "SpatVector")) {
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, ...)
\S4method{makeTiles}{SpatRaster}(x, y, filename="tile_.tif", extend=FALSE, na.rm=FALSE, 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{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
12 changes: 11 additions & 1 deletion src/raster_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ std::vector<std::string> SpatRaster::make_tiles(SpatRaster x, bool expand, bool
std::string f = noext(filename);
ff.reserve(d.size());
size_t nl = nlyr();
bool overwrite = opt.get_overwrite();
for (size_t i=0; i<d.size(); i++) {
std::string fout = f + std::to_string(d[i]) + fext;
if (file_exists(fout) && (!overwrite)) {
ff.push_back(fout);
continue;
}
SpatExtent exi = x.ext_from_cell(i);
opt.set_filenames({fout});
SpatRaster out = crop(exi, "near", false, opt);
Expand Down Expand Up @@ -186,11 +191,16 @@ std::vector<std::string> SpatRaster::make_tiles_vect(SpatVector x, bool expand,
std::string f = noext(filename);
ff.reserve(d.size());
size_t nl = nlyr();
bool overwrite = opt.get_overwrite();
for (size_t i=0; i<d.size(); i++) {
SpatExtent exi = x.geoms[i].extent;
std::string fout = f + std::to_string(d[i]) + fext;
if (file_exists(fout) && (!overwrite)) {
ff.push_back(fout);
continue;
}
opt.set_filenames( {fout} );
SpatRaster out;
SpatExtent exi = x.geoms[i].extent;
if (!e.intersects(exi)) continue;
if (expand) {
out = crop(exi, "near", false, ops);
Expand Down

0 comments on commit bc44995

Please sign in to comment.