Skip to content

Commit

Permalink
[ogr] Allow setting comments when creating fields in compatible datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 18, 2023
1 parent a1345f0 commit e9cc5fb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1859,6 +1859,10 @@ bool QgsOgrProvider::addAttributeOGRLevel( const QgsField &field, bool &ignoreEr
OGR_Fld_SetAlternativeName( fielddefn.get(), textEncoding()->fromUnicode( field.alias() ).constData() );
#endif

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,7,0)
OGR_Fld_SetComment( fielddefn.get(), textEncoding()->fromUnicode( field.comment() ).constData() );
#endif

if ( mOgrLayer->CreateField( fielddefn.get(), true ) != OGRERR_NONE )
{
pushError( tr( "OGR error creating field %1: %2" ).arg( field.name(), CPLGetLastErrorMsg() ) );
Expand Down Expand Up @@ -2926,13 +2930,17 @@ void QgsOgrProvider::computeCapabilities()
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,7,0)
if ( const char *pszAlterFieldDefnFlags = GDALGetMetadataItem( mOgrLayer->driver(), GDAL_DMD_ALTER_FIELD_DEFN_FLAGS, nullptr ) )
if ( const char *pszAlterFieldDefnFlags = GDALGetMetadataItem( mOgrLayer->driver(), GDAL_DMD_CREATION_FIELD_DEFN_FLAGS, nullptr ) )
{
char **papszTokens = CSLTokenizeString2( pszAlterFieldDefnFlags, " ", 0 );
if ( CSLFindString( papszTokens, "AlternativeName" ) >= 0 )
{
mAttributeEditCapabilities |= Qgis::VectorDataProviderAttributeEditCapability::EditAlias;
}
if ( CSLFindString( papszTokens, "Comment" ) >= 0 )
{
mAttributeEditCapabilities |= Qgis::VectorDataProviderAttributeEditCapability::EditComment;
}
CSLDestroy( papszTokens );
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -827,6 +827,9 @@ void QgsVectorFileWriter::init( QString vectorFileName,
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,6,0)
OGR_Fld_SetAlternativeName( fld.get(), mCodec->fromUnicode( attrField.alias() ).constData() );
#endif
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,7,0)
OGR_Fld_SetComment( fld.get(), mCodec->fromUnicode( attrField.comment() ).constData() );
#endif

// create the field
QgsDebugMsgLevel( "creating field " + attrField.name() +
Expand Down
44 changes: 44 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -3309,6 +3309,50 @@ def test_provider_set_field_alias(self):
self.assertEqual(fields['my_field'].alias(), 'my alias')
self.assertEqual(fields['my_field2'].alias(), 'my alias2')

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 7, 0), "GDAL 3.7 required")
def test_provider_set_field_comment(self):
"""
Test setting field comments via the vector data provider api
"""
metadata = QgsProviderRegistry.instance().providerMetadata('ogr')
with tempfile.TemporaryDirectory() as temp_dir:
tmpfile = os.path.join(temp_dir, 'test_gpkg.gpkg')

ok, err = metadata.createDatabase(tmpfile)
self.assertTrue(ok)
self.assertFalse(err)

conn = metadata.createConnection(tmpfile, {})
self.assertTrue(conn)

fields = QgsFields()
field = QgsField('my_field', QVariant.String)
field.setComment('my comment')
fields.append(field)
conn.createVectorTable('', 'test', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem('EPSG:4326'), False, {})
layer = QgsVectorLayer(tmpfile + '|layername=test')
self.assertTrue(layer.isValid())

fields = layer.fields()
self.assertEqual(fields['my_field'].comment(), 'my comment')

self.assertTrue(
layer.dataProvider().attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditComment)

field2 = QgsField('my_field2', QVariant.String)
field2.setComment('my comment2')

self.assertTrue(layer.dataProvider().addAttributes([field2]))

del layer

layer = QgsVectorLayer(tmpfile + '|layername=test')
self.assertTrue(layer.isValid())

fields = layer.fields()
self.assertEqual(fields['my_field'].comment(), 'my comment')
self.assertEqual(fields['my_field2'].comment(), 'my comment2')

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 7, 0), "GDAL 3.7.0 required")
def testFieldComment(self):
"""Test reading field comments"""
Expand Down

0 comments on commit e9cc5fb

Please sign in to comment.