Skip to content

Commit

Permalink
add config_options to st_write
Browse files Browse the repository at this point in the history
* fixes #1618
  • Loading branch information
edzer committed Mar 31, 2021
1 parent b0e733d commit 7974a4d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ CPL_gdal_warper <- function(infile, outfile, options, oo, doo, quiet = TRUE) {
.Call('_sf_CPL_gdal_warper', PACKAGE = 'sf', infile, outfile, options, oo, doo, quiet)
}

CPL_write_ogr <- function(obj, dsn, layer, driver, dco, lco, geom, dim, fids, quiet, append, delete_dsn = FALSE, delete_layer = FALSE, write_geometries = TRUE) {
.Call('_sf_CPL_write_ogr', PACKAGE = 'sf', obj, dsn, layer, driver, dco, lco, geom, dim, fids, quiet, append, delete_dsn, delete_layer, write_geometries)
CPL_write_ogr <- function(obj, dsn, layer, driver, dco, lco, geom, dim, fids, ConfigOptions, quiet, append, delete_dsn = FALSE, delete_layer = FALSE, write_geometries = TRUE) {
.Call('_sf_CPL_write_ogr', PACKAGE = 'sf', obj, dsn, layer, driver, dco, lco, geom, dim, fids, ConfigOptions, quiet, append, delete_dsn, delete_layer, write_geometries)
}

CPL_geos_binop <- function(sfc0, sfc1, op, par = 0.0, pattern = "", prepared = FALSE) {
Expand Down
7 changes: 4 additions & 3 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ abbreviate_shapefile_names = function(x) {
#' The default for \code{st_write} is \code{FALSE} which raises an error if the layer exists.
#' The default for \code{write_sf} is \code{TRUE}.
#' @param fid_column_name character, name of column with feature IDs; if
#' @param config_options character, named vector with GDAL config options
#' specified, this column is no longer written as feature attribute.
#' @details
#' Columns (variables) of a class not supported are dropped with a warning.
Expand Down Expand Up @@ -394,7 +395,7 @@ st_write.sf = function(obj, dsn, layer = NULL, ...,
driver = guess_driver_can_write(dsn),
dataset_options = NULL, layer_options = NULL, quiet = FALSE, factorsAsCharacter = TRUE,
append = NA, delete_dsn = FALSE, delete_layer = !is.na(append) && !append,
fid_column_name = NULL) {
fid_column_name = NULL, config_options = character(0)) {

ret = obj
if (!is.null(list(...)$update)) {
Expand Down Expand Up @@ -483,15 +484,15 @@ st_write.sf = function(obj, dsn, layer = NULL, ...,

ret = CPL_write_ogr(obj, dsn, layer, driver,
as.character(dataset_options), as.character(layer_options),
geom, dim, fids, quiet, append, delete_dsn, delete_layer,
geom, dim, fids, config_options, quiet, append, delete_dsn, delete_layer,
write_geometries)
if (ret == 1) { # try through temp file:
tmp = tempfile(fileext = paste0(".", tools::file_ext(dsn))) # nocov start
if (!quiet)
message(paste("writing first to temporary file", tmp))
if (CPL_write_ogr(obj, tmp, layer, driver,
as.character(dataset_options), as.character(layer_options),
geom, dim, fids, quiet, append, delete_dsn, delete_layer,
geom, dim, fids, config_options, quiet, append, delete_dsn, delete_layer,
write_geometries) == 1)
stop(paste("failed writing to temporary file", tmp))
if (!file.copy(tmp, dsn, overwrite = append || delete_dsn || delete_layer))
Expand Down
7 changes: 5 additions & 2 deletions man/st_write.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ BEGIN_RCPP
END_RCPP
}
// CPL_write_ogr
int CPL_write_ogr(Rcpp::List obj, Rcpp::CharacterVector dsn, Rcpp::CharacterVector layer, Rcpp::CharacterVector driver, Rcpp::CharacterVector dco, Rcpp::CharacterVector lco, Rcpp::List geom, Rcpp::CharacterVector dim, Rcpp::CharacterVector fids, bool quiet, Rcpp::LogicalVector append, bool delete_dsn, bool delete_layer, bool write_geometries);
RcppExport SEXP _sf_CPL_write_ogr(SEXP objSEXP, SEXP dsnSEXP, SEXP layerSEXP, SEXP driverSEXP, SEXP dcoSEXP, SEXP lcoSEXP, SEXP geomSEXP, SEXP dimSEXP, SEXP fidsSEXP, SEXP quietSEXP, SEXP appendSEXP, SEXP delete_dsnSEXP, SEXP delete_layerSEXP, SEXP write_geometriesSEXP) {
int CPL_write_ogr(Rcpp::List obj, Rcpp::CharacterVector dsn, Rcpp::CharacterVector layer, Rcpp::CharacterVector driver, Rcpp::CharacterVector dco, Rcpp::CharacterVector lco, Rcpp::List geom, Rcpp::CharacterVector dim, Rcpp::CharacterVector fids, Rcpp::CharacterVector ConfigOptions, bool quiet, Rcpp::LogicalVector append, bool delete_dsn, bool delete_layer, bool write_geometries);
RcppExport SEXP _sf_CPL_write_ogr(SEXP objSEXP, SEXP dsnSEXP, SEXP layerSEXP, SEXP driverSEXP, SEXP dcoSEXP, SEXP lcoSEXP, SEXP geomSEXP, SEXP dimSEXP, SEXP fidsSEXP, SEXP ConfigOptionsSEXP, SEXP quietSEXP, SEXP appendSEXP, SEXP delete_dsnSEXP, SEXP delete_layerSEXP, SEXP write_geometriesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Expand All @@ -526,12 +526,13 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< Rcpp::List >::type geom(geomSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type dim(dimSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type fids(fidsSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type ConfigOptions(ConfigOptionsSEXP);
Rcpp::traits::input_parameter< bool >::type quiet(quietSEXP);
Rcpp::traits::input_parameter< Rcpp::LogicalVector >::type append(appendSEXP);
Rcpp::traits::input_parameter< bool >::type delete_dsn(delete_dsnSEXP);
Rcpp::traits::input_parameter< bool >::type delete_layer(delete_layerSEXP);
Rcpp::traits::input_parameter< bool >::type write_geometries(write_geometriesSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_write_ogr(obj, dsn, layer, driver, dco, lco, geom, dim, fids, quiet, append, delete_dsn, delete_layer, write_geometries));
rcpp_result_gen = Rcpp::wrap(CPL_write_ogr(obj, dsn, layer, driver, dco, lco, geom, dim, fids, ConfigOptions, quiet, append, delete_dsn, delete_layer, write_geometries));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -1284,7 +1285,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_gdalmdiminfo", (DL_FUNC) &_sf_CPL_gdalmdiminfo, 3},
{"_sf_CPL_gdalmdimtranslate", (DL_FUNC) &_sf_CPL_gdalmdimtranslate, 5},
{"_sf_CPL_gdal_warper", (DL_FUNC) &_sf_CPL_gdal_warper, 6},
{"_sf_CPL_write_ogr", (DL_FUNC) &_sf_CPL_write_ogr, 14},
{"_sf_CPL_write_ogr", (DL_FUNC) &_sf_CPL_write_ogr, 15},
{"_sf_CPL_geos_binop", (DL_FUNC) &_sf_CPL_geos_binop, 6},
{"_sf_CPL_geos_is_valid_reason", (DL_FUNC) &_sf_CPL_geos_is_valid_reason, 1},
{"_sf_CPL_geos_make_valid", (DL_FUNC) &_sf_CPL_geos_make_valid, 1},
Expand Down
9 changes: 9 additions & 0 deletions src/gdal_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ void SetFields(OGRFeature *poFeature, std::vector<OGRFieldType> tp, Rcpp::List o
int CPL_write_ogr(Rcpp::List obj, Rcpp::CharacterVector dsn, Rcpp::CharacterVector layer,
Rcpp::CharacterVector driver, Rcpp::CharacterVector dco, Rcpp::CharacterVector lco,
Rcpp::List geom, Rcpp::CharacterVector dim, Rcpp::CharacterVector fids,
Rcpp::CharacterVector ConfigOptions,
bool quiet, Rcpp::LogicalVector append, bool delete_dsn = false, bool delete_layer = false,
bool write_geometries = true) {

if (ConfigOptions.size()) {
if (ConfigOptions.attr("names") == R_NilValue)
Rcpp::stop("config_options should be a character vector with names, as in c(key=\"value\")");
Rcpp::CharacterVector names = ConfigOptions.attr("names");
for (int i = 0; i < ConfigOptions.size(); i++)
CPLSetConfigOption(names[i], ConfigOptions[i]);
}

// init:
if (driver.size() != 1 || dsn.size() != 1 || layer.size() != 1)
Rcpp::stop("argument dsn, layer or driver not of length 1.\n");
Expand Down

0 comments on commit 7974a4d

Please sign in to comment.