Skip to content
Permalink
Browse files

[RASTER] Generate band name with GTiff EXTRA_DIM

Band names can also be retrieved from GeoTIFF files.

(cherry-picked from cb144b3)
  • Loading branch information
seignovert authored and nyalldawson committed Jul 2, 2018
1 parent fd4888b commit e146a9703268b14424ad7814f8d0ec393ca95319
@@ -930,7 +930,7 @@ QString QgsGdalProvider::generateBandName( int bandNumber ) const
if ( !const_cast<QgsGdalProvider *>( this )->initIfNeeded() )
return QString();

if ( strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "netCDF" ) == 0 )
if ( strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "netCDF" ) == 0 || strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "GTiff" ) == 0 )
{
char **GDALmetadata = GDALGetMetadata( mGdalDataset, nullptr );

@@ -943,11 +943,11 @@ QString QgsGdalProvider::generateBandName( int bandNumber ) const
i != metadata.constEnd(); ++i )
{
QString val( *i );
if ( !val.startsWith( QLatin1String( "NETCDF_DIM_EXTRA" ) ) && !val.contains( QLatin1String( "#units=" ) ) )
if ( !val.startsWith( QLatin1String( "NETCDF_DIM_EXTRA" ) ) && !val.startsWith( QLatin1String( "GTIFF_DIM_EXTRA" ) ) && !val.contains( QLatin1String( "#units=" ) ) )
continue;
QStringList values = val.split( '=' );
val = values.at( 1 );
if ( values.at( 0 ) == QLatin1String( "NETCDF_DIM_EXTRA" ) )
if ( values.at( 0 ) == QLatin1String( "NETCDF_DIM_EXTRA" ) || values.at( 0 ) == QLatin1String( "GTIFF_DIM_EXTRA" ) )
{
dimExtraValues = val.replace( '{', QString() ).replace( '}', QString() ).split( ',' );
//http://qt-project.org/doc/qt-4.8/qregexp.html#capturedTexts
@@ -970,14 +970,14 @@ QString QgsGdalProvider::generateBandName( int bandNumber ) const
i != metadata.constEnd(); ++i )
{
QString val( *i );
if ( !val.startsWith( QLatin1String( "NETCDF_DIM_" ) ) )
if ( !val.startsWith( QLatin1String( "NETCDF_DIM_" ) ) && !val.startsWith( QLatin1String( "GTIFF_DIM_" ) ) )
continue;
QStringList values = val.split( '=' );
for ( QStringList::const_iterator j = dimExtraValues.constBegin();
j != dimExtraValues.constEnd(); ++j )
{
QString dim = ( *j );
if ( values.at( 0 ) != "NETCDF_DIM_" + dim )
if ( values.at( 0 ) != "NETCDF_DIM_" + dim && values.at( 0 ) != "GTIFF_DIM_" + dim )
continue;
if ( unitsMap.contains( dim ) && !unitsMap[ dim ].isEmpty() && unitsMap[ dim ] != QLatin1String( "none" ) )
bandNameValues.append( dim + '=' + values.at( 1 ) + " (" + unitsMap[ dim ] + ')' );
@@ -50,6 +50,7 @@ class TestQgsGdalProvider : public QObject
void invalidNoDataInSourceIgnored();
void isRepresentableValue();
void mask();
void bandName(); // test band name based on `gtiff` tags (#7317)

private:
QString mTestDataDir;
@@ -221,5 +222,19 @@ void TestQgsGdalProvider::mask()
delete provider;
}

void TestQgsGdalProvider::bandName()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/gtiff_tags.tif";
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider( QStringLiteral( "gdal" ), raster, QgsDataProvider::ProviderOptions() );
QgsRasterDataProvider *rp = dynamic_cast< QgsRasterDataProvider * >( provider );
QVERIFY( rp );
if ( rp )
{
qDebug() << "Band Name 1: " << rp->generateBandName( 1 );
QCOMPARE( rp->generateBandName( 1 ), QStringLiteral( "Band 1 / wvln=1.234 (um)" ) );
}
delete provider;
}

QGSTEST_MAIN( TestQgsGdalProvider )
#include "testqgsgdalprovider.moc"
Binary file not shown.

0 comments on commit e146a97

Please sign in to comment.
You can’t perform that action at this time.