Skip to content

Commit

Permalink
[vector file writer] Preserve array fields when saving as geopackage
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jan 30, 2022
1 parent 50c4cd0 commit ea173f3
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
#include "qgsgeometry.h"

#include "qgslogger.h"
#include "qgsmessagelog.h"
#include "qgscoordinatereferencesystem.h"
Expand All @@ -47,6 +48,7 @@
#include <QMetaType>
#include <QMutex>
#include <QRegularExpression>
#include <QJsonDocument>

#include <cassert>
#include <cstdlib> // size_t
Expand Down Expand Up @@ -597,6 +599,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
}

OGRFieldType ogrType = OFTString; //default to string
OGRFieldSubType ogrSubType = OFSTNone;
int ogrWidth = attrField.length();
int ogrPrecision = attrField.precision();
if ( ogrPrecision > 0 )
Expand Down Expand Up @@ -629,6 +632,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,

case QVariant::Bool:
ogrType = OFTInteger;
ogrSubType = OFSTBoolean;
ogrWidth = 1;
ogrPrecision = 0;
break;
Expand Down Expand Up @@ -694,6 +698,14 @@ void QgsVectorFileWriter::init( QString vectorFileName,
}

case QVariant::List:
// handle GPKG conversion to JSON
if ( mOgrDriverName == QLatin1String( "GPKG" ) )
{
ogrType = OFTString;
ogrSubType = OFSTJSON;
break;
}

// fall through to default for other unsupported types
if ( attrField.subType() == QVariant::String )
{
Expand Down Expand Up @@ -803,14 +815,8 @@ void QgsVectorFileWriter::init( QString vectorFileName,
OGR_Fld_SetPrecision( fld.get(), ogrPrecision );
}

switch ( attrField.type() )
{
case QVariant::Bool:
OGR_Fld_SetSubType( fld.get(), OFSTBoolean );
break;
default:
break;
}
if ( ogrSubType != OFSTNone )
OGR_Fld_SetSubType( fld.get(), ogrSubType );

// create the field
QgsDebugMsgLevel( "creating field " + attrField.name() +
Expand Down Expand Up @@ -2537,6 +2543,19 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur

case QVariant::StringList:
{
// handle GPKG conversion to JSON
if ( mOgrDriverName == QLatin1String( "GPKG" ) )
{
const QJsonDocument doc = QJsonDocument::fromVariant( attrValue );
QString jsonString;
if ( !doc.isNull() )
{
jsonString = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).data() );
}
OGR_F_SetFieldString( poFeature.get(), ogrField, mCodec->fromUnicode( jsonString.constData() ) );
break;
}

QStringList list = attrValue.toStringList();
if ( mSupportedListSubTypes.contains( QVariant::String ) )
{
Expand All @@ -2563,6 +2582,19 @@ gdal::ogr_feature_unique_ptr QgsVectorFileWriter::createFeature( const QgsFeatur
}

case QVariant::List:
// handle GPKG conversion to JSON
if ( mOgrDriverName == QLatin1String( "GPKG" ) )
{
const QJsonDocument doc = QJsonDocument::fromVariant( attrValue );
QString jsonString;
if ( !doc.isNull() )
{
jsonString = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).data() );
}
OGR_F_SetFieldString( poFeature.get(), ogrField, mCodec->fromUnicode( jsonString.constData() ) );
break;
}

// fall through to default for unsupported types
if ( field.subType() == QVariant::String )
{
Expand Down

0 comments on commit ea173f3

Please sign in to comment.