Skip to content
Permalink
Browse files
Respect FastScan flag in gdal provider
  • Loading branch information
nyalldawson committed Jun 23, 2021
1 parent 2b384ce commit 3b34458a1133bdf6405a91783362ae9fbd0c49e4
Showing with 38 additions and 1 deletion.
  1. +31 −1 src/core/providers/gdal/qgsgdalprovider.cpp
  2. +7 −0 tests/src/core/testqgsgdalprovider.cpp
@@ -3553,7 +3553,7 @@ QgsProviderMetadata::ProviderCapabilities QgsGdalProviderMetadata::providerCapab
return FileBasedUris;
}

QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const QString &uri, Qgis::SublayerQueryFlags, QgsFeedback * ) const
QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const QString &uri, Qgis::SublayerQueryFlags flags, QgsFeedback * ) const
{
gdal::dataset_unique_ptr dataset;

@@ -3571,6 +3571,36 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
gdalUri = vsiPrefix + gdalUri;
}

if ( flags & Qgis::SublayerQueryFlag::FastScan )
{
// filter based on extension
const QVariantMap uriParts = decodeUri( uri );
const QString path = uriParts.value( QStringLiteral( "path" ) ).toString();
QFileInfo info( path );
if ( info.isFile() )
{
const QString suffix = info.suffix().toLower();

static QString sFilterString;
static QStringList sExtensions;
static QStringList sWildcards;

// get supported extensions
static std::once_flag initialized;
std::call_once( initialized, [ = ]
{
buildSupportedRasterFileFilterAndExtensions( sFilterString, sExtensions, sWildcards );
QgsDebugMsgLevel( QStringLiteral( "extensions: " ) + sExtensions.join( ' ' ), 2 );
QgsDebugMsgLevel( QStringLiteral( "wildcards: " ) + sWildcards.join( ' ' ), 2 );
} );

if ( !sExtensions.contains( suffix ) )
{
return {};
}
}
}

dataset.reset( QgsGdalProviderBase::gdalOpen( gdalUri, GDAL_OF_READONLY ) );
if ( !dataset )
{
@@ -428,6 +428,8 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
// not a raster
res = gdalMetadata->querySublayers( QString( TEST_DATA_DIR ) + "/lines.shp" );
QVERIFY( res.empty() );
res = gdalMetadata->querySublayers( QString( TEST_DATA_DIR ) + "/lines.shp", Qgis::SublayerQueryFlag::FastScan );
QVERIFY( res.empty() );

// single layer raster
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/landsat.tif" );
@@ -463,6 +465,8 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
QCOMPARE( res.at( 1 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 1 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/mixed_layers.gpkg", Qgis::SublayerQueryFlag::FastScan );
QCOMPARE( res.count(), 2 );

// netcdf file
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/mesh/trap_steady_05_3D.nc" );
@@ -483,6 +487,9 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
QCOMPARE( res.at( 1 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 1 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );

res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/mesh/trap_steady_05_3D.nc", Qgis::SublayerQueryFlag::FastScan );
QCOMPARE( res.count(), 8 );
}

QGSTEST_MAIN( TestQgsGdalProvider )

0 comments on commit 3b34458

Please sign in to comment.