Skip to content

Commit

Permalink
Implement GDAL driver detection via {vapour} for #15
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Mar 2, 2024
1 parent 69dcb8b commit 37021a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gpkg
Type: Package
Title: Utilities for the Open Geospatial Consortium 'GeoPackage' Format
Version: 0.0.8
Version: 0.0.9
Authors@R: person(given="Andrew", family="Brown", email="[email protected]", role = c("aut", "cre"))
Maintainer: Andrew Brown <[email protected]>
Description: Build Open Geospatial Consortium 'GeoPackage' files (<https://www.geopackage.org/>). 'GDAL' utilities for reading and writing spatial data are provided by the 'terra' package. Additional 'GeoPackage' and 'SQLite' features for attributes and tabular data are implemented with the 'RSQLite' package.
Expand All @@ -14,14 +14,15 @@ Imports:
Suggests:
RSQLite,
terra (>= 1.6),
vapour,
tinytest,
dplyr,
dbplyr,
knitr,
rmarkdown
License: CC0
Depends: R (>= 3.1.0)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
LazyData: true
Expand Down
49 changes: 43 additions & 6 deletions R/gpkg-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,57 @@ gpkg_write <- function(x,
}

.gpkg_process_sources <- function(x, ...) {

if (!is.list(x) || is.data.frame(x)) {
x <- list(x)
}

# TODO: extend this; only intended for prototyping before general sln

# objects with a file-based
# objects with a file source
src_raster <- vapply(x, inherits, logical(1), c('SpatRaster', 'SpatRasterCollection'))
src_vector <- vapply(x, inherits, logical(1), 'SpatVectorProxy')
obj_vector <- vapply(x, inherits, logical(1), c('sf', 'SpatVector'))
obj_attrib <- vapply(x, inherits, logical(1), 'data.frame')
pth_raster <- vapply(x, .is.file, logical(1), "tif+|vrt|grd|png")
pth_vector <- vapply(x, .is.file, logical(1), "shp|gpkg")
pth_attrib <- vapply(x, .is.file, logical(1), "csv")

# pth_raster <- vapply(x, .is.file, logical(1), "tif+|vrt|grd|png")
# pth_vector <- vapply(x, .is.file, logical(1), "shp|gpkg")
# pth_attrib <- vapply(x, .is.file, logical(1), "csv")
pth_file <- vapply(x, .is.file, logical(1), ".*")

# TODO: gdal is not used to read attributes,
# provide support for some other tabular data formats?
# arrow? openxlsx?
pth_attrib <- pth_file & vapply(x, .is.file, logical(1), "csv")
pth_raster <- rep(FALSE, length(x))
pth_vector <- rep(FALSE, length(x))

if (any(pth_file)) {
if (!requireNamespace("vapour")) {
stop("package 'vapour' is required to auto-detect GDAL drivers needed to read from arbitrary file paths", call. = FALSE)

Check warning on line 130 in R/gpkg-io.R

View check run for this annotation

Codecov / codecov/patch

R/gpkg-io.R#L130

Added line #L130 was not covered by tests
}

gdal_drv <- vapply(x, function(y) {
if (!is.character(y)) {
""
} else
vapour::vapour_driver(y)
}, character(1))

drv <- vapour::vapour_all_drivers()
drm <- match(gdal_drv, drv$driver)


pth_raster <- pth_file & drv$raster[drm]

# TODO: how to handle GPKG as a raster and vector source?
pth_raster[gdal_drv == "GPKG"] <- FALSE

pth_vector <- pth_file & drv$vector[drm]

# TODO: handling of CSV files as attributes/without GDAL
# filter vapour drivers to subset that terra can readwrite
pth_vector[gdal_drv == "CSV"] <- FALSE
}


# classify list of object input grid, features, attributes
# - each processing function handles local objects and/or file paths
Expand Down

0 comments on commit 37021a0

Please sign in to comment.