Skip to content

Commit

Permalink
GDALWriter: Write features directly if they have no array columns
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jan 8, 2024
1 parent 3f67a2c commit b476a40
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/deferred_gdal_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ DeferredGDALWriter::finish()
for (const auto& feature : m_features) {
for (const auto& [field_name, _] : feature.map()) {
if (ogr_fields.find(field_name) == ogr_fields.end()) {
OGRFieldType field_type = ogr_type(feature.field_type(field_name), m_unnest);
const auto& typ = feature.field_type(field_name);

if (typ == typeid(Feature::DoubleArray) || typ == typeid(Feature::IntegerArray) || typ == typeid(Feature::Integer64Array)) {
m_contains_nested_fields = true;
}

OGRFieldType field_type = ogr_type(typ, m_unnest_if_needed);
ogr_fields[field_name] = OGR_Fld_Create(field_name.c_str(), field_type);
}
}
Expand All @@ -59,7 +65,7 @@ DeferredGDALWriter::finish()

OGRFeatureDefnH defn = OGR_L_GetLayerDefn(m_layer);
for (const auto& feature : m_features) {
if (m_unnest) {
if (m_unnest_if_needed && m_contains_nested_fields) {
GDALFeatureUnnester unnester(feature, defn);
unnester.unnest();

Expand Down
14 changes: 10 additions & 4 deletions src/gdal_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace exactextract {
GDALWriter::
GDALWriter(const std::string& filename, bool unnest)
: m_unnest(unnest)
: m_unnest_if_needed(unnest)
{
auto driver_name = get_driver_name(filename);
auto driver = OGRGetDriverByName(driver_name.c_str());
Expand Down Expand Up @@ -62,7 +62,7 @@ std::unique_ptr<Feature>
GDALWriter::create_feature()
{
auto defn = OGR_L_GetLayerDefn(m_layer);
if (m_unnest) {
if (m_unnest_if_needed && m_contains_nested_fields) {
return std::make_unique<MapFeature>();
}
return std::make_unique<GDALFeature>(OGR_F_Create(defn));
Expand Down Expand Up @@ -101,7 +101,13 @@ GDALWriter::add_operation(const Operation& op)
op.set_empty_result(mf);

for (const auto& field_name : op.field_names()) {
OGRFieldType ogr_typ = ogr_type(mf.field_type(field_name), m_unnest);
const auto& typ = mf.field_type(field_name);

if (typ == typeid(Feature::DoubleArray) || typ == typeid(Feature::IntegerArray) || typ == typeid(Feature::Integer64Array)) {
m_contains_nested_fields = true;
}

OGRFieldType ogr_typ = ogr_type(typ, m_unnest_if_needed);

auto def = OGR_Fld_Create(field_name.c_str(), ogr_typ);
OGR_L_CreateField(m_layer, def, true);
Expand All @@ -112,7 +118,7 @@ GDALWriter::add_operation(const Operation& op)
void
GDALWriter::write(const Feature& f)
{
if (m_unnest) {
if (m_unnest_if_needed && m_contains_nested_fields) {
auto defn = OGR_L_GetLayerDefn(m_layer);

GDALFeatureUnnester unnester(f, defn);
Expand Down
3 changes: 2 additions & 1 deletion src/gdal_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class GDALWriter : public OutputWriter
GDALDatasetH m_dataset;
OGRLayerH m_layer;

bool m_unnest = true;
bool m_unnest_if_needed = true;
bool m_contains_nested_fields = false;
};

}

0 comments on commit b476a40

Please sign in to comment.