Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support retrieving sublayer path for OGR layers on GDAL 3.4+
  • Loading branch information
nyalldawson committed Aug 25, 2021
1 parent ec80d5a commit abdfcb7
Show file tree
Hide file tree
Showing 59 changed files with 68 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/core/providers/ogr/qgsogrprovidermetadata.cpp
Expand Up @@ -31,6 +31,7 @@ email : nyall dot dawson at gmail dot com
#include "qgsproviderutils.h"
#include "qgsgdalutils.h"

#include <gdal.h>
#include <QFileInfo>
#include <QFile>
#include <QDir>
Expand Down Expand Up @@ -1305,6 +1306,54 @@ QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const
} ), res.end() );
}

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3,4,0)

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QMutex *mutex = nullptr;
#else
QRecursiveMutex *mutex = nullptr;
#endif
GDALDatasetH hDS = firstLayer->getDatasetHandleAndMutex( mutex );
QMutexLocker locker( mutex );
if ( GDALGroupH rootGroup = GDALDatasetGetRootGroup( hDS ) )
{
std::function< void( GDALGroupH, const QStringList & ) > recurseGroup;
recurseGroup = [&recurseGroup, &res]( GDALGroupH group, const QStringList & currentPath )
{
if ( char **vectorLayerNames = GDALGroupGetVectorLayerNames( group, nullptr ) )
{
const QStringList layers = QgsOgrUtils::cStringListToQStringList( vectorLayerNames );
// attach path to matching layers
for ( const QString &layer : layers )
{
for ( int i = 0; i < res.size(); ++i )
{
if ( res.at( i ).name() == layer )
{
res[i].setPath( currentPath );
}
}
}
}

if ( char **subgroupNames = GDALGroupGetGroupNames( group, nullptr ) )
{
for ( int i = 0; subgroupNames[i]; ++i )
{
if ( GDALGroupH subgroup = GDALGroupOpenGroup( group, subgroupNames[i], nullptr ) )
{
recurseGroup( subgroup, QStringList( currentPath ) << QString::fromUtf8( subgroupNames[i] ) );
GDALGroupRelease( subgroup );
}
}
}
};

recurseGroup( rootGroup, {} );
GDALGroupRelease( rootGroup );
}
#endif

return res;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -2045,6 +2045,24 @@ def test_provider_sublayer_details(self):
'driverName': 'SQLite',
'geomColName': ''}])

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 4, 0), "GDAL 3.4 required")
def test_provider_sublayer_details_hierarchy(self):
"""
Test retrieving sublayer details from a datasource with a hierarchy of layers
"""
metadata = QgsProviderRegistry.instance().providerMetadata('ogr')

res = metadata.querySublayers(os.path.join(TEST_DATA_DIR, 'featuredataset.gdb'))
self.assertEqual(len(res), 4)
self.assertEqual(res[0].name(), 'fd1_lyr1')
self.assertEqual(res[0].path(), ['fd1'])
self.assertEqual(res[1].name(), 'fd1_lyr2')
self.assertEqual(res[1].path(), ['fd1'])
self.assertEqual(res[2].name(), 'standalone')
self.assertEqual(res[2].path(), [])
self.assertEqual(res[3].name(), 'fd2_lyr')
self.assertEqual(res[3].path(), ['fd2'])

def test_provider_sublayer_details_fast_scan(self):
"""
Test retrieving sublayer details from data provider metadata, using fast scan
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a00000001.gdbtable
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 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/featuredataset.gdb/a00000004.spx
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a00000006.gdbtable
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a00000009.gdbtable
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a00000009.spx
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000a.gdbtable
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000a.spx
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000b.gdbtable
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000b.spx
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000c.gdbtable
Binary file not shown.
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/a0000000c.spx
Binary file not shown.
Binary file added tests/testdata/featuredataset.gdb/gdb
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/featuredataset.gdb/timestamps
@@ -0,0 +1 @@
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

0 comments on commit abdfcb7

Please sign in to comment.