|
| 1 | +/*************************************************************************** |
| 2 | + testqgsgdalprovider.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : March 2015 |
| 5 | + Copyright : (C) 2015 by Nyall Dawson |
| 6 | + Email : nyall.dawson@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/QtTest> |
| 16 | +#include <QObject> |
| 17 | +#include <QString> |
| 18 | +#include <QStringList> |
| 19 | +#include <QObject> |
| 20 | +#include <QApplication> |
| 21 | +#include <QFileInfo> |
| 22 | +#include <QDir> |
| 23 | + |
| 24 | +//qgis includes... |
| 25 | +#include <qgis.h> |
| 26 | +#include <qgsapplication.h> |
| 27 | +#include <qgsproviderregistry.h> |
| 28 | +#include <qgsrasterdataprovider.h> |
| 29 | +#include <qgsrectangle.h> |
| 30 | + |
| 31 | +/** \ingroup UnitTests |
| 32 | + * This is a unit test for the gdal provider |
| 33 | + */ |
| 34 | +class TestQgsGdalProvider : public QObject |
| 35 | +{ |
| 36 | + Q_OBJECT |
| 37 | + |
| 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 scaleDataType(); //test resultant data types for int raster with float scale (#11573) |
| 45 | + void warpedVrt(); //test loading raster which requires a warped vrt |
| 46 | + |
| 47 | + private: |
| 48 | + QString mTestDataDir; |
| 49 | + QString mReport; |
| 50 | +}; |
| 51 | + |
| 52 | +//runs before all tests |
| 53 | +void TestQgsGdalProvider::initTestCase() |
| 54 | +{ |
| 55 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 56 | + QgsApplication::init(); |
| 57 | + QgsApplication::initQgis(); |
| 58 | + |
| 59 | + mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt |
| 60 | + mReport = "<h1>GDAL Provider Tests</h1>\n"; |
| 61 | +} |
| 62 | + |
| 63 | +//runs after all tests |
| 64 | +void TestQgsGdalProvider::cleanupTestCase() |
| 65 | +{ |
| 66 | + QgsApplication::exitQgis(); |
| 67 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; |
| 68 | + QFile myFile( myReportFile ); |
| 69 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 70 | + { |
| 71 | + QTextStream myQTextStream( &myFile ); |
| 72 | + myQTextStream << mReport; |
| 73 | + myFile.close(); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void TestQgsGdalProvider::scaleDataType() |
| 78 | +{ |
| 79 | + QString rasterWithOffset = QString( TEST_DATA_DIR ) + QDir::separator() + "int_raster_with_scale.tif"; |
| 80 | + QgsDataProvider* provider = QgsProviderRegistry::instance()->provider( "gdal", rasterWithOffset ); |
| 81 | + QgsRasterDataProvider* rp = dynamic_cast< QgsRasterDataProvider* >( provider ); |
| 82 | + QVERIFY( rp ); |
| 83 | + //raster is an integer data type, but has a scale < 1, so data type must be float |
| 84 | + QCOMPARE( rp->dataType( 1 ), QGis::Float32 ); |
| 85 | + QCOMPARE( rp->srcDataType( 1 ), QGis::Float32 ); |
| 86 | +} |
| 87 | + |
| 88 | +void TestQgsGdalProvider::warpedVrt() |
| 89 | +{ |
| 90 | + QString raster = QString( TEST_DATA_DIR ) + QDir::separator() + "requires_warped_vrt.tif"; |
| 91 | + QgsDataProvider* provider = QgsProviderRegistry::instance()->provider( "gdal", raster ); |
| 92 | + QgsRasterDataProvider* rp = dynamic_cast< QgsRasterDataProvider* >( provider ); |
| 93 | + QVERIFY( rp ); |
| 94 | + |
| 95 | + qDebug() << "x min: " << rp->extent().xMinimum(); |
| 96 | + qDebug() << "x max: " << rp->extent().xMaximum(); |
| 97 | + qDebug() << "y min: " << rp->extent().yMinimum(); |
| 98 | + qDebug() << "y max: " << rp->extent().yMaximum(); |
| 99 | + |
| 100 | + QVERIFY( qgsDoubleNear( rp->extent().xMinimum(), 2058589, 1 ) ); |
| 101 | + QVERIFY( qgsDoubleNear( rp->extent().xMaximum(), 3118999, 1 ) ); |
| 102 | + QVERIFY( qgsDoubleNear( rp->extent().yMinimum(), 2281355, 1 ) ); |
| 103 | + QVERIFY( qgsDoubleNear( rp->extent().yMaximum(), 3129683, 1 ) ); |
| 104 | +} |
| 105 | + |
| 106 | +QTEST_MAIN( TestQgsGdalProvider ) |
| 107 | +#include "testqgsgdalprovider.moc" |
0 commit comments