Skip to content
Permalink
Browse files
Ensure GDAL provider querySublayers preserves open options and other …
…uri components in sublayer uris
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 86518bb commit c3a08254e57158d3f430ddfa9579cad56bb6e167
Showing with 27 additions and 4 deletions.
  1. +7 −4 src/core/providers/gdal/qgsgdalprovider.cpp
  2. +20 −0 tests/src/core/testqgsgdalprovider.cpp
@@ -1654,6 +1654,7 @@ QList<QgsProviderSublayerDetails> QgsGdalProvider::sublayerDetails( GDALDatasetH
char **metadata = GDALGetMetadata( dataset, "SUBDATASETS" );

QVariantMap uriParts = decodeGdalUri( baseUri );
const QString datasetPath = uriParts.value( QStringLiteral( "path" ) ).toString();

if ( metadata )
{
@@ -1679,10 +1680,10 @@ QList<QgsProviderSublayerDetails> QgsGdalProvider::sublayerDetails( GDALDatasetH
else
{
// try to extract layer name from a path like 'NETCDF:"/baseUri":cell_node'
sepIdx = layerName.indexOf( baseUri + "\":" );
sepIdx = layerName.indexOf( datasetPath + "\":" );
if ( sepIdx >= 0 )
{
layerName = layerName.mid( layerName.indexOf( baseUri + "\":" ) + baseUri.length() + 2 );
layerName = layerName.mid( layerName.indexOf( datasetPath + "\":" ) + datasetPath.length() + 2 );
}
}

@@ -1693,10 +1694,12 @@ QList<QgsProviderSublayerDetails> QgsGdalProvider::sublayerDetails( GDALDatasetH
details.setDescription( layerDesc );
details.setLayerNumber( i );

QVariantMap layerUriParts = decodeGdalUri( uri );
const QVariantMap layerUriParts = decodeGdalUri( uri );
// update original uri parts with this layername and path -- this ensures that other uri components
// like open options are preserved for the sublayer uris
uriParts.insert( QStringLiteral( "layerName" ), layerUriParts.value( QStringLiteral( "layerName" ) ) );
uriParts.insert( QStringLiteral( "path" ), layerUriParts.value( QStringLiteral( "path" ) ) );
details.setUri( encodeGdalUri( layerUriParts ) );
details.setUri( encodeGdalUri( uriParts ) );

res << details;
}
@@ -491,6 +491,26 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/mesh/trap_steady_05_3D.nc", Qgis::SublayerQueryFlag::FastScan );
QCOMPARE( res.count(), 8 );

// netcdf with open options
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/mesh/trap_steady_05_3D.nc|option:HONOUR_VALID_RANGE=YES" );
QCOMPARE( res.count(), 8 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "cell_node" ) );
QCOMPARE( res.at( 0 ).description(), QStringLiteral( "[320x4] cell_node (32-bit integer)" ) );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "NETCDF:\"%1/mesh/trap_steady_05_3D.nc\":cell_node|option:HONOUR_VALID_RANGE=YES" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "gdal" ) );
QCOMPARE( res.at( 0 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 0 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );
QCOMPARE( res.at( 1 ).layerNumber(), 2 );
QCOMPARE( res.at( 1 ).name(), QStringLiteral( "layerface_Z" ) );
QCOMPARE( res.at( 1 ).description(), QStringLiteral( "[37x3520] layerface_Z (32-bit floating-point)" ) );
QCOMPARE( res.at( 1 ).uri(), QStringLiteral( "NETCDF:\"%1/mesh/trap_steady_05_3D.nc\":layerface_Z|option:HONOUR_VALID_RANGE=YES" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 1 ).providerKey(), QStringLiteral( "gdal" ) );
QCOMPARE( res.at( 1 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 1 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );

// aigrid file
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/aigrid" );
QCOMPARE( res.count(), 1 );

0 comments on commit c3a0825

Please sign in to comment.