Skip to content

Commit

Permalink
fixes #1244 #1258
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Aug 26, 2023
1 parent 96de18d commit 8fb1244
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
16 changes: 14 additions & 2 deletions R/tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,26 @@ setMethod("makeTiles", signature(x="SpatRaster"),


setMethod("vrt", signature(x="character"),
function(x, filename="", options=NULL, overwrite=FALSE) {
function(x, filename="", options=NULL, overwrite=FALSE, set_names=FALSE, return_filename=FALSE) {
opt <- spatOptions(filename, overwrite=overwrite)
r <- rast()
if (is.null(options)) {
options=""[0]
}
r@pnt <- r@pnt$make_vrt(x, options, opt)
f <- r@pnt$make_vrt(x, options, opt)
messages(r, "vrt")
if (set_names) {
v <- readLines(f)
nms <- names(rast(x[1]))
i <- grep("band=", v)
if (length(i) == length(nms)) {
nms <- paste0("<Description>", nms, "</Description>")
v[i] <- paste(v[i], nms)
writeLines(v, f)
}
}
if (return_filename) return(f)
rast(f)
}
)

7 changes: 5 additions & 2 deletions man/vrt.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
\title{Virtual Raster Dataset}

\description{
Create a Virtual Raster Dataset (VRT) from a collection of file-based raster datasets (tiles).
Create a Virtual Raster Dataset (VRT) from a collection of file-based raster datasets (tiles). See
\href{https://gdal.org/programs/gdalbuildvrt.html}{gdalbuildvrt} for details.
}

\usage{
\S4method{vrt}{character}(x, filename="", options=NULL, overwrite=FALSE)
\S4method{vrt}{character}(x, filename="", options=NULL, overwrite=FALSE, set_names=FALSE, return_filename=FALSE)
}

\arguments{
\item{x}{character. Filenames of raster "tiles". See \code{\link{tiles}}}
\item{filename}{character. Output VRT filename}
\item{options}{character. All arguments as separate vector elements. Options as for \href{https://gdal.org/programs/gdalbuildvrt.html}{gdalbuildvrt}}
\item{overwrite}{logical. Should \code{filename} be overwritten if it exists?}
\item{set_names}{logical. Add the layer names of the first tile to the vrt?}
\item{return_filename}{logical. If \code{TRUE} the filename is returned, otherwise a SpatRaster is retunred}

This comment has been minimized.

Copy link
@kadyb

kadyb Aug 27, 2023

Contributor

retunred

Small typo (should be "returned").

}

\value{
Expand Down
28 changes: 11 additions & 17 deletions src/gdalio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,9 @@ std::vector<std::vector<std::string>> sdinfo(std::string fname) {

#if GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR < 1

SpatRaster SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt) {
SpatRaster out;
out.setError( "GDAL version >= 2.1 required for vrt");
return out;
std::string SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt) {
setError( "GDAL version >= 2.1 required for vrt");
return("");
}


Expand All @@ -328,15 +327,14 @@ std::string gdalinfo(std::string filename, std::vector<std::string> options, std

# include "gdal_utils.h" // requires >= 2.1

SpatRaster SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt) {
std::string SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt) {

SpatRaster out;
std::string outfile = opt.get_filename();
if (outfile.empty()) {
outfile = tempFile(opt.get_tempdir(), opt.pid, ".vrt");
} else if (file_exists(outfile) && (!opt.get_overwrite())) {
out.setError("output file exists. You can use 'overwrite=TRUE' to overwrite it");
return(out);
setError("output file exists. You can use 'overwrite=TRUE' to overwrite it");
return("");
}

/*
Expand Down Expand Up @@ -377,9 +375,9 @@ SpatRaster SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<
std::vector <char *> vops = string_to_charpnt(options);
GDALBuildVRTOptions* vrtops = GDALBuildVRTOptionsNew(vops.data(), NULL);
if (vrtops == NULL) {
out.setError("options error");
setError("options error");
CSLDestroy( names );
return(out);
return("");
}
int pbUsageError;
// GDALDataset *ds = (GDALDataset *) GDALBuildVRT(outfile.c_str(), tiles.size(), (GDALDatasetH *) tiles.data(), nullptr, vrtops, &pbUsageError);
Expand All @@ -391,15 +389,11 @@ SpatRaster SpatRaster::make_vrt(std::vector<std::string> filenames, std::vector<

//for (size_t i= 0; i<tiles.size(); i++) GDALClose(tiles[i]);
if(ds == NULL ) {
out.setError("cannot create vrt. Error #"+ std::to_string(pbUsageError));
return out;
setError("cannot create vrt. Error #"+ std::to_string(pbUsageError));
return("");
}
GDALClose(ds);
if (!out.constructFromFile(outfile, {-1}, {""}, {}, {})) {
out.setError("cannot open created vrt");
return out;
}
return out;
return(outfile);
}


Expand Down
2 changes: 1 addition & 1 deletion src/spatRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ class SpatRaster {
//bool writeValues2(std::vector<std::vector<double>> &vals, size_t startrow, size_t nrows);
bool writeStop();
bool writeHDR(std::string filename);
SpatRaster make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt);
std::string make_vrt(std::vector<std::string> filenames, std::vector<std::string> options, SpatOptions &opt);
bool write_aux_json(std::string filename);

//bool writeStartGDAL(std::string filename, std::string driver, std::string datatype, bool overwrite, SpatOptions &opt);
Expand Down

0 comments on commit 8fb1244

Please sign in to comment.