Skip to content

Commit

Permalink
use ExecuteSQL to run a query string
Browse files Browse the repository at this point in the history
  • Loading branch information
barryrowlingson committed Aug 30, 2018
1 parent b56cb8a commit 55e7776
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 1,026 deletions.
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ CPL_get_layers <- function(datasource, options, do_count = FALSE) {
.Call('_sf_CPL_get_layers', PACKAGE = 'sf', datasource, options, do_count)
}

CPL_read_ogr <- function(datasource, layer, options, quiet, toTypeUser, promote_to_multi = TRUE, int64_as_string = FALSE) {
.Call('_sf_CPL_read_ogr', PACKAGE = 'sf', datasource, layer, options, quiet, toTypeUser, promote_to_multi, int64_as_string)
CPL_read_ogr <- function(datasource, layer, query, options, quiet, toTypeUser, promote_to_multi = TRUE, int64_as_string = FALSE) {
.Call('_sf_CPL_read_ogr', PACKAGE = 'sf', datasource, layer, query, options, quiet, toTypeUser, promote_to_multi, int64_as_string)
}

CPL_gdalinfo <- function(obj, options) {
Expand Down
4 changes: 2 additions & 2 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ st_read.default = function(dsn, layer, ...) {
#' to the current working directory (see \link{getwd}). "Shapefiles" consist of several files with the same basename
#' that reside in the same directory, only one of them having extension \code{.shp}.
#' @export
st_read.character = function(dsn, layer, ..., options = NULL, quiet = FALSE, geometry_column = 1L, type = 0,
st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet = FALSE, geometry_column = 1L, type = 0,
promote_to_multi = TRUE, stringsAsFactors = default.stringsAsFactors(),
int64_as_string = FALSE, check_ring_dir = FALSE) {

Expand All @@ -120,7 +120,7 @@ st_read.character = function(dsn, layer, ..., options = NULL, quiet = FALSE, geo
if (length(promote_to_multi) > 1)
stop("`promote_to_multi' should have length one, and applies to all geometry columns")

x = CPL_read_ogr(dsn, layer, as.character(options), quiet, type, promote_to_multi, int64_as_string)
x = CPL_read_ogr(dsn, layer, query, as.character(options), quiet, type, promote_to_multi, int64_as_string)

which.geom = which(vapply(x, function(f) inherits(f, "sfc"), TRUE))

Expand Down
4 changes: 0 additions & 4 deletions inst/include/sf_RcppExports.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace sf {
}
if (rcpp_result_gen.inherits("interrupted-error"))
throw Rcpp::internal::InterruptedException();
if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen))
throw Rcpp::LongjumpException(rcpp_result_gen);
if (rcpp_result_gen.inherits("try-error"))
throw Rcpp::exception(Rcpp::as<std::string>(rcpp_result_gen).c_str());
return Rcpp::as<Rcpp::List >(rcpp_result_gen);
Expand All @@ -59,8 +57,6 @@ namespace sf {
}
if (rcpp_result_gen.inherits("interrupted-error"))
throw Rcpp::internal::InterruptedException();
if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen))
throw Rcpp::LongjumpException(rcpp_result_gen);
if (rcpp_result_gen.inherits("try-error"))
throw Rcpp::exception(Rcpp::as<std::string>(rcpp_result_gen).c_str());
return Rcpp::as<Rcpp::List >(rcpp_result_gen);
Expand Down
17 changes: 5 additions & 12 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ BEGIN_RCPP
END_RCPP
}
// CPL_read_ogr
Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer, Rcpp::CharacterVector options, bool quiet, Rcpp::NumericVector toTypeUser, bool promote_to_multi, bool int64_as_string);
RcppExport SEXP _sf_CPL_read_ogr(SEXP datasourceSEXP, SEXP layerSEXP, SEXP optionsSEXP, SEXP quietSEXP, SEXP toTypeUserSEXP, SEXP promote_to_multiSEXP, SEXP int64_as_stringSEXP) {
Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer, Rcpp::CharacterVector query, Rcpp::CharacterVector options, bool quiet, Rcpp::NumericVector toTypeUser, bool promote_to_multi, bool int64_as_string);
RcppExport SEXP _sf_CPL_read_ogr(SEXP datasourceSEXP, SEXP layerSEXP, SEXP querySEXP, SEXP optionsSEXP, SEXP quietSEXP, SEXP toTypeUserSEXP, SEXP promote_to_multiSEXP, SEXP int64_as_stringSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type datasource(datasourceSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type layer(layerSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type query(querySEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type options(optionsSEXP);
Rcpp::traits::input_parameter< bool >::type quiet(quietSEXP);
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type toTypeUser(toTypeUserSEXP);
Rcpp::traits::input_parameter< bool >::type promote_to_multi(promote_to_multiSEXP);
Rcpp::traits::input_parameter< bool >::type int64_as_string(int64_as_stringSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_read_ogr(datasource, layer, options, quiet, toTypeUser, promote_to_multi, int64_as_string));
rcpp_result_gen = Rcpp::wrap(CPL_read_ogr(datasource, layer, query, options, quiet, toTypeUser, promote_to_multi, int64_as_string));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -878,10 +879,6 @@ RcppExport SEXP _sf_CPL_read_wkb(SEXP wkb_listSEXP, SEXP EWKBSEXP, SEXP spatiali
UNPROTECT(1);
Rf_onintr();
}
bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen);
if (rcpp_isLongjump_gen) {
Rcpp::internal::resumeJump(rcpp_result_gen);
}
Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error");
if (rcpp_isError_gen) {
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
Expand Down Expand Up @@ -913,10 +910,6 @@ RcppExport SEXP _sf_CPL_write_wkb(SEXP sfcSEXP, SEXP EWKBSEXP) {
UNPROTECT(1);
Rf_onintr();
}
bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen);
if (rcpp_isLongjump_gen) {
Rcpp::internal::resumeJump(rcpp_result_gen);
}
Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error");
if (rcpp_isError_gen) {
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
Expand Down Expand Up @@ -953,7 +946,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_gdal_segmentize", (DL_FUNC) &_sf_CPL_gdal_segmentize, 2},
{"_sf_CPL_gdal_linestring_sample", (DL_FUNC) &_sf_CPL_gdal_linestring_sample, 2},
{"_sf_CPL_get_layers", (DL_FUNC) &_sf_CPL_get_layers, 3},
{"_sf_CPL_read_ogr", (DL_FUNC) &_sf_CPL_read_ogr, 7},
{"_sf_CPL_read_ogr", (DL_FUNC) &_sf_CPL_read_ogr, 8},
{"_sf_CPL_gdalinfo", (DL_FUNC) &_sf_CPL_gdalinfo, 2},
{"_sf_CPL_gdalwarp", (DL_FUNC) &_sf_CPL_gdalwarp, 3},
{"_sf_CPL_gdalrasterize", (DL_FUNC) &_sf_CPL_gdalrasterize, 3},
Expand Down
9 changes: 8 additions & 1 deletion src/gdal_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,22 @@ Rcpp::List CPL_get_layers(Rcpp::CharacterVector datasource, Rcpp::CharacterVecto
}

// [[Rcpp::export]]
Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer,
Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector layer,
Rcpp::CharacterVector query,
Rcpp::CharacterVector options, bool quiet, Rcpp::NumericVector toTypeUser,
bool promote_to_multi = true, bool int64_as_string = false) {
// adapted from the OGR tutorial @ www.gdal.org
std::vector <char *> open_options = create_options(options, quiet);
GDALDataset *poDS;

poDS = (GDALDataset *) GDALOpenEx( datasource[0], GDAL_OF_VECTOR | GDAL_OF_READONLY, NULL,
open_options.data(), NULL );
if( poDS == NULL ) {
Rcpp::Rcout << "Cannot open data source " << datasource[0] << std::endl;
Rcpp::stop("Open failed.\n");
}


if (layer.size() == 0) { // no layer specified
switch (poDS->GetLayerCount()) {
case 0: { // error:
Expand All @@ -221,6 +224,10 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
}
}

if(!Rcpp::CharacterVector::is_na(query[0])){
poDS->ExecuteSQL(query[0], NULL, NULL);
};

OGRLayer *poLayer = poDS->GetLayerByName(layer[0]);
if (poLayer == NULL) {
Rcpp::Rcout << "Cannot open layer " << layer[0] << std::endl;
Expand Down
Loading

0 comments on commit 55e7776

Please sign in to comment.