Skip to content

Commit

Permalink
fixes #1261
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Aug 26, 2023
1 parent 8fb1244 commit 14f1585
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Expand Up @@ -18,5 +18,5 @@ S3method(str, SpatRaster)
S3method(str, SpatExtent)
S3method(str, SpatGraticule)

export(add_legend, focalMat, gdal, getGDALconfig, graticule, halo, setGDALconfig, map.pal, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers)
export(add_legend, focalMat, gdal, getGDALconfig, graticule, halo, setGDALconfig, map.pal, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles)

25 changes: 25 additions & 0 deletions R/tiles.R
Expand Up @@ -68,3 +68,28 @@ setMethod("vrt", signature(x="character"),
}
)


vrt_tiles <- function(x) {
if (inherits(x, "SpatRaster")) {
x <- sources(x)
}
if (!inherits(x, "character")) {
error("vrt_sources", "x must be a filename (character) or SpatRaster)")
}
x <- grep(".vrt$", x, ignore.case =TRUE, value=TRUE)
if (length(x) == 0) {
error("vrt_sources", 'no filenames with extension ".vrt"')
}
tiles <- lapply(x, function(f) {
v <- readLines(f)
v <- v[grep("SourceFilename", v)]
s <- strsplit(v, "\"")
rel <- sapply(s, \(x) x[2])
ff <- strsplit(sapply(s, \(x)x[3]), "<")
ff <- gsub(">", "", sapply(ff, \(x) x[1]))
ff[rel=="1"] <- file.path(dirname(f), ff[rel=="1"])
ff
})
unlist(tiles)
}

8 changes: 6 additions & 2 deletions man/vrt.Rd
Expand Up @@ -4,22 +4,26 @@

\alias{vrt}
\alias{vrt,character-method}

\alias{vrt_tiles}

\title{Virtual Raster Dataset}

\description{
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.

\code{vrt_tiles} returns the filenames of the tiles in a VRT.
}

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

vrt_tiles(filename)
}

\arguments{
\item{x}{character. Filenames of raster "tiles". See \code{\link{tiles}}}
\item{filename}{character. Output VRT filename}
\item{filename}{character. 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?}
Expand Down

0 comments on commit 14f1585

Please sign in to comment.