Skip to content

Commit

Permalink
for #1426
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Feb 13, 2024
1 parent a8fafb2 commit 8dec2df
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
45 changes: 23 additions & 22 deletions src/read_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,32 @@ SpatDataFrame readAttributes(OGRLayer *poLayer, bool as_proxy) {
if (nfields == 0) return df;

OGRFieldType ft;
poLayer->ResetReading();
OGRFeature *poFeature;
OGRFieldDefn *poFieldDefn;
df.resize_cols(nfields);
bool first = true;
unsigned dtype;
long longNA = NA<long>::value;

while( (poFeature = poLayer->GetNextFeature()) != NULL ) {
if (first) {
for (size_t i = 0; i < nfields; i++ ) {
poFieldDefn = poFDefn->GetFieldDefn(i);
std::string fname = poFieldDefn->GetNameRef();
ft = poFieldDefn->GetType();
if (ft == OFTReal) {
dtype = 0;
} else if ((ft == OFTInteger) | (ft == OFTInteger64)) {
if (poFieldDefn->GetSubType() == OFSTBoolean) {
dtype = 3;
} else {
dtype = 1;
}
} else {
dtype = 2;
}
df.add_column(dtype, fname);
for (size_t i = 0; i < nfields; i++ ) {
poFieldDefn = poFDefn->GetFieldDefn(i);
std::string fname = poFieldDefn->GetNameRef();
ft = poFieldDefn->GetType();
if (ft == OFTReal) {
dtype = 0;
} else if ((ft == OFTInteger) | (ft == OFTInteger64)) {
if (poFieldDefn->GetSubType() == OFSTBoolean) {
dtype = 3;
} else {
dtype = 1;
}
first = false;
} else {
dtype = 2;
}
df.add_column(dtype, fname);
}

OGRFeature *poFeature;
poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL ) {

for (size_t i = 0; i < nfields; i++ ) {
poFieldDefn = poFDefn->GetFieldDefn( i );
Expand Down Expand Up @@ -464,6 +461,8 @@ bool layerQueryFilter(GDALDataset *&poDS, OGRLayer *&poLayer, std::string &layer
}


#include "Rcpp.h"

bool SpatVector::read_ogr(GDALDataset *&poDS, std::string layer, std::string query, std::vector<double> extent, SpatVector filter, bool as_proxy, std::string what) {

if (poDS == NULL) {
Expand Down Expand Up @@ -648,6 +647,8 @@ bool SpatVector::read_ogr(GDALDataset *&poDS, std::string layer, std::string que
}
}

Rcpp::Rcout << fcnt << "\n";

SpatVectorCollection sv;
std::vector<double> dempty;
SpatVector filter2;
Expand Down
51 changes: 31 additions & 20 deletions src/write_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ GDALDataset* SpatVector::write_ogr(std::string filename, std::string lyrname, st
} else {
append = false;
}
if (nrow() == 0) {
setError("no geometries to write");
return(poDS);
}
// if (nrow() == 0) {
// addWarning("no geometries to write");
//return(poDS);
// }
}

if (append) {
Expand Down Expand Up @@ -94,26 +94,29 @@ GDALDataset* SpatVector::write_ogr(std::string filename, std::string lyrname, st
}

if( poDS == NULL ) {
setError("Creation of output dataset failed" );
setError("Creation of output dataset failed");
return poDS;
}

OGRwkbGeometryType wkb;
SpatGeomType geomtype = geoms[0].gtype;
if (geomtype == points) {
wkb = wkbPoint;
} else if (geomtype == lines) {
wkb = wkbMultiLineString;
} else if (geomtype == polygons) {
wkb = wkbMultiPolygon;
if (nrow() > 0) {
SpatGeomType geomtype = geoms[0].gtype;
if (geomtype == points) {
wkb = wkbPoint;
} else if (geomtype == lines) {
wkb = wkbMultiLineString;
} else if (geomtype == polygons) {
wkb = wkbMultiPolygon;
} else {
setError("this geometry type is not supported: " + type());
return poDS;
}
} else {
setError("this geometry type is not supported: " + type());
return poDS;
wkb = wkbUnknown;
}

std::string s = srs.wkt;


OGRSpatialReference *SRS = NULL;
if (!s.empty()) {
SRS = new OGRSpatialReference;
Expand Down Expand Up @@ -193,6 +196,12 @@ GDALDataset* SpatVector::write_ogr(std::string filename, std::string lyrname, st
}
}


if (ngeoms == 0) {
return poDS;
}


// use a single transaction as in sf
// makes a big difference for gpkg by avoiding many INSERTs
bool can_do_transaction = poDS->TestCapability(ODsCTransactions); // == TRUE);
Expand Down Expand Up @@ -353,21 +362,23 @@ GDALDataset* SpatVector::write_ogr(std::string filename, std::string lyrname, st
}
}
}

if (transaction && (gcntr>0) && (poDS->CommitTransaction() != OGRERR_NONE)) {
poDS->RollbackTransaction();
setError("transaction commit failed");
}

return poDS;
}



bool SpatVector::write(std::string filename, std::string lyrname, std::string driver, bool append, bool overwrite, std::vector<std::string> options) {

if (nrow() == 0) {
addWarning("nothing to write");
return false;
}
// if (nrow() == 0) {
// setError("nothing to write");
// return false;
// }

GDALDataset *poDS = write_ogr(filename, lyrname, driver, append, overwrite, options);
if (poDS != NULL) GDALClose( poDS );
Expand Down

0 comments on commit 8dec2df

Please sign in to comment.