Skip to content

Commit 05be43d

Browse files
committedJul 22, 2020
[ogr] Automatically set QGIS field alias if OGR is able to read one
from the underlying datasource Supported on GDAL >= 3.2, for the file geodatabase driver only
1 parent dac3f00 commit 05be43d

File tree

62 files changed

+22
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+22
-0
lines changed
 

‎src/core/providers/ogr/qgsogrprovider.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,14 @@ void QgsOgrProvider::loadFields()
12461246
width, prec, QString(), varSubType
12471247
);
12481248

1249+
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,2,0)
1250+
const QString alias = textEncoding()->toUnicode( OGR_Fld_GetAlternativeNameRef( fldDef ) );
1251+
if ( !alias.isEmpty() )
1252+
{
1253+
newField.setAlias( alias );
1254+
}
1255+
#endif
1256+
12491257
// check if field is nullable
12501258
bool nullable = OGR_Fld_IsNullable( fldDef );
12511259
if ( !nullable )

‎tests/src/python/test_provider_ogr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,20 @@ def testMixOfFilterExpressionAndSubsetStringWhenFilterExpressionCompilationFails
730730

731731
self.assertCountEqual([f.attributes() for f in vl.getFeatures(request)], [['rectangle', '1']])
732732

733+
@unittest.skip(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 2, 0))
734+
def testFieldAliases(self):
735+
"""
736+
Test that field aliases are taken from OGR where available (requires GDAL 3.2 or later)
737+
"""
738+
datasource = os.path.join(unitTestDataPath(), 'field_alias.gdb')
739+
vl = QgsVectorLayer(datasource, 'test', 'ogr')
740+
self.assertTrue(vl.isValid())
741+
742+
fields = vl.fields()
743+
self.assertEqual([f.name() for f in fields], ['OBJECTID', 'text', 'short_int', 'long_int', 'float', 'double', 'date', 'blob', 'guid', 'raster', 'SHAPE_Length', 'SHAPE_Area'])
744+
self.assertEqual([f.alias() for f in fields],
745+
['', 'My Text Field', 'My Short Int Field', 'My Long Int Field', 'My Float Field', 'My Double Field', 'My Date Field', 'My Blob Field', 'My GUID field', 'My Raster Field', '', ''])
746+
733747

734748
if __name__ == '__main__':
735749
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.