Skip to content

Commit

Permalink
add layer_options to capturedWriteOGR method, #77
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Apr 7, 2016
1 parent f5cac75 commit c2fc7df
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ README.Rmd
cran-comments.md
myfile.geojson
^CONDUCT\.md$

src/*.o
src/*.so
src/*.a
src/*.dll
5 changes: 3 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#' @param obj_size pls pass in \code{object.size(obj)}
#' @param layer spatial layer to use
#' @param writeOGR pls pass in \code{writeOGR} (no quotes)
#' @param layer_options pls pass in layer options
#' @return character vector of GeoJSON if all goes well
#' @examples \dontrun{
#' capturedWriteOGR(cities[1:10,],
#' object.size(cities[1:10,]),
#' "cities",
#' writeOGR))
#' }
capturedWriteOGR <- function(obj, obj_size, layer, writeOGR) {
.Call('geojsonio_capturedWriteOGR', PACKAGE = 'geojsonio', obj, obj_size, layer, writeOGR)
capturedWriteOGR <- function(obj, obj_size, layer, writeOGR, layer_options) {
.Call('geojsonio_capturedWriteOGR', PACKAGE = 'geojsonio', obj, obj_size, layer, writeOGR, layer_options)
}

4 changes: 2 additions & 2 deletions R/cwgr.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#' coordinates(states) <- ~long+lat
#' cwgr(states[1:100,], "states")
#' }
cwgr <- function(obj, layer) {
capturedWriteOGR(obj, object.size(obj), layer, writeOGR)
cwgr <- function(obj, layer = "", layer_options = "") {
capturedWriteOGR(obj, object.size(obj), layer, writeOGR, layer_options)
}
14 changes: 8 additions & 6 deletions R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,17 @@ write_ogr <- function(input, dir, file, precision = NULL, overwrite, ...){
if (!is.null(precision)) {
## add precision to vector of layer_options in '...'
dots$layer_options <- c(dots$layer_options, paste0("COORDINATE_PRECISION=", precision))
}
args <- c(list(obj = input, dsn = dir, layer = "", driver = "GeoJSON"), dots)
do.call(writeOGR, args)
res <- file.copy(dir, file, overwrite = overwrite)
if (res) {
message("Success! File is at ", file)
} else {
dots$layer_options <- ""
}
# args <- c(list(obj = input, dsn = dir, layer = "", driver = "GeoJSON"), dots)
# do.call(writeOGR, args)
if (!overwrite && file.exists(file)) {
stop(file, " already exists and overwrite = FALSE", call. = FALSE)
}
cat(cwgr(input, "", dots$layer_options), file = file)
message("Success! File is at ", file)
#res <- file.copy(dir, file, overwrite = overwrite)
}

convert_ordered <- function(df) {
Expand Down
7 changes: 4 additions & 3 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
using namespace Rcpp;

// capturedWriteOGR
CharacterVector capturedWriteOGR(SEXP obj, int obj_size, SEXP layer, Function writeOGR);
RcppExport SEXP geojsonio_capturedWriteOGR(SEXP objSEXP, SEXP obj_sizeSEXP, SEXP layerSEXP, SEXP writeOGRSEXP) {
CharacterVector capturedWriteOGR(SEXP obj, int obj_size, SEXP layer, Function writeOGR, CharacterVector layer_options);
RcppExport SEXP geojsonio_capturedWriteOGR(SEXP objSEXP, SEXP obj_sizeSEXP, SEXP layerSEXP, SEXP writeOGRSEXP, SEXP layer_optionsSEXP) {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
Rcpp::traits::input_parameter< SEXP >::type obj(objSEXP);
Rcpp::traits::input_parameter< int >::type obj_size(obj_sizeSEXP);
Rcpp::traits::input_parameter< SEXP >::type layer(layerSEXP);
Rcpp::traits::input_parameter< Function >::type writeOGR(writeOGRSEXP);
__result = Rcpp::wrap(capturedWriteOGR(obj, obj_size, layer, writeOGR));
Rcpp::traits::input_parameter< CharacterVector >::type layer_options(layer_optionsSEXP);
__result = Rcpp::wrap(capturedWriteOGR(obj, obj_size, layer, writeOGR, layer_options));
return __result;
END_RCPP
}
Binary file modified src/RcppExports.o
Binary file not shown.
6 changes: 4 additions & 2 deletions src/captured_write_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace Rcpp;
//' @param obj_size pls pass in \code{object.size(obj)}
//' @param layer spatial layer to use
//' @param writeOGR pls pass in \code{writeOGR} (no quotes)
//' @param layer_options pls pass in layer options
//' @return character vector of GeoJSON if all goes well
//' @examples \dontrun{
//' capturedWriteOGR(cities[1:10,],
Expand All @@ -26,7 +27,8 @@ using namespace Rcpp;
CharacterVector capturedWriteOGR(SEXP obj,
int obj_size,
SEXP layer,
Function writeOGR) {
Function writeOGR,
CharacterVector layer_options) {

// we don't know how big the output is going to be.
// this is the main problem with this approach.
Expand Down Expand Up @@ -67,7 +69,7 @@ CharacterVector capturedWriteOGR(SEXP obj,
// we are calling R from C since the rgdal folks have not
// exposed anything we can use AFAI can tell

writeOGR(obj, "/vsistdout/", layer, "GeoJSON");
writeOGR(obj, "/vsistdout/", layer, "GeoJSON", layer_options = layer_options);

// ok, we let it do it's thing, now make sure we've
// cleatned up after it
Expand Down
Binary file modified src/captured_write_ogr.o
Binary file not shown.
Binary file modified src/geojsonio.so
Binary file not shown.

0 comments on commit c2fc7df

Please sign in to comment.