opengeos / whiteboxR

WhiteboxTools R Frontend
https://whiteboxR.gishub.org
Other
172 stars 30 forks source link

Extend path expansion to multi-file inputs and tool `wd` argument #78

Closed brownag closed 2 years ago

brownag commented 2 years ago

This PR implements several new places where path expansion and file-path-helpers via wbt_file_path() can be applied

library(whitebox)
library(terra)
library(elevatr)
wbt_verbose(TRUE)

if (!dir.exists("~/wbtwdtest")) 
  dir.create("~/wbtwdtest")
setwd("~/wbtwdtest")
wbt_wd("")
#> Reset WhiteboxTools working directory to current R working directory: /home/andrew/wbtwdtestUnset WhiteboxTools working directory flag `whitebox.wd` / `--wd`

dir.create("Input")
dir.create("Output")

# sample tile locations
xy <- data.frame(x = -seq(119, 120, 0.5), y =   37)

# download tiles
dat <- lapply(1:nrow(xy), function(i) {
  # need two points/tile to use get_elev_raster()
  # add +0.0001 to x coord to duplicate
  setNames(terra::rast(elevatr::get_elev_raster(rbind(xy[i,], transform(xy[i,], x = x + 0.0001)), 
                                                prj = "EPSG:4326", z = 10)), 
           nm = "Elevation")
})

# write sample tiles to file in Input folder
tiles <- lapply(seq_along(dat), function(i) { 
  writeRaster(dat[[i]], sprintf("Input/test%s.tif", i), overwrite = TRUE)
})

# now this works! (all relative paths with semicolon separator)
wbt_mosaic(output = "Output/foo.tif", inputs = "Input/test1.tif;Input/test2.tif;Input/test3.tif")
#> mosaic - Elapsed Time (including I/O): 0.97s
library(whitebox)
library(terra)
library(elevatr)
wbt_verbose(TRUE)

if (!dir.exists("~/wbtwdtest")) 
  dir.create("~/wbtwdtest")
dir.create("~/wbtwdtest/Input")
dir.create("~/wbtwdtest/Output")

# sample tile locations
xy <- data.frame(x = -seq(119, 120, 0.5), y =   37)

# download tiles
dat <- lapply(1:nrow(xy), function(i) {
  # need two points/tile to use get_elev_raster()
  # add +0.0001 to x coord to duplicate
  setNames(terra::rast(elevatr::get_elev_raster(rbind(xy[i,], transform(xy[i,], x = x + 0.0001)), 
                                                prj = "EPSG:4326", z = 10)), 
           nm = "Elevation")
})

# write sample tiles to file in Input folder
tiles <- lapply(seq_along(dat), function(i) { 
  writeRaster(dat[[i]], sprintf("~/wbtwdtest/Input/test%s.tif", i), overwrite = TRUE)
})

# path expansion in semicolon separated list!
wbt_mosaic(output = "~/wbtwdtest/Output/foo.tif", inputs = "~/wbtwdtest/Input/test1.tif;~/wbtwdtest/Input/test2.tif;~/wbtwdtest/Input/test3.tif")
#> mosaic - Elapsed Time (including I/O): 0.83s