Skip to content

Commit

Permalink
NetCDF sublayers test
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Dec 20, 2012
1 parent e242af4 commit 482a007
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,23 @@ QString QgsGdalProvider::metadata()
myMetadata += "</p>\n";

//just use the first band
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, 1 );
if ( GDALGetOverviewCount( myGdalBand ) > 0 )
if ( GDALGetRasterCount( mGdalDataset ) > 0 )
{
int myOverviewInt;
for ( myOverviewInt = 0;
myOverviewInt < GDALGetOverviewCount( myGdalBand );
myOverviewInt++ )
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, 1 );
if ( GDALGetOverviewCount( myGdalBand ) > 0 )
{
GDALRasterBandH myOverview;
myOverview = GDALGetOverview( myGdalBand, myOverviewInt );
myMetadata += "<p>X : " + QString::number( GDALGetRasterBandXSize( myOverview ) );
myMetadata += ",Y " + QString::number( GDALGetRasterBandYSize( myOverview ) ) + "</p>";
int myOverviewInt;
for ( myOverviewInt = 0;
myOverviewInt < GDALGetOverviewCount( myGdalBand );
myOverviewInt++ )
{
GDALRasterBandH myOverview;
myOverview = GDALGetOverview( myGdalBand, myOverviewInt );
myMetadata += "<p>X : " + QString::number( GDALGetRasterBandXSize( myOverview ) );
myMetadata += ",Y " + QString::number( GDALGetRasterBandYSize( myOverview ) ) + "</p>";
}
}
}
myMetadata += "</p>\n";

if ( GDALGetGeoTransform( mGdalDataset, mGeoTransform ) != CE_None )
{
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ADD_QGIS_TEST(filewritertest testqgsvectorfilewriter.cpp)
ADD_QGIS_TEST(regression992 regression992.cpp)
ADD_QGIS_TEST(regression1141 regression1141.cpp)
ADD_QGIS_TEST(rasterlayertest testqgsrasterlayer.cpp)
ADD_QGIS_TEST(rastersublayertest testqgsrastersublayer.cpp)
ADD_QGIS_TEST(rasterfilewritertest testqgsrasterfilewriter.cpp)
ADD_QGIS_TEST(contrastenhancementtest testcontrastenhancements.cpp)
ADD_QGIS_TEST(maplayertest testqgsmaplayer.cpp)
Expand Down
171 changes: 171 additions & 0 deletions tests/src/core/testqgsrastersublayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/***************************************************************************
testqgsrastersublayer.cpp
--------------------------------------
Date : Dec 2012
Copyright : (C) 2012 by Radim Blazek
Email : radim.blazek@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QObject>
#include <iostream>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QPainter>
#include <QTime>
#include <QDesktopServices>

#include <gdal.h>

//qgis includes...
#include <qgsrasterlayer.h>
#include <qgsrasterpyramid.h>
#include <qgsrasterbandstats.h>
#include <qgsrasterpyramid.h>
#include <qgsmaplayerregistry.h>
#include <qgsapplication.h>
#include <qgsmaprenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgssinglebandgrayrenderer.h>
#include <qgssinglebandpseudocolorrenderer.h>
#include <qgsvectorcolorrampv2.h>
#include <qgscptcityarchive.h>

//qgis unit test includes
#include <qgsrenderchecker.h>

/** \ingroup UnitTests
* This is a unit test for raster sublayers
*/
class TestQgsRasterSubLayer: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {};// will be called before each testfunction is executed.
void cleanup() {};// will be called after every testfunction.

void subLayersList();
void checkStats();
private:
QString mTestDataDir;
QString mFileName;
QgsRasterLayer * mpRasterLayer;
QString mReport;
bool mHasNetCDF;
};

//runs before all tests
void TestQgsRasterSubLayer::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
// disable any PAM stuff to make sure stats are consistent
CPLSetConfigOption( "GDAL_PAM_ENABLED", "NO" );
QString mySettings = QgsApplication::showSettings();
mySettings = mySettings.replace( "\n", "<br />" );
mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt

GDALAllRegister();
QString format = "netCDF";
GDALDriverH myGdalDriver = GDALGetDriverByName( format.toLocal8Bit().constData() );
mHasNetCDF = myGdalDriver != 0;

mFileName = mTestDataDir + "landsat2.nc";

mReport += "<h1>Raster Sub Layer Tests</h1>\n";
//mReport += "<p>" + mySettings + "</p>";

if ( mHasNetCDF )
{
QFileInfo myRasterFileInfo( mFileName );
mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
myRasterFileInfo.completeBaseName() );
qDebug() << "raster metadata: " << mpRasterLayer->dataProvider()->metadata();
mReport += "raster metadata: " + mpRasterLayer->dataProvider()->metadata();

}
else
{
mReport += "<p>NetCDF format is not compiled in GDAL library, cannot test sub layers.</p>";
}
}

//runs after all tests
void TestQgsRasterSubLayer::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
if ( mHasNetCDF ) delete mpRasterLayer;
}

void TestQgsRasterSubLayer::subLayersList()
{
if ( mHasNetCDF )
{
mReport += "<h2>Check Sublayers List</h2>\n";
// Layer with sublayers is not valid
//QVERIFY( mpRasterLayer->isValid() );
QStringList expected;
expected << "NETCDF:\"" + mFileName + "\":Band1";
expected << "NETCDF:\"" + mFileName + "\":Band2";

QStringList sublayers = mpRasterLayer->subLayers();
mReport += QString( "sublayers:<br>%1<br>\n" ).arg( sublayers.join("<br>") );
mReport += QString( "expected:<br>%1<br>\n" ).arg( expected.join("<br>") );
QVERIFY( sublayers == expected );
mReport += "<p>Passed</p>";
}
}

void TestQgsRasterSubLayer::checkStats()
{
if ( mHasNetCDF )
{
mReport += "<h2>Check Stats</h2>\n";
QString sublayerUri = mpRasterLayer->subLayers().value(0);
mReport += "sublayer: " + sublayerUri + "<br>\n";

QgsRasterLayer *sublayer = new QgsRasterLayer( sublayerUri, "Sublayer 1" );

QgsRasterBandStats myStatistics = sublayer->dataProvider()->bandStatistics( 1,
QgsRasterBandStats::Min | QgsRasterBandStats::Max );
int width = 200;
int height = 200;
double min = 122;
double max = 130;
mReport += QString( "width = %1 expected = %2<br>\n" ).arg( sublayer->width() ).arg( width );
mReport += QString( "height = %1 expected = %2<br>\n" ).arg( sublayer->height() ).arg( height );
mReport += QString( "min = %1 expected = %2<br>\n" ).arg( myStatistics.minimumValue ).arg( min );
mReport += QString( "max = %1 expected = %2<br>\n" ).arg( myStatistics.maximumValue ).arg( max );

QVERIFY( sublayer->width() == width );
QVERIFY( sublayer->height() == height );
QVERIFY( doubleNear ( myStatistics.minimumValue, min ) );
QVERIFY( doubleNear ( myStatistics.maximumValue, max ) );
mReport += "<p>Passed</p>";
delete sublayer;
}
}


QTEST_MAIN( TestQgsRasterSubLayer )
#include "moc_testqgsrastersublayer.cxx"
Binary file added tests/testdata/landsat2.nc
Binary file not shown.

0 comments on commit 482a007

Please sign in to comment.