Skip to content

Commit

Permalink
Merge pull request #1 from dis-organization/execute_sql
Browse files Browse the repository at this point in the history
checks and cleanup for ExecuteSQL
  • Loading branch information
barryrowlingson authored Sep 3, 2018
2 parents 55e7776 + 2745b02 commit 5893906
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions inst/include/sf_RcppExports.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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 @@ -57,6 +59,8 @@ 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
8 changes: 8 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,10 @@ 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 @@ -910,6 +914,10 @@ 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
39 changes: 24 additions & 15 deletions src/gdal_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Rcpp::List allocate_out_list(OGRFeatureDefn *poFDefn, int n_features, bool int64

int to_multi_what(std::vector<OGRGeometry *> gv) {
bool points = false, multipoints = false,
lines = false, multilines = false,
lines = false, multilines = false,
polygons = false, multipolygons = false;

for (unsigned int i = 0; i < gv.size(); i++) {
Expand Down Expand Up @@ -132,7 +132,7 @@ Rcpp::List CPL_get_layers(Rcpp::CharacterVector datasource, Rcpp::CharacterVecto
Rcpp::stop("argument datasource should have length 1.\n"); // #nocov
std::vector <char *> open_options = create_options(options, false);
GDALDataset *poDS;
poDS = (GDALDataset *) GDALOpenEx(datasource[0], GDAL_OF_VECTOR | GDAL_OF_READONLY, NULL,
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;
Expand Down Expand Up @@ -191,14 +191,14 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
std::vector <char *> open_options = create_options(options, quiet);
GDALDataset *poDS;

poDS = (GDALDataset *) GDALOpenEx( datasource[0], GDAL_OF_VECTOR | GDAL_OF_READONLY, NULL,
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 @@ -224,11 +224,15 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
}
}

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

OGRLayer *poLayer = poDS->GetLayerByName(layer[0]);
poLayer = poDS->ExecuteSQL(query[0], NULL, NULL);
if (poLayer == NULL) {
Rcpp::stop("SQL execution failed, cannot open layer.\n");
}
} else {
poLayer = poDS->GetLayerByName(layer[0]);
}
if (poLayer == NULL) {
Rcpp::Rcout << "Cannot open layer " << layer[0] << std::endl;
Rcpp::stop("Opening layer failed.\n");
Expand Down Expand Up @@ -323,9 +327,9 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
poFeature->GetFieldAsDateTime(iField, &Year, &Month, &Day, &Hour, &Minute,
&Second, &TZFlag);
// POSIXlt: sec min hour mday mon year wday yday isdst ...
Rcpp::List dtlst =
Rcpp::List::create((double) Second, (double) Minute,
(double) Hour, (double) Day, (double) Month - 1, (double) Year - 1900,
Rcpp::List dtlst =
Rcpp::List::create((double) Second, (double) Minute,
(double) Hour, (double) Day, (double) Month - 1, (double) Year - 1900,
0.0, 0.0, 0.0);
dtlst.attr("class") = "POSIXlt";
if (TZFlag == 100)
Expand Down Expand Up @@ -388,7 +392,7 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
for (int j = 0; j < iv.size(); j++)
iv[j] = il[j];
lv[i] = iv;
}
}
break;
case OFTInteger64List: {
Rcpp::List lv;
Expand Down Expand Up @@ -460,8 +464,8 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
OGRGeometry *geom = poFeatureV[i]->StealGeometry(iGeom); // transfer ownership
if (geom == NULL)
geom = OGRGeometryFactory::createGeometry((OGRwkbGeometryType) toType); // #nocov
else if ((geom =
OGRGeometryFactory::forceTo(geom, (OGRwkbGeometryType) toType, NULL))
else if ((geom =
OGRGeometryFactory::forceTo(geom, (OGRwkbGeometryType) toType, NULL))
== NULL)
Rcpp::stop("OGRGeometryFactory::forceTo returned NULL"); // #nocov
handle_error(poFeatureV[i]->SetGeomFieldDirectly(iGeom, geom));
Expand Down Expand Up @@ -502,7 +506,7 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
}

if (warn_int64)
Rcpp::Rcout << "Integer64 values larger than " << dbl_max_int64 <<
Rcpp::Rcout << "Integer64 values larger than " << dbl_max_int64 <<
" lost significance after conversion to double;" << std::endl <<
"use argument int64_as_string = TRUE to import them lossless, as character" << std::endl;

Expand All @@ -511,6 +515,11 @@ Rcpp::List CPL_read_ogr(Rcpp::CharacterVector datasource, Rcpp::CharacterVector
OGRFeature::DestroyFeature(poFeatureV[i]);
for (i = 0; i < to_be_freed.size(); i++)
OGRGeometryFactory::destroyGeometry(to_be_freed[i]);

// clean up if SQL was used https://www.gdal.org/classGDALDataset.html#ab2c2b105b8f76a279e6a53b9b4a182e0
if(!Rcpp::CharacterVector::is_na(query[0])){
poDS->ReleaseResultSet(poLayer);
}
GDALClose(poDS);

return out;
Expand Down

0 comments on commit 5893906

Please sign in to comment.