-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
testqgsrasterfilewriter.cpp
189 lines (164 loc) · 5.92 KB
/
testqgsrasterfilewriter.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
184
185
186
187
188
189
/***************************************************************************
testqgsrasterfilewriter.cpp
--------------------------------------
Date : 5 Sep 2012
Copyright : (C) 2012 by Radim Blazek
Email : radim dot blazek at gmail dot 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 "cpl_conv.h"
//qgis includes...
#include <qgsrasterchecker.h>
#include <qgsrasterlayer.h>
#include <qgsrasterfilewriter.h>
#include <qgsrasternuller.h>
#include <qgsapplication.h>
/** \ingroup UnitTests
* This is a unit test for the QgsRasterFileWriter class.
*/
class TestQgsRasterFileWriter: 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 writeTest();
private:
bool writeTest( QString rasterName );
void log( QString msg );
void logError( QString msg );
QString mTestDataDir;
QString mReport;
};
//runs before all tests
void TestQgsRasterFileWriter::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 />" );
//create some objects that will be used in all tests...
//create a raster layer that will be used in all tests...
mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt
mReport += "<h1>Raster File Writer Tests</h1>\n";
mReport += "<p>" + mySettings + "</p>";
}
//runs after all tests
void TestQgsRasterFileWriter::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();
}
}
void TestQgsRasterFileWriter::writeTest()
{
QDir dir( mTestDataDir + "/raster" );
QStringList filters;
filters << "*.tif";
QStringList rasterNames = dir.entryList( filters, QDir::Files );
bool allOK = true;
foreach ( QString rasterName, rasterNames )
{
bool ok = writeTest( "raster/" + rasterName );
if ( !ok ) allOK = false;
}
QVERIFY( allOK );
}
bool TestQgsRasterFileWriter::writeTest( QString theRasterName )
{
mReport += "<h2>" + theRasterName + "</h2>\n";
QString myFileName = mTestDataDir + "/" + theRasterName;
qDebug() << myFileName;
QFileInfo myRasterFileInfo( myFileName );
QgsRasterLayer * mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
myRasterFileInfo.completeBaseName() );
qDebug() << theRasterName << " metadata: " << mpRasterLayer->dataProvider()->metadata();
if ( !mpRasterLayer->isValid() ) return false;
// Open provider only (avoid layer)?
QgsRasterDataProvider * provider = mpRasterLayer->dataProvider();
// I don't see any method to get only a name without opening file
QTemporaryFile tmpFile;
tmpFile.open(); // fileName is no avialable until open
QString tmpName = tmpFile.fileName();
tmpFile.close();
// do not remove when class is destroyd so that we can read the file and see difference
tmpFile.setAutoRemove( false );
qDebug() << "temporary output file: " << tmpName;
mReport += "temporary output file: " + tmpName + "<br>";
QgsRasterFileWriter fileWriter( tmpName );
QgsRasterPipe* pipe = new QgsRasterPipe();
if ( !pipe->set( provider->clone() ) )
{
logError( "Cannot set pipe provider" );
return false;
}
qDebug() << "provider set";
// Nuller currently is not really used
QgsRasterNuller *nuller = new QgsRasterNuller();
for ( int band = 1; band <= provider->bandCount(); band++ )
{
//nuller->setNoData( ... );
}
if ( !pipe->insert( 1, nuller ) )
{
logError( "Cannot set pipe nuller" );
return false;
}
qDebug() << "nuller set";
// Reprojection not really done
QgsRasterProjector *projector = new QgsRasterProjector;
projector->setCRS( provider->crs(), provider->crs() );
if ( !pipe->insert( 2, projector ) )
{
logError( "Cannot set pipe projector" );
return false;
}
qDebug() << "projector set";
fileWriter.writeRaster( pipe, provider->xSize(), provider->ySize(), provider->extent(), provider->crs() );
delete pipe;
QgsRasterChecker checker;
bool ok = checker.runTest( "gdal", tmpName, "gdal", myRasterFileInfo.filePath() );
mReport += checker.report();
// All OK, we can delete the file
tmpFile.setAutoRemove( ok );
return ok;
}
void TestQgsRasterFileWriter::log( QString msg )
{
mReport += msg + "<br>";
}
void TestQgsRasterFileWriter::logError( QString msg )
{
mReport += "Error:<font color='red'>" + msg + "</font><br>";
qDebug() << msg;
}
QTEST_MAIN( TestQgsRasterFileWriter )
#include "testqgsrasterfilewriter.moc"