/
testqgsrastersublayer.cpp
183 lines (161 loc) · 6.43 KB
/
testqgsrastersublayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
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 "qgstest.h"
#include <QObject>
#include <QString>
#include <QStringList>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QPainter>
#include <QTime>
#include <QDesktopServices>
#include <gdal.h>
#include "cpl_conv.h"
//qgis includes...
#include <qgsrasterlayer.h>
#include <qgsrasterpyramid.h>
#include <qgsrasterbandstats.h>
#include "qgsrasterdataprovider.h"
#include <qgsapplication.h>
#include <qgssinglebandgrayrenderer.h>
#include <qgssinglebandpseudocolorrenderer.h>
#include <qgscolorramp.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
public:
TestQgsRasterSubLayer();
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 = nullptr;
QString mReport;
bool mHasNetCDF = false ;
};
TestQgsRasterSubLayer::TestQgsRasterSubLayer() = default;
//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', QLatin1String( "<br />" ) );
mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
GDALAllRegister();
QString format = QStringLiteral( "netCDF" );
GDALDriverH myGdalDriver = GDALGetDriverByName( format.toLocal8Bit().constData() );
mHasNetCDF = myGdalDriver != 0;
mFileName = mTestDataDir + "landsat2.nc";
mReport += QLatin1String( "<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()->htmlMetadata();
mReport += "raster metadata: " + mpRasterLayer->dataProvider()->htmlMetadata();
}
else
{
mReport += QLatin1String( "<p>NetCDF format is not compiled in GDAL library, cannot test sub layers.</p>" );
}
}
//runs after all tests
void TestQgsRasterSubLayer::cleanupTestCase()
{
delete mpRasterLayer;
QgsApplication::exitQgis();
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}
void TestQgsRasterSubLayer::subLayersList()
{
if ( mHasNetCDF )
{
mReport += QLatin1String( "<h2>Check Sublayers List</h2>\n" );
// Layer with sublayers is not valid
//QVERIFY( mpRasterLayer->isValid() );
QStringList expected;
// Sublayer format: NETCDF:"/path/to/landsat2.nc":Band1
// NETCDF:"c:/path/to/landsat2.nc":Band1
// File path is delicate on Windows -> compare only sublayers
expected << QStringLiteral( "Band1" );
expected << QStringLiteral( "Band2" );
QStringList sublayers;
Q_FOREACH ( const QString &s, mpRasterLayer->subLayers() )
{
qDebug() << "sublayer: " << s;
sublayers << s.split( ':' ).last();
}
qDebug() << "sublayers: " << sublayers.join( QStringLiteral( "," ) );
mReport += QStringLiteral( "sublayers:<br>%1<br>\n" ).arg( sublayers.join( QStringLiteral( "<br>" ) ) );
mReport += QStringLiteral( "expected:<br>%1<br>\n" ).arg( expected.join( QStringLiteral( "<br>" ) ) );
QVERIFY( sublayers == expected );
mReport += QLatin1String( "<p>Passed</p>" );
}
}
void TestQgsRasterSubLayer::checkStats()
{
if ( mHasNetCDF )
{
mReport += QLatin1String( "<h2>Check Stats</h2>\n" );
QString sublayerUri = mpRasterLayer->subLayers().value( 0 );
mReport += "sublayer: " + sublayerUri + "<br>\n";
QgsRasterLayer *sublayer = new QgsRasterLayer( sublayerUri, QStringLiteral( "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 += QStringLiteral( "width = %1 expected = %2<br>\n" ).arg( sublayer->width() ).arg( width );
mReport += QStringLiteral( "height = %1 expected = %2<br>\n" ).arg( sublayer->height() ).arg( height );
mReport += QStringLiteral( "min = %1 expected = %2<br>\n" ).arg( myStatistics.minimumValue ).arg( min );
mReport += QStringLiteral( "max = %1 expected = %2<br>\n" ).arg( myStatistics.maximumValue ).arg( max );
QVERIFY( sublayer->width() == width );
QVERIFY( sublayer->height() == height );
QGSCOMPARENEAR( myStatistics.minimumValue, min, 4 * DBL_EPSILON );
QGSCOMPARENEAR( myStatistics.maximumValue, max, 4 * DBL_EPSILON );
mReport += QLatin1String( "<p>Passed</p>" );
delete sublayer;
}
}
QGSTEST_MAIN( TestQgsRasterSubLayer )
#include "testqgsrastersublayer.moc"