Skip to content

Commit

Permalink
[ogr] Automatically set QGIS field alias if OGR is able to read one
Browse files Browse the repository at this point in the history
from the underlying datasource

Supported on GDAL >= 3.2, for the file geodatabase driver only
  • Loading branch information
nyalldawson committed Jul 22, 2020
1 parent dac3f00 commit 05be43d
Show file tree
Hide file tree
Showing 62 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -1246,6 +1246,14 @@ void QgsOgrProvider::loadFields()
width, prec, QString(), varSubType
);

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,2,0)
const QString alias = textEncoding()->toUnicode( OGR_Fld_GetAlternativeNameRef( fldDef ) );
if ( !alias.isEmpty() )
{
newField.setAlias( alias );
}
#endif

// check if field is nullable
bool nullable = OGR_Fld_IsNullable( fldDef );
if ( !nullable )
Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -730,6 +730,20 @@ def testMixOfFilterExpressionAndSubsetStringWhenFilterExpressionCompilationFails

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

@unittest.skip(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 2, 0))
def testFieldAliases(self):
"""
Test that field aliases are taken from OGR where available (requires GDAL 3.2 or later)
"""
datasource = os.path.join(unitTestDataPath(), 'field_alias.gdb')
vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())

fields = vl.fields()
self.assertEqual([f.name() for f in fields], ['OBJECTID', 'text', 'short_int', 'long_int', 'float', 'double', 'date', 'blob', 'guid', 'raster', 'SHAPE_Length', 'SHAPE_Area'])
self.assertEqual([f.alias() for f in fields],
['', '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', '', ''])


if __name__ == '__main__':
unittest.main()
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000001.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000001.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000001.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000002.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000002.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000003.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000003.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000003.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000004.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000004.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000004.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000004.spx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000005.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000005.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000005.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000006.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000006.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000006.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000007.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000007.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000007.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000009.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000009.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000009.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a00000009.spx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000a.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000a.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000a.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000b.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000b.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000b.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000c.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000c.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000c.gdbtablx
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000d.gdbindexes
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000d.gdbtable
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/a0000000d.gdbtablx
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/gdb
Binary file not shown.
Binary file added tests/testdata/field_alias.gdb/timestamps
Binary file not shown.

0 comments on commit 05be43d

Please sign in to comment.