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

replace core GDAL functionality with {gdalraster} #19

Open
brownag opened this issue Mar 5, 2024 · 1 comment
Open

replace core GDAL functionality with {gdalraster} #19

brownag opened this issue Mar 5, 2024 · 1 comment

Comments

@brownag
Copy link
Owner

brownag commented Mar 5, 2024

I recently added some updates to use {vapour} for things {terra} could not directly provide.

It may be worth going even closer to the source with {gdalraster}. This change be pending the addition of the OGR vector-related bindings https://usdaforestservice.github.io/gdalraster/articles/gdalvector-draft.html. Since I have several things to address between now and the next release, there is some time to see how this develops, and perhaps make suggestions for anything missing from {gdalraster}

@brownag
Copy link
Owner Author

brownag commented Apr 29, 2024

Currently {vapour} is used to detect drivers needed for reading data from arbitrary sources for input into new GeoPackage. Basically it is used so the user does not need to specify a different argument or function to add vector, raster, and attribute data to a geopackage.

{gdalraster} does not directly provide the ability to load a GDALDataset for arbitrary DSN, but both GDALRaster and GDALVector encapsulate GDALDataset which can be used to get the driver short name used to read a DSN. The following works with the current development "gdalvector" branch (v0.10.9110), but requires trying to load a GDALRaster first. If GDALRaster fails, GDALVector is attempted. If GDALVector fails, error message.

.gdal_dataset_from_path <- function(x) {
  stopifnot(requireNamespace("gdalraster"))
  sapply(x, function(xx) {
    res <- try(new(gdalraster::GDALRaster, xx), silent = TRUE)
    if (inherits(res, 'try-error')) {
        res <- try(new(gdalraster::GDALVector, xx), silent = TRUE)
    }
    if (inherits(res, 'try-error')) {
      stop("opening data source ", shQuote(xx), " failed", call. = FALSE)
    }
    res
  })
}

#' Detect GDAL Drivers from Data Source Name
#' @param x character. Vector of data source names.
#' @return character. Driver short names detected from file extension of `x`
#' @export
gpkg_gdal_detect_drivers <- function(x) {
  stopifnot(requireNamespace("gdalraster"))
  sapply(.gdal_dataset_from_path(x), function(xx) {
    xx$getDriverShortName()
  })
}

gpkg_gdal_detect_drivers(c(
  system.file("extdata", "LF20_EVC_220.csv", package = "gdalraster"),
  system.file("extdata", "storml_evc.tif", package = "gdalraster"),
  system.file("extdata", "ynp_fires_1984_2022.gpkg", package = "gdalraster")
))
#> Loading required namespace: gdalraster
#>         /home/andrew/R/x86_64-pc-linux-gnu-library/4.3/gdalraster/extdata/LF20_EVC_220.csv 
#>                                                                                      "CSV" 
#>           /home/andrew/R/x86_64-pc-linux-gnu-library/4.3/gdalraster/extdata/storml_evc.tif 
#>                                                                                    "GTiff" 
#> /home/andrew/R/x86_64-pc-linux-gnu-library/4.3/gdalraster/extdata/ynp_fires_1984_2022.gpkg 
#>                                                                                     "GPKG"

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

1 participant