|
| 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" |
0 commit comments