Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][ogr] Allow creation of binary (blob) fields for compatible …
…filetypes
  • Loading branch information
nyalldawson committed Nov 12, 2018
1 parent 5158720 commit 3637930
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/gui/qgsnewgeopackagelayerdialog.cpp
Expand Up @@ -96,7 +96,8 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number (integer 64 bit)" ), "integer64" );
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldFloat.svg" ) ), tr( "Decimal number (real)" ), "real" );
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDate.svg" ) ), tr( "Date" ), "date" );
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDateTime.svg" ) ), tr( "Date&time" ), "datetime" );
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDateTime.svg" ) ), tr( "Datetime" ), "datetime" );
mFieldTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBinary.svg" ) ), tr( "Binary (BLOB)" ), "binary" );

mOkButton = buttonBox->button( QDialogButtonBox::Ok );
mOkButton->setEnabled( false );
Expand Down Expand Up @@ -444,11 +445,14 @@ bool QgsNewGeoPackageLayerDialog::apply()
ogrType = OFTDate;
else if ( fieldType == QLatin1String( "datetime" ) )
ogrType = OFTDateTime;
else if ( fieldType == QLatin1String( "binary" ) )
ogrType = OFTBinary;

int ogrWidth = fieldWidth.toInt();

gdal::ogr_field_def_unique_ptr fld( OGR_Fld_Create( fieldName.toUtf8().constData(), ogrType ) );
OGR_Fld_SetWidth( fld.get(), ogrWidth );
if ( ogrType != OFTBinary )
OGR_Fld_SetWidth( fld.get(), ogrWidth );

if ( OGR_L_CreateField( hLayer, fld.get(), true ) != OGRERR_NONE )
{
Expand Down
11 changes: 11 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -475,6 +475,7 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
bool supportsDate = true;
bool supportsTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" ) && mGDALDriverName != QLatin1String( "GPKG" );
bool supportsDateTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" );
bool supportsBinary = false;
const char *pszDataTypes = nullptr;
if ( mOgrOrigLayer )
{
Expand All @@ -488,6 +489,7 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
supportsDate = CSLFindString( papszTokens, "Date" ) >= 0;
supportsTime = CSLFindString( papszTokens, "Time" ) >= 0;
supportsDateTime = CSLFindString( papszTokens, "DateTime" ) >= 0;
supportsBinary = CSLFindString( papszTokens, "Binary" ) >= 0;
CSLDestroy( papszTokens );
}

Expand All @@ -506,6 +508,11 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), QStringLiteral( "datetime" ), QVariant::DateTime );
}
if ( supportsBinary )
{
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Binary object (BLOB)" ), QStringLiteral( "binary" ), QVariant::ByteArray );
}

bool supportsBoolean = false;

Expand Down Expand Up @@ -1623,6 +1630,10 @@ bool QgsOgrProvider::addAttributeOGRLevel( const QgsField &field, bool &ignoreEr
case QVariant::String:
type = OFTString;
break;
case QVariant::ByteArray:
type = OFTBinary;
break;

default:
pushError( tr( "type %1 for field %2 not found" ).arg( field.typeName(), field.name() ) );
ignoreErrorOut = true;
Expand Down

0 comments on commit 3637930

Please sign in to comment.