From db7bf61782eff44eb518d164146c8f0ec5a8b512 Mon Sep 17 00:00:00 2001 From: njtierney Date: Fri, 22 Mar 2024 13:49:45 +1000 Subject: [PATCH] add function to get gdal available driver list - specifying either "vector" or "raster" (currently). This removes a little bit of copied complexity in a few places in the pkg --- R/tar-terra-rast.R | 8 ++------ R/tar-terra-vect.R | 8 ++------ R/utils.R | 7 +++++++ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/R/tar-terra-rast.R b/R/tar-terra-rast.R index 089cf2e..ffd01b1 100644 --- a/R/tar-terra-rast.R +++ b/R/tar-terra-rast.R @@ -67,9 +67,7 @@ tar_terra_rast <- function(name, tidy_eval = tidy_eval ) - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("raster") # if not specified by user, pull the corresponding geotargets option filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") @@ -106,9 +104,7 @@ create_format_terra_raster <- function(filetype, gdal, ...) { check_pkg_installed("terra") - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "raster" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("raster") filetype <- filetype %||% geotargets_option_get("gdal.raster.driver") filetype <- rlang::arg_match0(filetype, drv$name) diff --git a/R/tar-terra-vect.R b/R/tar-terra-vect.R index 8eddcc9..b3441c7 100644 --- a/R/tar-terra-vect.R +++ b/R/tar-terra-vect.R @@ -75,9 +75,7 @@ tar_terra_vect <- function(name, tidy_eval = tidy_eval ) - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("vector") # if not specified by user, pull the corresponding geotargets option filetype <- filetype %||% geotargets_option_get("gdal.vector.driver") @@ -124,9 +122,7 @@ create_format_terra_vect <- function(filetype, options, ...) { check_pkg_installed("terra") - # get list of drivers available for writing depending on what the user's GDAL supports - drv <- terra::gdal(drivers = TRUE) - drv <- drv[drv$type == "vector" & grepl("write", drv$can), ] + drv <- get_gdal_available_driver_list("vector") if (is.null(filetype)) { filetype <- "GeoJSON" diff --git a/R/utils.R b/R/utils.R index 8494296..9d21a94 100644 --- a/R/utils.R +++ b/R/utils.R @@ -13,3 +13,10 @@ check_pkg_installed <- function(pkg, call = rlang::caller_env()) { ) } } + +get_gdal_available_driver_list <- function(driver_type){ + # get list of drivers available for writing depending on what the user's GDAL supports + drv <- terra::gdal(drivers = TRUE) + drv <- drv[drv$type == driver_type & grepl("write", drv$can), ] + drv +}