Skip to content

Commit

Permalink
added unit test and test raster
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAnnex authored and nyalldawson committed Oct 9, 2018
1 parent c4872b8 commit 9b4cb7a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,59 +936,61 @@ QString QgsGdalProvider::generateBandName( int bandNumber ) const
if ( strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "netCDF" ) == 0 || strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "GTiff" ) == 0 )
{
char **GDALmetadata = GDALGetMetadata(mGdalDataset, nullptr);
if (GDALmetadata)
if ( GDALmetadata )
{
QStringList metadata = QgsOgrUtils::cStringListToQStringList(GDALmetadata);
QStringList dimExtraValues;
QMap<QString, QString> unitsMap;
for (QStringList::const_iterator i = metadata.constBegin(); i != metadata.constEnd(); ++i) {
QString val(*i);
if (!val.startsWith(QLatin1String("NETCDF_DIM_EXTRA")) && !val.startsWith(QLatin1String("GTIFF_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") || values.at(0) == QLatin1String("GTIFF_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
} else {
unitsMap[values.at(0).split('#').at(0)] = val;
}
}
if (!dimExtraValues.isEmpty())
if ( !dimExtraValues.isEmpty() )
{
QStringList bandNameValues;
GDALRasterBandH gdalBand = GDALGetRasterBand(mGdalDataset, bandNumber);
GDALmetadata = GDALGetMetadata(gdalBand, nullptr);
if (GDALmetadata)
if ( GDALmetadata )
{
metadata = QgsOgrUtils::cStringListToQStringList(GDALmetadata);
for (QStringList::const_iterator i = metadata.constBegin(); i != metadata.constEnd(); ++i)
{
QString val(*i);
if (!val.startsWith(QLatin1String("NETCDF_DIM_")) && !val.startsWith(QLatin1String("GTIFF_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 && values.at(0) != "GTIFF_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"))
if ( unitsMap.contains(dim) && !unitsMap[dim].isEmpty() && unitsMap[dim] != QLatin1String("none") )
bandNameValues.append(dim + '=' + values.at(1) + " (" + unitsMap[dim] + ')');
else
bandNameValues.append(dim + '=' + values.at(1));
}
}
}
if (!bandNameValues.isEmpty())
if ( !bandNameValues.isEmpty() )
{
return tr("Band") + QStringLiteral(" %1 / %2").arg(bandNumber, 1 + (int) std::log10((float) bandCount()), 10, QChar('0')).arg(bandNameValues.join(QStringLiteral(" / ")));
}
}
}
}
QString generatedBandName = QgsRasterDataProvider::generateBandName( bandNumber );
GDALRasterBandH myGdalBand = getBand( bandNumber );
QString gdalBandName(GDALGetDescription(myGdalBand));
if(!gdalBandName.isEmpty())
if( !gdalBandName.isEmpty() )
{
return generatedBandName + QStringLiteral(": ") + gdalBandName;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/src/providers/testqgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,33 @@ void TestQgsGdalProvider::bandName()
delete provider;
}

void TestQgsGdalProvider::bandNameNoDescription()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/band1_byte_ct_epsg4326.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" ) );
}
delete provider;
}

void TestQgsGdalProvider::bandNameWithDescription()
{
QString raster = QStringLiteral( TEST_DATA_DIR ) + "/raster/gtiff_desc.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: 1.234 um" ) );
}
delete provider;
}

QGSTEST_MAIN( TestQgsGdalProvider )
#include "testqgsgdalprovider.moc"
Binary file added tests/testdata/raster/gtiff_desc.tif
Binary file not shown.

0 comments on commit 9b4cb7a

Please sign in to comment.