From 14f1585d9cf8c2e78b893244f1ccde892ad452c8 Mon Sep 17 00:00:00 2001 From: rhijmans Date: Sat, 26 Aug 2023 14:53:14 -0700 Subject: [PATCH] fixes #1261 --- NAMESPACE | 2 +- R/tiles.R | 25 +++++++++++++++++++++++++ man/vrt.Rd | 8 ++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 1bd634d55..fade18172 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/tiles.R b/R/tiles.R index b9b61e4b6..b645a8e07 100644 --- a/R/tiles.R +++ b/R/tiles.R @@ -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) +} + diff --git a/man/vrt.Rd b/man/vrt.Rd index c858c90ca..c5425624f 100644 --- a/man/vrt.Rd +++ b/man/vrt.Rd @@ -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?}