diff --git a/R/RcppExports.R b/R/RcppExports.R index 57a366b3c..3d8d0aa2b 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -117,8 +117,8 @@ CPL_gdalinfo <- function(obj, options, oo, co) { .Call(`_sf_CPL_gdalinfo`, obj, options, oo, co) } -CPL_ogrinfo <- function(obj, options, oo, co) { - .Call(`_sf_CPL_ogrinfo`, obj, options, oo, co) +CPL_ogrinfo <- function(obj, options, oo, co, read_only = FALSE) { + .Call(`_sf_CPL_ogrinfo`, obj, options, oo, co, read_only) } CPL_gdaladdo <- function(obj, method, overviews, bands, oo, co, clean = FALSE, read_only = FALSE) { diff --git a/R/gdal_utils.R b/R/gdal_utils.R index 36e8a827f..6fa1596df 100644 --- a/R/gdal_utils.R +++ b/R/gdal_utils.R @@ -31,6 +31,7 @@ resampling_method = function(option = "near") { #' @param quiet logical; if \code{TRUE}, suppress printing the output for \code{info} and \code{mdiminfo}, and suppress printing progress #' @param processing character; processing options for \code{demprocessing} #' @param colorfilename character; name of color file for \code{demprocessing} (mandatory if \code{processing="color-relief"}) +#' @param read_only logical; only for `ogrinfo`: if `TRUE`, source is opened in read-only mode #' @return \code{info} returns a character vector with the raster metadata; all other utils return (invisibly) a logical indicating success (i.e., \code{TRUE}); in case of failure, an error is raised. #' @export #' @seealso \link{gdal_addo} for adding overlays to a raster file; \link{st_layers} to query geometry type(s) and crs from layers in a (vector) data source @@ -76,7 +77,7 @@ gdal_utils = function(util = "info", source, destination, options = character(0) quiet = !(util %in% c("info", "gdalinfo", "ogrinfo", "vectorinfo", "mdiminfo")) || ("-multi" %in% options), processing = character(0), colorfilename = character(0), - config_options = character(0)) { + config_options = character(0), read_only = FALSE) { stopifnot(is.character(options), is.character(config_options)) if (!quiet && "-multi" %in% options) @@ -103,7 +104,8 @@ gdal_utils = function(util = "info", source, destination, options = character(0) ret = switch(util, gdalinfo =, info = CPL_gdalinfo(if (missing(source)) character(0) else source, options, oo, config_options), - vectorinfo =, ogrinfo = CPL_ogrinfo(if (missing(source)) character(0) else source, options, oo, config_options), + vectorinfo =, ogrinfo = CPL_ogrinfo(if (missing(source)) character(0) else source, options, oo, config_options, + isTRUE(read_only) || "-ro" %in% options), warp = CPL_gdalwarp(source, destination, options, oo, doo, config_options, quiet, "-overwrite" %in% options), warper = CPL_gdal_warper(source, destination, as.integer(resampling_method(options)), oo, doo, config_options, quiet), # nocov diff --git a/man/gdal_utils.Rd b/man/gdal_utils.Rd index f8cfef2e0..370392d31 100644 --- a/man/gdal_utils.Rd +++ b/man/gdal_utils.Rd @@ -13,7 +13,8 @@ gdal_utils( ("-multi" \%in\% options), processing = character(0), colorfilename = character(0), - config_options = character(0) + config_options = character(0), + read_only = FALSE ) } \arguments{ @@ -32,6 +33,8 @@ gdal_utils( \item{colorfilename}{character; name of color file for \code{demprocessing} (mandatory if \code{processing="color-relief"})} \item{config_options}{named character vector with GDAL config options, like \code{c(option1=value1, option2=value2)}} + +\item{read_only}{logical; only for \code{ogrinfo}: if \code{TRUE}, source is opened in read-only mode} } \value{ \code{info} returns a character vector with the raster metadata; all other utils return (invisibly) a logical indicating success (i.e., \code{TRUE}); in case of failure, an error is raised. diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 039d37e11..8dac5ea11 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -373,8 +373,8 @@ BEGIN_RCPP END_RCPP } // CPL_ogrinfo -Rcpp::CharacterVector CPL_ogrinfo(Rcpp::CharacterVector obj, Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector co); -RcppExport SEXP _sf_CPL_ogrinfo(SEXP objSEXP, SEXP optionsSEXP, SEXP ooSEXP, SEXP coSEXP) { +Rcpp::CharacterVector CPL_ogrinfo(Rcpp::CharacterVector obj, Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector co, bool read_only); +RcppExport SEXP _sf_CPL_ogrinfo(SEXP objSEXP, SEXP optionsSEXP, SEXP ooSEXP, SEXP coSEXP, SEXP read_onlySEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -382,7 +382,8 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type options(optionsSEXP); Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type oo(ooSEXP); Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type co(coSEXP); - rcpp_result_gen = Rcpp::wrap(CPL_ogrinfo(obj, options, oo, co)); + Rcpp::traits::input_parameter< bool >::type read_only(read_onlySEXP); + rcpp_result_gen = Rcpp::wrap(CPL_ogrinfo(obj, options, oo, co, read_only)); return rcpp_result_gen; END_RCPP } @@ -1510,7 +1511,7 @@ static const R_CallMethodDef CallEntries[] = { {"_sf_CPL_read_ogr", (DL_FUNC) &_sf_CPL_read_ogr, 14}, {"_sf_CPL_read_gdal_stream", (DL_FUNC) &_sf_CPL_read_gdal_stream, 12}, {"_sf_CPL_gdalinfo", (DL_FUNC) &_sf_CPL_gdalinfo, 4}, - {"_sf_CPL_ogrinfo", (DL_FUNC) &_sf_CPL_ogrinfo, 4}, + {"_sf_CPL_ogrinfo", (DL_FUNC) &_sf_CPL_ogrinfo, 5}, {"_sf_CPL_gdaladdo", (DL_FUNC) &_sf_CPL_gdaladdo, 8}, {"_sf_CPL_gdalwarp", (DL_FUNC) &_sf_CPL_gdalwarp, 8}, {"_sf_CPL_gdalrasterize", (DL_FUNC) &_sf_CPL_gdalrasterize, 8}, diff --git a/src/gdal_utils.cpp b/src/gdal_utils.cpp index 080e8f1f1..16f2a26b7 100644 --- a/src/gdal_utils.cpp +++ b/src/gdal_utils.cpp @@ -70,14 +70,14 @@ Rcpp::CharacterVector CPL_gdalinfo(Rcpp::CharacterVector obj, Rcpp::CharacterVec // [[Rcpp::export]] Rcpp::CharacterVector CPL_ogrinfo(Rcpp::CharacterVector obj, Rcpp::CharacterVector options, - Rcpp::CharacterVector oo, Rcpp::CharacterVector co) { + Rcpp::CharacterVector oo, Rcpp::CharacterVector co, bool read_only = false) { set_config_options(co); std::vector options_char = create_options(options, true); std::vector oo_char = create_options(oo, true); // open options GDALDatasetH ds = NULL; #if GDAL_VERSION_NUM >= 3070000 if (obj.size()) - ds = GDALOpenEx((const char *) obj[0], GA_Update, NULL, oo_char.data(), NULL); + ds = GDALOpenEx((const char *) obj[0], read_only ? GA_ReadOnly : GA_Update, NULL, oo_char.data(), NULL); GDALVectorInfoOptions* opt = GDALVectorInfoOptionsNew(options_char.data(), NULL); char *ret_val = GDALVectorInfo(ds, opt); if (ret_val == NULL)