|
| 1 | +/*************************************************************************** |
| 2 | + testqgswcsprovider.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : July 2012 |
| 5 | + Copyright : (C) 2012 by Radim Blazek |
| 6 | + Email : radim dot blazek at gmail dot 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 <cmath> |
| 16 | + |
| 17 | +#include <QtTest> |
| 18 | +#include <QObject> |
| 19 | +#include <QString> |
| 20 | +#include <QStringList> |
| 21 | +#include <QObject> |
| 22 | +#include <QApplication> |
| 23 | + |
| 24 | +#include <qgsdatasourceuri.h> |
| 25 | +#include <qgsrasterlayer.h> |
| 26 | +#include <qgsrasterdataprovider.h> |
| 27 | +#include <qgsproviderregistry.h> |
| 28 | +#include <qgsapplication.h> |
| 29 | + |
| 30 | +#define TINY_VALUE std::numeric_limits<double>::epsilon() * 20 |
| 31 | + |
| 32 | +/** \ingroup UnitTests |
| 33 | + * This is a unit test for the QgsRasterLayer class. |
| 34 | + */ |
| 35 | +class TestQgsWcsProvider: public QObject |
| 36 | +{ |
| 37 | + Q_OBJECT; |
| 38 | + private slots: |
| 39 | + void initTestCase();// will be called before the first testfunction is executed. |
| 40 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 41 | + void init() {};// will be called before each testfunction is executed. |
| 42 | + void cleanup() {};// will be called after every testfunction. |
| 43 | + |
| 44 | + void read(); |
| 45 | + private: |
| 46 | + bool read( QString identifier, QString theFilePath, QString & theReport ); |
| 47 | + // Log error in html |
| 48 | + void error( QString theMessage, QString &theReport ); |
| 49 | + // compare values and add table row in html report, set ok to false if not equal |
| 50 | + QString compareHead(); |
| 51 | + void compare( QString theParamName, int wcsVal, int gdalVal, QString &theReport, bool &theOk ); |
| 52 | + void compare( QString theParamName, double wcsVal, double gdalVal, QString &theReport, bool &theOk, double theTolerance = 0 ); |
| 53 | + void compareRow( QString theParamName, QString wcsVal, QString gdalVal, QString &theReport, bool theOk, QString theDifference = "", QString theTolerance = "" ); |
| 54 | + double tolerance( double val, int places = 6 ); |
| 55 | + QString mTestDataDir; |
| 56 | + QString mReport; |
| 57 | + QString mUrl; |
| 58 | +}; |
| 59 | + |
| 60 | +//runs before all tests |
| 61 | +void TestQgsWcsProvider::initTestCase() |
| 62 | +{ |
| 63 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 64 | + QgsApplication::init( QString() ); |
| 65 | + QgsApplication::initQgis(); |
| 66 | + QString mySettings = QgsApplication::showSettings(); |
| 67 | + mySettings = mySettings.replace( "\n", "<br />" ); |
| 68 | + mReport += "<h1>WCS provider tests</h1>\n"; |
| 69 | + mReport += "<p>" + mySettings + "</p>"; |
| 70 | + |
| 71 | + mReport += "<style>"; |
| 72 | + mReport += ".tab { border-spacing: 0px; border-width: 1px 1px 0 0; border-style: solid; }"; |
| 73 | + mReport += ".cell { border-width: 0 0 1px 1px; border-style: solid; font-size: smaller; text-align: center}"; |
| 74 | + mReport += ".ok { background: #00ff00; }"; |
| 75 | + mReport += ".err { background: #ff0000; }"; |
| 76 | + mReport += ".errmsg { color: #ff0000; }"; |
| 77 | + mReport += "</style>"; |
| 78 | + |
| 79 | + |
| 80 | + //create some objects that will be used in all tests... |
| 81 | + //create a raster layer that will be used in all tests... |
| 82 | + mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator() + "raster"; |
| 83 | + qDebug() << "mTestDataDir = " << mTestDataDir; |
| 84 | + |
| 85 | + mUrl = "http://127.0.0.1//cgi-bin/wcstest"; |
| 86 | +} |
| 87 | + |
| 88 | +//runs after all tests |
| 89 | +void TestQgsWcsProvider::cleanupTestCase() |
| 90 | +{ |
| 91 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgiswcstest.html"; |
| 92 | + QFile myFile( myReportFile ); |
| 93 | + //if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 94 | + if ( myFile.open( QIODevice::WriteOnly ) ) |
| 95 | + { |
| 96 | + QTextStream myQTextStream( &myFile ); |
| 97 | + myQTextStream << mReport; |
| 98 | + myFile.close(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void TestQgsWcsProvider::read( ) |
| 103 | +{ |
| 104 | + bool ok = true; |
| 105 | + QStringList identifiers; |
| 106 | + |
| 107 | + // identifiers in mapfile have the same name as files without .tif extension |
| 108 | + identifiers << "band1_byte_noct_epsg4326"; |
| 109 | + identifiers << "band1_int16_noct_epsg4326"; |
| 110 | + identifiers << "band1_float32_noct_epsg4326"; |
| 111 | + |
| 112 | + // How to reasonably log multiple fails within this loop? |
| 113 | + foreach( QString identifier, identifiers ) |
| 114 | + { |
| 115 | + QString filePath = mTestDataDir + QDir::separator() + identifier + ".tif"; |
| 116 | + if ( !read( identifier, filePath, mReport ) ) |
| 117 | + { |
| 118 | + ok = false; |
| 119 | + } |
| 120 | + } |
| 121 | + QVERIFY2( ok, "Reading data failed. See report for details." ); |
| 122 | +} |
| 123 | + |
| 124 | +bool TestQgsWcsProvider::read( QString theIdentifier, QString theFilePath, QString & theReport ) |
| 125 | +{ |
| 126 | + bool ok = true; |
| 127 | + QgsDataSourceURI uri; |
| 128 | + uri.setParam( "url", mUrl ); |
| 129 | + uri.setParam( "identifier", theIdentifier ); |
| 130 | + uri.setParam( "crs", "epsg:4326" ); |
| 131 | + |
| 132 | + theReport += QString( "<h2>Identifier (coverage): %1</h2>" ).arg( theIdentifier ); |
| 133 | + |
| 134 | + QgsRasterDataProvider* wcsProvider = QgsRasterLayer::loadProvider( "wcs", uri.encodedUri() ); |
| 135 | + if ( !wcsProvider || !wcsProvider->isValid() ) |
| 136 | + { |
| 137 | + error( QString( "Cannot load WCS provider with URI: %1" ).arg( QString( uri.encodedUri() ) ), theReport ); |
| 138 | + ok = false; |
| 139 | + } |
| 140 | + |
| 141 | + QgsRasterDataProvider* gdalProvider = QgsRasterLayer::loadProvider( "gdal", theFilePath ); |
| 142 | + if ( !gdalProvider || !gdalProvider->isValid() ) |
| 143 | + { |
| 144 | + error( QString( "Cannot load GDAL provider with URI: %1" ).arg( theFilePath ), theReport ); |
| 145 | + ok = false; |
| 146 | + } |
| 147 | + |
| 148 | + if ( !ok ) return false; |
| 149 | + |
| 150 | + theReport += QString( "WCS URI: %1<br>" ).arg( QString( uri.encodedUri() ).replace( "&", "&" ) ); |
| 151 | + theReport += QString( "GDAL URI: %1<br>" ).arg( theFilePath ); |
| 152 | + |
| 153 | + theReport += "<br>"; |
| 154 | + theReport += "<table class='tab'>"; |
| 155 | + theReport += compareHead(); |
| 156 | + |
| 157 | + compare( "Band count", wcsProvider->bandCount(), gdalProvider->bandCount(), theReport, ok ); |
| 158 | + |
| 159 | + compare( "Width", wcsProvider->xSize(), gdalProvider->xSize(), theReport, ok ); |
| 160 | + compare( "Height", wcsProvider->ySize(), gdalProvider->ySize(), theReport, ok ); |
| 161 | + |
| 162 | + compareRow( "Extent", wcsProvider->extent().toString(), gdalProvider->extent().toString(), theReport, wcsProvider->extent() == gdalProvider->extent() ); |
| 163 | + if ( wcsProvider->extent() != gdalProvider->extent() ) ok = false; |
| 164 | + |
| 165 | + if ( !ok ) return false; |
| 166 | + |
| 167 | + compare( "No data (NULL) value", wcsProvider->noDataValue(), gdalProvider->noDataValue(), theReport, ok ); |
| 168 | + |
| 169 | + theReport += "</table>"; |
| 170 | + |
| 171 | + |
| 172 | + bool allOk = true; |
| 173 | + for ( int band = 1; band <= gdalProvider->bandCount(); band++ ) |
| 174 | + { |
| 175 | + bool bandOk = true; |
| 176 | + theReport += QString( "<h3>Band %1</h3>" ).arg( band ); |
| 177 | + theReport += "<table class='tab'>"; |
| 178 | + theReport += compareHead(); |
| 179 | + |
| 180 | + // Data types may differ (?) |
| 181 | + bool typesOk = true; |
| 182 | + compare( "Source data type", wcsProvider->srcDataType( band ), gdalProvider->srcDataType( band ), theReport, typesOk ); |
| 183 | + compare( "Data type", wcsProvider->dataType( band ), gdalProvider->dataType( band ), theReport, typesOk ) ; |
| 184 | + |
| 185 | + bool statsOk = true; |
| 186 | + QgsRasterBandStats wcsStats = wcsProvider->bandStatistics( band ); |
| 187 | + QgsRasterBandStats gdalStats = gdalProvider->bandStatistics( band ); |
| 188 | + |
| 189 | + // Min/max may 'slightly' differ, for big numbers however, the difference may |
| 190 | + // be quite big, for example for Float32 with max -3.332e+38, the difference is 1.47338e+24 |
| 191 | + double tol = tolerance( gdalStats.minimumValue ); |
| 192 | + compare( "Minimum value", wcsStats.minimumValue, gdalStats.minimumValue, theReport, statsOk, tol ); |
| 193 | + tol = tolerance( gdalStats.maximumValue ); |
| 194 | + compare( "Maximum value", wcsStats.maximumValue, gdalStats.maximumValue, theReport, statsOk, tol ); |
| 195 | + |
| 196 | + // TODO: enable once fixed (WCS excludes nulls but GDAL does not) |
| 197 | + //compare( "Cells count", wcsStats.elementCount, gdalStats.elementCount, theReport, statsOk ); |
| 198 | + |
| 199 | + tol = tolerance( gdalStats.mean ); |
| 200 | + compare( "Mean", wcsStats.mean, gdalStats.mean, theReport, statsOk, tol ); |
| 201 | + |
| 202 | + // stdDev usually differ significantly |
| 203 | + tol = tolerance( gdalStats.stdDev, 1 ); |
| 204 | + compare( "Standard deviation", wcsStats.stdDev, gdalStats.stdDev, theReport, statsOk, tol ); |
| 205 | + |
| 206 | + theReport += "</table>"; |
| 207 | + theReport += "<br>"; |
| 208 | + |
| 209 | + if ( !bandOk ) |
| 210 | + { |
| 211 | + allOk = false; |
| 212 | + continue; |
| 213 | + } |
| 214 | + |
| 215 | + if ( !statsOk || typesOk ) |
| 216 | + { |
| 217 | + allOk = false; |
| 218 | + // create values table anyway so that values are available |
| 219 | + } |
| 220 | + |
| 221 | + theReport += "<table><tr>"; |
| 222 | + theReport += "<td>Data comparison</td>"; |
| 223 | + theReport += "<td class='cell ok' style='border: 1px solid'>correct value</td>"; |
| 224 | + theReport += "<td></td>"; |
| 225 | + theReport += "<td class='cell err' style='border: 1px solid'>wrong value<br>expected value</td></tr>"; |
| 226 | + theReport += "</tr></table>"; |
| 227 | + theReport += "<br>"; |
| 228 | + |
| 229 | + int width = gdalProvider->xSize(); |
| 230 | + int height = gdalProvider->ySize(); |
| 231 | + int blockSize = width * height * gdalProvider->typeSize( gdalProvider->dataType( band ) ) ; |
| 232 | + void * gdalData = malloc( blockSize ); |
| 233 | + void * wcsData = malloc( blockSize ); |
| 234 | + |
| 235 | + gdalProvider->readBlock( band, gdalProvider->extent(), width, height, gdalData ); |
| 236 | + wcsProvider->readBlock( band, gdalProvider->extent(), width, height, wcsData ); |
| 237 | + |
| 238 | + // compare data values |
| 239 | + QString htmlTable = "<table class='tab'>"; |
| 240 | + for ( int row = 0; row < height; row ++ ) |
| 241 | + { |
| 242 | + htmlTable += "<tr>"; |
| 243 | + for ( int col = 0; col < width; col ++ ) |
| 244 | + { |
| 245 | + bool cellOk = true; |
| 246 | + double wcsVal = wcsProvider->readValue( wcsData, wcsProvider->dataType( band ), row * width + col ); |
| 247 | + double gdalVal = gdalProvider->readValue( gdalData, gdalProvider->dataType( band ), row * width + col ); |
| 248 | + |
| 249 | + QString valStr; |
| 250 | + if ( wcsVal == gdalVal ) |
| 251 | + { |
| 252 | + valStr = QString( "%1" ).arg( wcsVal ); |
| 253 | + } |
| 254 | + else |
| 255 | + { |
| 256 | + cellOk = false; |
| 257 | + allOk = false; |
| 258 | + valStr = QString( "%1<br>%2" ).arg( wcsVal ).arg( gdalVal ); |
| 259 | + } |
| 260 | + htmlTable += QString( "<td class='cell %1'>%2</td>" ).arg( cellOk ? "ok" : "err" ).arg( valStr ); |
| 261 | + } |
| 262 | + htmlTable += "</tr>"; |
| 263 | + } |
| 264 | + htmlTable += "</table>"; |
| 265 | + |
| 266 | + theReport += htmlTable; |
| 267 | + |
| 268 | + free( gdalData ); |
| 269 | + free( wcsData ); |
| 270 | + } |
| 271 | + delete wcsProvider; |
| 272 | + delete gdalProvider; |
| 273 | + return allOk; |
| 274 | +} |
| 275 | + |
| 276 | +void TestQgsWcsProvider::error( QString theMessage, QString &theReport ) |
| 277 | +{ |
| 278 | + theReport += "<font class='errmsg'>Error: "; |
| 279 | + theReport += theMessage; |
| 280 | + theReport += "</font>"; |
| 281 | +} |
| 282 | + |
| 283 | +double TestQgsWcsProvider::tolerance( double val, int places ) |
| 284 | +{ |
| 285 | + // float precission is about 7 decimal digits, double about 16 |
| 286 | + // default places = 6 |
| 287 | + return 1. * pow( 10, round( log10( qAbs( val ) ) - places ) ); |
| 288 | +} |
| 289 | + |
| 290 | +QString TestQgsWcsProvider::compareHead() |
| 291 | +{ |
| 292 | + return "<tr><th class='cell'>Param name</th><th class='cell'>WCS (tested) value</th><th class='cell'>GDAL (expected) value</th><th class='cell'>Difference</th><th class='cell'>Tolerance</th></tr>"; |
| 293 | +} |
| 294 | + |
| 295 | +void TestQgsWcsProvider::compare( QString theParamName, int wcsVal, int gdalVal, QString &theReport, bool &theOk ) |
| 296 | +{ |
| 297 | + bool ok = wcsVal == gdalVal; |
| 298 | + compareRow( theParamName, QString::number( wcsVal ), QString::number( gdalVal ), theReport, ok, QString::number( wcsVal - gdalVal ) ); |
| 299 | + if ( !ok ) theOk = false; |
| 300 | +} |
| 301 | + |
| 302 | +void TestQgsWcsProvider::compare( QString theParamName, double wcsVal, double gdalVal, QString &theReport, bool &theOk, double theTolerance ) |
| 303 | +{ |
| 304 | + bool ok = ( qAbs( wcsVal - gdalVal ) <= theTolerance ); |
| 305 | + |
| 306 | + compareRow( theParamName, QString::number( wcsVal ), QString::number( gdalVal ), theReport, ok, QString::number( wcsVal - gdalVal ), QString::number( theTolerance ) ); |
| 307 | + if ( !ok ) theOk = false; |
| 308 | +} |
| 309 | + |
| 310 | +void TestQgsWcsProvider::compareRow( QString theParamName, QString wcsVal, QString gdalVal, QString &theReport, bool theOk, QString theDifference, QString theTolerance ) |
| 311 | +{ |
| 312 | + theReport += "<tr>"; |
| 313 | + theReport += QString( "<td class='cell'>%1</td><td class='cell %2'>%3</td><td class='cell'>%4</td>" ).arg( theParamName ).arg( theOk ? "ok" : "err" ).arg( wcsVal ).arg( gdalVal ); |
| 314 | + theReport += QString( "<td class='cell'>%1</td>" ).arg( theDifference ); |
| 315 | + theReport += QString( "<td class='cell'>%1</td>" ).arg( theTolerance ); |
| 316 | + theReport += "</tr>"; |
| 317 | +} |
| 318 | + |
| 319 | +QTEST_MAIN( TestQgsWcsProvider ) |
| 320 | +#include "moc_testqgswcsprovider.cxx" |
0 commit comments