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

Overhead in vrt() #1258

Closed
kadyb opened this issue Aug 11, 2023 · 4 comments
Closed

Overhead in vrt() #1258

kadyb opened this issue Aug 11, 2023 · 4 comments

Comments

@kadyb
Copy link
Contributor

kadyb commented Aug 11, 2023

Maybe there is room for optimizing the vrt() function?

library("sf")
library("terra") # 1.7.39
gdal() # 3.6.2

n = 4000
r = rast(ncols = n, nrows = n, vals = rnorm(n ^ 2))
filename = tempfile(fileext = "_.tif")
ff = makeTiles(r, 100, filename) # 1600 tiles
vrt = tempfile(fileext = ".vrt")

system.time({ terra::vrt(ff, filename = vrt) })
#> user  system elapsed 
#> 8.66    7.55   16.21

system.time({ sf::gdal_utils(util = "buildvrt", source = ff, destination = vrt) })
#> user  system elapsed
#> 2.54    2.84    5.39
@dfriend21
Copy link
Contributor

I've also noticed that reading in a ".vrt" that's already been created and that references many rasters takes a while - at least it takes longer than I expect, although I'm not familiar with what's going on under the hood, so I'm not sure what a reasonable expectation is.

library(terra)
#> terra 1.7.39

r <- rast(system.file("ex/elev.tif", package = "terra"))
paths <- makeTiles(r, 3, tempfile(fileext = ".tif"))
length(paths)
#> [1] 960
system.time(r_vrt <- vrt(paths))
#>    user  system elapsed 
#>    3.44    2.66    6.36
system.time(rast(sources(r_vrt)))
#>    user  system elapsed 
#>    2.11    1.65    4.06

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

@rhijmans
Copy link
Member

I think the difference is because terra::vrt returns a SpatRaster:

system.time(x <- terra::vrt(ff, filename = vrt) )
#   user  system elapsed 
#  14.94   21.25   36.25 

Which is about the same duration as

system.time({ sf::gdal_utils(util = "buildvrt", source = ff, destination = vrt) })
#   user  system elapsed 
#   4.94    7.47   12.44 
system.time(y <- terra::rast(vrt) )
#   user  system elapsed 
#   9.47   15.17   24.64 

There could be an option to only create the .vrt file and not return a SpatRaster.

rhijmans added a commit that referenced this issue Aug 26, 2023
@rhijmans
Copy link
Member

You can now request that the filename is returned instead of a SpatRaster:

f <- terra::vrt(ff, filename = vrt, return_filename=T)

@kadyb
Copy link
Contributor Author

kadyb commented Aug 27, 2023

Thanks! Now I get:

system.time({ terra::vrt(ff, filename = vrt) })
#> user  system elapsed 
#> 8.17    7.70   15.87

system.time({ terra::vrt(ff, filename = vrt, return_filename = TRUE) })
#> user  system elapsed 
#> 3.30    2.45    5.75

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

3 participants