~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to tests/src/core/testqgsrasterlayer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
     testqgsvectorfilewriter.cpp
 
3
     --------------------------------------
 
4
    Date                 : Frida  Nov 23  2007
 
5
    Copyright            : (C) 2007 by Tim Sutton
 
6
    Email                : tim@linfiniti.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 <QSettings>
 
26
#include <QTime>
 
27
#include <QDesktopServices>
 
28
 
 
29
 
 
30
//qgis includes...
 
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>
 
38
 
 
39
//qgis unit test includes
 
40
#include <qgsrenderchecker.h>
 
41
 
 
42
 
 
43
/** \ingroup UnitTests
 
44
 * This is a unit test for the QgsRasterLayer class.
 
45
 */
 
46
class TestQgsRasterLayer: public QObject
 
47
{
 
48
    Q_OBJECT;
 
49
  private slots:
 
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.
 
54
 
 
55
    void isValid();
 
56
    void pseudoColor();
 
57
    void landsatBasic();
 
58
    void landsatBasic875Qml();
 
59
    void checkDimensions();
 
60
    void buildExternalOverviews();
 
61
    void registry();
 
62
  private:
 
63
    bool render( QString theFileName );
 
64
    bool setQml( QString theType );
 
65
    QString mTestDataDir;
 
66
    QgsRasterLayer * mpRasterLayer;
 
67
    QgsRasterLayer * mpLandsatRasterLayer;
 
68
    QgsMapRenderer * mpMapRenderer;
 
69
    QString mReport;
 
70
};
 
71
 
 
72
//runs before all tests
 
73
void TestQgsRasterLayer::initTestCase()
 
74
{
 
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();
 
95
  QStringList myLayers;
 
96
  myLayers << mpRasterLayer->getLayerID();
 
97
  mpMapRenderer->setLayerSet( myLayers );
 
98
  mReport += "<h1>Raster Layer Tests</h1>\n";
 
99
}
 
100
//runs after all tests
 
101
void TestQgsRasterLayer::cleanupTestCase()
 
102
{
 
103
  QString myReportFile = QDir::tempPath() + QDir::separator() + "rastertest.html";
 
104
  QFile myFile( myReportFile );
 
105
  if ( myFile.open( QIODevice::WriteOnly ) )
 
106
  {
 
107
    QTextStream myQTextStream( &myFile );
 
108
    myQTextStream << mReport;
 
109
    myFile.close();
 
110
    QDesktopServices::openUrl( "file://" + myReportFile );
 
111
  }
 
112
 
 
113
}
 
114
 
 
115
void TestQgsRasterLayer::isValid()
 
116
{
 
117
  QVERIFY( mpRasterLayer->isValid() );
 
118
  mpMapRenderer->setExtent( mpRasterLayer->extent() );
 
119
  QVERIFY( render( "raster" ) );
 
120
}
 
121
 
 
122
void TestQgsRasterLayer::pseudoColor()
 
123
{
 
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" ) );
 
132
}
 
133
 
 
134
void TestQgsRasterLayer::landsatBasic()
 
135
{
 
136
  QStringList myLayers;
 
137
  myLayers << mpLandsatRasterLayer->getLayerID();
 
138
  mpMapRenderer->setLayerSet( myLayers );
 
139
  mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
 
140
  QVERIFY( render( "landsat_basic" ) );
 
141
}
 
142
void TestQgsRasterLayer::landsatBasic875Qml()
 
143
{
 
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" ) );
 
151
}
 
152
void TestQgsRasterLayer::checkDimensions()
 
153
{
 
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 );
 
159
}
 
160
 
 
161
void TestQgsRasterLayer::buildExternalOverviews()
 
162
{
 
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
 
165
 
 
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() );
 
172
 
 
173
 
 
174
  //
 
175
  // Ok now we can go on to test
 
176
  //
 
177
 
 
178
  bool myInternalFlag = false;
 
179
  QgsRasterLayer::RasterPyramidList myPyramidList = mypLayer->buildPyramidList();
 
180
  for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
 
181
  {
 
182
    //mark to be pyramided
 
183
    myPyramidList[myCounterInt].build = true;
 
184
  }
 
185
  //now actually make the pyramids
 
186
  QString myResult = mypLayer->buildPyramids(
 
187
                       myPyramidList,
 
188
                       "NEAREST",
 
189
                       myInternalFlag
 
190
                     );
 
191
  qDebug( "%s", myResult.toLocal8Bit().constData() );
 
192
  //
 
193
  // Lets verify we have pyramids now...
 
194
  //
 
195
  myPyramidList = mypLayer->buildPyramidList();
 
196
  for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
 
197
  {
 
198
    //mark to be pyramided
 
199
    QVERIFY( myPyramidList.at( myCounterInt ).exists );
 
200
  }
 
201
 
 
202
  //
 
203
  // And that they were indeed in an external file...
 
204
  //
 
205
  QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );
 
206
  //cleanup
 
207
  delete mypLayer;
 
208
}
 
209
 
 
210
 
 
211
void TestQgsRasterLayer::registry()
 
212
{
 
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() );
 
219
 
 
220
  QgsMapLayerRegistry::instance()->addMapLayer( mypLayer, false );
 
221
  QgsMapLayerRegistry::instance()->removeMapLayer( mypLayer->getLayerID() );
 
222
  //cleanup
 
223
  //delete mypLayer;
 
224
}
 
225
 
 
226
//
 
227
// Helper methods
 
228
//
 
229
 
 
230
 
 
231
bool TestQgsRasterLayer::render( QString theTestType )
 
232
{
 
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();
 
241
  return myResultFlag;
 
242
}
 
243
 
 
244
bool TestQgsRasterLayer::setQml( QString theType )
 
245
{
 
246
  //load a qml style and apply to our layer
 
247
  // huh? this is failing but shouldnt!
 
248
  //if (! mpLandsatRasterLayer->isValid() )
 
249
  //{
 
250
  //  qDebug(" **** setQml -> mpLandsatRasterLayer is invalid");
 
251
  //  return false;
 
252
  //}
 
253
  QString myFileName = mTestDataDir + "landsat_" + theType + ".qml";
 
254
  bool myStyleFlag = false;
 
255
  mpLandsatRasterLayer->loadNamedStyle( myFileName, myStyleFlag );
 
256
  if ( !myStyleFlag )
 
257
  {
 
258
    qDebug( " **** setQml -> mpLandsatRasterLayer is invalid" );
 
259
    qDebug( "Qml File :%s", myFileName.toLocal8Bit().constData() );
 
260
  }
 
261
  return myStyleFlag;
 
262
}
 
263
 
 
264
QTEST_MAIN( TestQgsRasterLayer )
 
265
#include "moc_testqgsrasterlayer.cxx"
 
266