Skip to content

Commit bce663a

Browse files
committed
read_VECT(): allow missing layer arg with GDAL-GRASS driver *
vect() supports a missing layer argument, so supporting this too for GDAL-GRASS driver. Also, for efficiency reasons, this commit only requests layer names in case of polygons, to work around OSGeo/gdal-grass#46.
1 parent 0bc303c commit bce663a

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

R/vect_link_ng.R

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) 2022 Roger S. Bivand
33
#
44
read_VECT <- function(
5-
vname, layer, proxy = FALSE, use_gdal_grass_driver = TRUE, type = NULL,
5+
vname, layer = "", proxy = FALSE, use_gdal_grass_driver = TRUE, type = NULL,
66
flags = "overwrite", ignore.stderr = get.ignore.stderrOption()) {
77
if (!(requireNamespace("terra", quietly = TRUE))) {
88
stop("terra required for SpatVector output")
@@ -12,6 +12,7 @@ read_VECT <- function(
1212
if (get.suppressEchoCmdInFuncOption()) {
1313
inEchoCmd <- set.echoCmdOption(FALSE)
1414
}
15+
stopifnot(length(layer) == 1L)
1516
if (!missing(layer)) layer <- as.character(layer)
1617
vinfo <- vInfo(vname)
1718
types <- names(vinfo)[which(vinfo > 0)]
@@ -31,31 +32,24 @@ read_VECT <- function(
3132
ignore.stderr = ignore.stderr
3233
)
3334
has_grassraster_drv <- gdal_has_grassraster_driver()
35+
3436
if (has_grassraster_drv && use_gdal_grass_driver) {
3537
args <- list(name = vca[1], type = "vector")
3638
if (length(vca) == 2L) args <- c(args, mapset = vca[2])
3739
tf <- do.call(generate_header_path, args)
38-
layers <- terra::vector_layers(tf)
39-
if (missing(layer)) {
40-
# Set index as 1 and remove this condition once GDAL-GRASS driver issue
40+
if (layer == "" && type == "area") {
41+
layers <- terra::vector_layers(tf)
42+
# Remove this condition once GDAL-GRASS driver issue
4143
# has been solved (https://github.com/OSGeo/gdal-grass/issues/46).
4244
# Then also move the type assignment code (from vInfo) to the
4345
# v.out.ogr case, where it is used as an argument
44-
index <- ifelse(type == "area", 2, 1)
45-
layer <- layers[index]
46-
} else if (!(layer %in% layers)) {
47-
stop(
48-
"Layer ",
49-
layer,
50-
" not found. Available layers: ",
51-
paste(layers, collapse = ", ")
52-
)
46+
layer <- layers[2]
5347
}
5448
# message("Reading ", tf, " (layer ", layer, ")")
5549
res <- getMethod("vect", "character")(tf, layer, proxy = proxy)
5650

5751
} else {
58-
if (missing(layer)) layer <- "1"
52+
if (layer == "") layer <- "1"
5953
tf <- tempfile(fileext = ".gpkg")
6054
execGRASS("v.out.ogr",
6155
flags = flags, input = vname, type = type,

0 commit comments

Comments
 (0)