1
/***************************************************************************
2
testqgsvectorfilewriter.cpp
3
--------------------------------------
4
Date : Frida Nov 23 2007
5
Copyright : (C) 2007 by Tim Sutton
6
Email : tim@linfiniti.com
7
***************************************************************************
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. *
14
***************************************************************************/
18
#include <QStringList>
21
#include <QApplication>
27
#include <QDesktopServices>
31
#include <qgsrasterlayer.h>
32
#include <qgsrasterpyramid.h>
33
#include <qgsrasterbandstats.h>
34
#include <qgsmaplayerregistry.h>
35
#include <qgsapplication.h>
36
#include <qgsmaprenderer.h>
37
#include <qgsmaplayerregistry.h>
39
//qgis unit test includes
40
#include <qgsrenderchecker.h>
43
/** \ingroup UnitTests
44
* This is a unit test for the QgsRasterLayer class.
46
class TestQgsRasterLayer: public QObject
50
void initTestCase();// will be called before the first testfunction is executed.
51
void cleanupTestCase();// will be called after the last testfunction was executed.
52
void init() {};// will be called before each testfunction is executed.
53
void cleanup() {};// will be called after every testfunction.
58
void landsatBasic875Qml();
59
void checkDimensions();
60
void buildExternalOverviews();
63
bool render( QString theFileName );
64
bool setQml( QString theType );
66
QgsRasterLayer * mpRasterLayer;
67
QgsRasterLayer * mpLandsatRasterLayer;
68
QgsMapRenderer * mpMapRenderer;
72
//runs before all tests
73
void TestQgsRasterLayer::initTestCase()
75
// init QGIS's paths - true means that all path will be inited from prefix
76
QString qgisPath = QCoreApplication::applicationDirPath();
77
QgsApplication::setPrefixPath( INSTALL_PREFIX, true );
78
QgsApplication::showSettings();
79
//create some objects that will be used in all tests...
80
//create a raster layer that will be used in all tests...
81
mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt
82
QString myFileName = mTestDataDir + "tenbytenraster.asc";
83
QString myLandsatFileName = mTestDataDir + "landsat.tif";
84
QFileInfo myRasterFileInfo( myFileName );
85
mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
86
myRasterFileInfo.completeBaseName() );
87
QFileInfo myLandsatRasterFileInfo( myLandsatFileName );
88
mpLandsatRasterLayer = new QgsRasterLayer( myLandsatRasterFileInfo.filePath(),
89
myLandsatRasterFileInfo.completeBaseName() );
90
// Register the layer with the registry
91
QgsMapLayerRegistry::instance()->addMapLayer( mpRasterLayer );
92
QgsMapLayerRegistry::instance()->addMapLayer( mpLandsatRasterLayer );
93
// add the test layer to the maprender
94
mpMapRenderer = new QgsMapRenderer();
96
myLayers << mpRasterLayer->getLayerID();
97
mpMapRenderer->setLayerSet( myLayers );
98
mReport += "<h1>Raster Layer Tests</h1>\n";
100
//runs after all tests
101
void TestQgsRasterLayer::cleanupTestCase()
103
QString myReportFile = QDir::tempPath() + QDir::separator() + "rastertest.html";
104
QFile myFile( myReportFile );
105
if ( myFile.open( QIODevice::WriteOnly ) )
107
QTextStream myQTextStream( &myFile );
108
myQTextStream << mReport;
110
QDesktopServices::openUrl( "file://" + myReportFile );
115
void TestQgsRasterLayer::isValid()
117
QVERIFY( mpRasterLayer->isValid() );
118
mpMapRenderer->setExtent( mpRasterLayer->extent() );
119
QVERIFY( render( "raster" ) );
122
void TestQgsRasterLayer::pseudoColor()
124
mpRasterLayer->setDrawingStyle( QgsRasterLayer::SingleBandPseudoColor );
125
mpRasterLayer->setColorShadingAlgorithm( QgsRasterLayer::PseudoColorShader );
126
mpRasterLayer->setContrastEnhancementAlgorithm(
127
QgsContrastEnhancement::StretchToMinimumMaximum, false );
128
mpRasterLayer->setMinimumValue( mpRasterLayer->grayBandName(), 0.0, false );
129
mpRasterLayer->setMaximumValue( mpRasterLayer->grayBandName(), 10.0 );
130
mpMapRenderer->setExtent( mpRasterLayer->extent() );
131
QVERIFY( render( "raster_pseudo" ) );
134
void TestQgsRasterLayer::landsatBasic()
136
QStringList myLayers;
137
myLayers << mpLandsatRasterLayer->getLayerID();
138
mpMapRenderer->setLayerSet( myLayers );
139
mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
140
QVERIFY( render( "landsat_basic" ) );
142
void TestQgsRasterLayer::landsatBasic875Qml()
144
//a qml that orders the rgb bands as 8,7,5
145
QStringList myLayers;
146
myLayers << mpLandsatRasterLayer->getLayerID();
147
mpMapRenderer->setLayerSet( myLayers );
148
mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
149
QVERIFY( setQml( "875" ) );
150
QVERIFY( render( "landsat_875" ) );
152
void TestQgsRasterLayer::checkDimensions()
154
QVERIFY( mpRasterLayer->width() == 10 );
155
QVERIFY( mpRasterLayer->height() == 10 );
156
// regression check for ticket #832
157
// note bandStatistics call is base 1
158
QVERIFY( mpRasterLayer->bandStatistics( 1 ).elementCount == 100 );
161
void TestQgsRasterLayer::buildExternalOverviews()
163
//before we begin delete any old ovr file (if it exists)
164
//and make a copy of the landsat raster into the temp dir
166
QString myTempPath = QDir::tempPath() + QDir::separator();
167
QFile::remove( myTempPath + "landsat.tif.ovr" );
168
QFile::copy( mTestDataDir + "landsat.tif", myTempPath + "landsat.tif" );
169
QFileInfo myRasterFileInfo( myTempPath + "landsat.tif" );
170
QgsRasterLayer * mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
171
myRasterFileInfo.completeBaseName() );
175
// Ok now we can go on to test
178
bool myInternalFlag = false;
179
QgsRasterLayer::RasterPyramidList myPyramidList = mypLayer->buildPyramidList();
180
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
182
//mark to be pyramided
183
myPyramidList[myCounterInt].build = true;
185
//now actually make the pyramids
186
QString myResult = mypLayer->buildPyramids(
191
qDebug( "%s", myResult.toLocal8Bit().constData() );
193
// Lets verify we have pyramids now...
195
myPyramidList = mypLayer->buildPyramidList();
196
for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
198
//mark to be pyramided
199
QVERIFY( myPyramidList.at( myCounterInt ).exists );
203
// And that they were indeed in an external file...
205
QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );
211
void TestQgsRasterLayer::registry()
213
QString myTempPath = QDir::tempPath() + QDir::separator();
214
QFile::remove( myTempPath + "landsat.tif.ovr" );
215
QFile::copy( mTestDataDir + "landsat.tif", myTempPath + "landsat.tif" );
216
QFileInfo myRasterFileInfo( myTempPath + "landsat.tif" );
217
QgsRasterLayer * mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
218
myRasterFileInfo.completeBaseName() );
220
QgsMapLayerRegistry::instance()->addMapLayer( mypLayer, false );
221
QgsMapLayerRegistry::instance()->removeMapLayer( mypLayer->getLayerID() );
231
bool TestQgsRasterLayer::render( QString theTestType )
233
mReport += "<h2>" + theTestType + "</h2>\n";
234
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
235
QString myTestDataDir = myDataDir + QDir::separator();
236
QgsRenderChecker myChecker;
237
myChecker.setExpectedImage( myTestDataDir + "expected_" + theTestType + ".png" );
238
myChecker.setMapRenderer( mpMapRenderer );
239
bool myResultFlag = myChecker.runTest( theTestType );
240
mReport += "\n\n\n" + myChecker.report();
244
bool TestQgsRasterLayer::setQml( QString theType )
246
//load a qml style and apply to our layer
247
// huh? this is failing but shouldnt!
248
//if (! mpLandsatRasterLayer->isValid() )
250
// qDebug(" **** setQml -> mpLandsatRasterLayer is invalid");
253
QString myFileName = mTestDataDir + "landsat_" + theType + ".qml";
254
bool myStyleFlag = false;
255
mpLandsatRasterLayer->loadNamedStyle( myFileName, myStyleFlag );
258
qDebug( " **** setQml -> mpLandsatRasterLayer is invalid" );
259
qDebug( "Qml File :%s", myFileName.toLocal8Bit().constData() );
264
QTEST_MAIN( TestQgsRasterLayer )
265
#include "moc_testqgsrasterlayer.cxx"