Skip to content

Commit 482a007

Browse files
committedDec 20, 2012
NetCDF sublayers test
1 parent e242af4 commit 482a007

File tree

4 files changed

+185
-11
lines changed

4 files changed

+185
-11
lines changed
 

‎src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,23 @@ QString QgsGdalProvider::metadata()
306306
myMetadata += "</p>\n";
307307

308308
//just use the first band
309-
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, 1 );
310-
if ( GDALGetOverviewCount( myGdalBand ) > 0 )
309+
if ( GDALGetRasterCount( mGdalDataset ) > 0 )
311310
{
312-
int myOverviewInt;
313-
for ( myOverviewInt = 0;
314-
myOverviewInt < GDALGetOverviewCount( myGdalBand );
315-
myOverviewInt++ )
311+
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, 1 );
312+
if ( GDALGetOverviewCount( myGdalBand ) > 0 )
316313
{
317-
GDALRasterBandH myOverview;
318-
myOverview = GDALGetOverview( myGdalBand, myOverviewInt );
319-
myMetadata += "<p>X : " + QString::number( GDALGetRasterBandXSize( myOverview ) );
320-
myMetadata += ",Y " + QString::number( GDALGetRasterBandYSize( myOverview ) ) + "</p>";
314+
int myOverviewInt;
315+
for ( myOverviewInt = 0;
316+
myOverviewInt < GDALGetOverviewCount( myGdalBand );
317+
myOverviewInt++ )
318+
{
319+
GDALRasterBandH myOverview;
320+
myOverview = GDALGetOverview( myGdalBand, myOverviewInt );
321+
myMetadata += "<p>X : " + QString::number( GDALGetRasterBandXSize( myOverview ) );
322+
myMetadata += ",Y " + QString::number( GDALGetRasterBandYSize( myOverview ) ) + "</p>";
323+
}
321324
}
322325
}
323-
myMetadata += "</p>\n";
324326

325327
if ( GDALGetGeoTransform( mGdalDataset, mGeoTransform ) != CE_None )
326328
{

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ ADD_QGIS_TEST(filewritertest testqgsvectorfilewriter.cpp)
8787
ADD_QGIS_TEST(regression992 regression992.cpp)
8888
ADD_QGIS_TEST(regression1141 regression1141.cpp)
8989
ADD_QGIS_TEST(rasterlayertest testqgsrasterlayer.cpp)
90+
ADD_QGIS_TEST(rastersublayertest testqgsrastersublayer.cpp)
9091
ADD_QGIS_TEST(rasterfilewritertest testqgsrasterfilewriter.cpp)
9192
ADD_QGIS_TEST(contrastenhancementtest testcontrastenhancements.cpp)
9293
ADD_QGIS_TEST(maplayertest testqgsmaplayer.cpp)
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/***************************************************************************
2+
testqgsrastersublayer.cpp
3+
--------------------------------------
4+
Date : Dec 2012
5+
Copyright : (C) 2012 by Radim Blazek
6+
Email : radim.blazek@gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QtTest>
16+
#include <QObject>
17+
#include <QString>
18+
#include <QStringList>
19+
#include <QObject>
20+
#include <iostream>
21+
#include <QApplication>
22+
#include <QFileInfo>
23+
#include <QDir>
24+
#include <QPainter>
25+
#include <QTime>
26+
#include <QDesktopServices>
27+
28+
#include <gdal.h>
29+
30+
//qgis includes...
31+
#include <qgsrasterlayer.h>
32+
#include <qgsrasterpyramid.h>
33+
#include <qgsrasterbandstats.h>
34+
#include <qgsrasterpyramid.h>
35+
#include <qgsmaplayerregistry.h>
36+
#include <qgsapplication.h>
37+
#include <qgsmaprenderer.h>
38+
#include <qgsmaplayerregistry.h>
39+
#include <qgssinglebandgrayrenderer.h>
40+
#include <qgssinglebandpseudocolorrenderer.h>
41+
#include <qgsvectorcolorrampv2.h>
42+
#include <qgscptcityarchive.h>
43+
44+
//qgis unit test includes
45+
#include <qgsrenderchecker.h>
46+
47+
/** \ingroup UnitTests
48+
* This is a unit test for raster sublayers
49+
*/
50+
class TestQgsRasterSubLayer: public QObject
51+
{
52+
Q_OBJECT;
53+
private slots:
54+
void initTestCase();// will be called before the first testfunction is executed.
55+
void cleanupTestCase();// will be called after the last testfunction was executed.
56+
void init() {};// will be called before each testfunction is executed.
57+
void cleanup() {};// will be called after every testfunction.
58+
59+
void subLayersList();
60+
void checkStats();
61+
private:
62+
QString mTestDataDir;
63+
QString mFileName;
64+
QgsRasterLayer * mpRasterLayer;
65+
QString mReport;
66+
bool mHasNetCDF;
67+
};
68+
69+
//runs before all tests
70+
void TestQgsRasterSubLayer::initTestCase()
71+
{
72+
// init QGIS's paths - true means that all path will be inited from prefix
73+
QgsApplication::init();
74+
QgsApplication::initQgis();
75+
// disable any PAM stuff to make sure stats are consistent
76+
CPLSetConfigOption( "GDAL_PAM_ENABLED", "NO" );
77+
QString mySettings = QgsApplication::showSettings();
78+
mySettings = mySettings.replace( "\n", "<br />" );
79+
mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt
80+
81+
GDALAllRegister();
82+
QString format = "netCDF";
83+
GDALDriverH myGdalDriver = GDALGetDriverByName( format.toLocal8Bit().constData() );
84+
mHasNetCDF = myGdalDriver != 0;
85+
86+
mFileName = mTestDataDir + "landsat2.nc";
87+
88+
mReport += "<h1>Raster Sub Layer Tests</h1>\n";
89+
//mReport += "<p>" + mySettings + "</p>";
90+
91+
if ( mHasNetCDF )
92+
{
93+
QFileInfo myRasterFileInfo( mFileName );
94+
mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
95+
myRasterFileInfo.completeBaseName() );
96+
qDebug() << "raster metadata: " << mpRasterLayer->dataProvider()->metadata();
97+
mReport += "raster metadata: " + mpRasterLayer->dataProvider()->metadata();
98+
99+
}
100+
else
101+
{
102+
mReport += "<p>NetCDF format is not compiled in GDAL library, cannot test sub layers.</p>";
103+
}
104+
}
105+
106+
//runs after all tests
107+
void TestQgsRasterSubLayer::cleanupTestCase()
108+
{
109+
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
110+
QFile myFile( myReportFile );
111+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
112+
{
113+
QTextStream myQTextStream( &myFile );
114+
myQTextStream << mReport;
115+
myFile.close();
116+
}
117+
if ( mHasNetCDF ) delete mpRasterLayer;
118+
}
119+
120+
void TestQgsRasterSubLayer::subLayersList()
121+
{
122+
if ( mHasNetCDF )
123+
{
124+
mReport += "<h2>Check Sublayers List</h2>\n";
125+
// Layer with sublayers is not valid
126+
//QVERIFY( mpRasterLayer->isValid() );
127+
QStringList expected;
128+
expected << "NETCDF:\"" + mFileName + "\":Band1";
129+
expected << "NETCDF:\"" + mFileName + "\":Band2";
130+
131+
QStringList sublayers = mpRasterLayer->subLayers();
132+
mReport += QString( "sublayers:<br>%1<br>\n" ).arg( sublayers.join("<br>") );
133+
mReport += QString( "expected:<br>%1<br>\n" ).arg( expected.join("<br>") );
134+
QVERIFY( sublayers == expected );
135+
mReport += "<p>Passed</p>";
136+
}
137+
}
138+
139+
void TestQgsRasterSubLayer::checkStats()
140+
{
141+
if ( mHasNetCDF )
142+
{
143+
mReport += "<h2>Check Stats</h2>\n";
144+
QString sublayerUri = mpRasterLayer->subLayers().value(0);
145+
mReport += "sublayer: " + sublayerUri + "<br>\n";
146+
147+
QgsRasterLayer *sublayer = new QgsRasterLayer( sublayerUri, "Sublayer 1" );
148+
149+
QgsRasterBandStats myStatistics = sublayer->dataProvider()->bandStatistics( 1,
150+
QgsRasterBandStats::Min | QgsRasterBandStats::Max );
151+
int width = 200;
152+
int height = 200;
153+
double min = 122;
154+
double max = 130;
155+
mReport += QString( "width = %1 expected = %2<br>\n" ).arg( sublayer->width() ).arg( width );
156+
mReport += QString( "height = %1 expected = %2<br>\n" ).arg( sublayer->height() ).arg( height );
157+
mReport += QString( "min = %1 expected = %2<br>\n" ).arg( myStatistics.minimumValue ).arg( min );
158+
mReport += QString( "max = %1 expected = %2<br>\n" ).arg( myStatistics.maximumValue ).arg( max );
159+
160+
QVERIFY( sublayer->width() == width );
161+
QVERIFY( sublayer->height() == height );
162+
QVERIFY( doubleNear ( myStatistics.minimumValue, min ) );
163+
QVERIFY( doubleNear ( myStatistics.maximumValue, max ) );
164+
mReport += "<p>Passed</p>";
165+
delete sublayer;
166+
}
167+
}
168+
169+
170+
QTEST_MAIN( TestQgsRasterSubLayer )
171+
#include "moc_testqgsrastersublayer.cxx"

‎tests/testdata/landsat2.nc

83.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.