Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to get '.vrt' sources? #1261

Closed
dfriend21 opened this issue Aug 24, 2023 · 2 comments
Closed

Function to get '.vrt' sources? #1261

dfriend21 opened this issue Aug 24, 2023 · 2 comments

Comments

@dfriend21
Copy link
Contributor

It'd be nice if there were a function to get the sources for a '.vrt' file. Using sources() returns the '.vrt' file itself, as expected, but I'd also like to be able to get the files the '.vrt' points to. For now I've made a function that searches the XML and retrieves the file names, which works fine, but it'd be nice if there were a function for this in terra.

library(terra)
#> terra 1.7.39

r <- rast(system.file("ex/elev.tif", package = "terra"))
r_vrt <- vrt(makeTiles(r, 50, tempfile(fileext = ".tif")))

vrt_sources <- function(r_vrt){
  vrt_xml <- xml2::read_xml(sources(r_vrt))
  vrt_path_nodes <- xml2::xml_find_all(vrt_xml, ".//SourceFilename")
  relativeToVRT <- xml2::xml_attr(vrt_path_nodes, "relativeToVRT")
  relativeToVRT[is.na(relativeToVRT)] <- "0"
  is_relative <- relativeToVRT == "1"
  vrt_paths <- xml2::xml_text(vrt_path_nodes)
  vrt_paths[is_relative] <- paste0(dirname(sources(r)), "/", vrt_paths[is_relative])
  return(vrt_paths)
}
vrt_sources(r_vrt)

Created on 2023-08-24 with reprex v2.0.2

@rhijmans
Copy link
Member

I have cobbled something together:

terra::vrt_tiles(r_vrt)
#[1] "c:\\temp\\Rtmp4gamPX\\file5ffc75b233e21.tif"
#[2] "c:\\temp\\Rtmp4gamPX\\file5ffc75b233e22.tif"
#[3] "c:\\temp\\Rtmp4gamPX\\file5ffc75b233e23.tif"
#[4] "c:\\temp\\Rtmp4gamPX\\file5ffc75b233e24.tif"

@dfriend21
Copy link
Contributor Author

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants