-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.