brownag / gpkg

Utilities for the Open Geospatial Consortium (OGC) 'GeoPackage' Format in R
http://humus.rocks/gpkg/
Creative Commons Zero v1.0 Universal
18 stars 0 forks source link

replace core GDAL functionality with {gdalraster} #19

Open brownag opened 6 months ago

brownag commented 6 months ago

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 commented 4 months ago

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"