PageRenderTime 76ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/core/testqgsrasterlayer.cpp

https://github.com/sourcepole/qgis
C++ | 266 lines | 184 code | 26 blank | 56 comment | 7 complexity | 03b6a5c4b547b83a3b2493ffbf109d25 MD5 | raw file
Possible License(s): GPL-2.0
  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. //qgis includes...
  29. #include <qgsrasterlayer.h>
  30. #include <qgsrasterpyramid.h>
  31. #include <qgsrasterbandstats.h>
  32. #include <qgsmaplayerregistry.h>
  33. #include <qgsapplication.h>
  34. #include <qgsmaprenderer.h>
  35. #include <qgsmaplayerregistry.h>
  36. //qgis unit test includes
  37. #include <qgsrenderchecker.h>
  38. /** \ingroup UnitTests
  39. * This is a unit test for the QgsRasterLayer class.
  40. */
  41. class TestQgsRasterLayer: public QObject
  42. {
  43. Q_OBJECT;
  44. private slots:
  45. void initTestCase();// will be called before the first testfunction is executed.
  46. void cleanupTestCase();// will be called after the last testfunction was executed.
  47. void init() {};// will be called before each testfunction is executed.
  48. void cleanup() {};// will be called after every testfunction.
  49. void isValid();
  50. void pseudoColor();
  51. void landsatBasic();
  52. void landsatBasic875Qml();
  53. void checkDimensions();
  54. void buildExternalOverviews();
  55. void registry();
  56. private:
  57. bool render( QString theFileName );
  58. bool setQml( QString theType );
  59. QString mTestDataDir;
  60. QgsRasterLayer * mpRasterLayer;
  61. QgsRasterLayer * mpLandsatRasterLayer;
  62. QgsMapRenderer * mpMapRenderer;
  63. QString mReport;
  64. };
  65. //runs before all tests
  66. void TestQgsRasterLayer::initTestCase()
  67. {
  68. // init QGIS's paths - true means that all path will be inited from prefix
  69. QString qgisPath = QCoreApplication::applicationDirPath();
  70. QgsApplication::setPrefixPath( INSTALL_PREFIX, true );
  71. QgsApplication::showSettings();
  72. //create some objects that will be used in all tests...
  73. //create a raster layer that will be used in all tests...
  74. mTestDataDir = QString( TEST_DATA_DIR ) + QDir::separator(); //defined in CmakeLists.txt
  75. QString myFileName = mTestDataDir + "tenbytenraster.asc";
  76. QString myLandsatFileName = mTestDataDir + "landsat.tif";
  77. QFileInfo myRasterFileInfo( myFileName );
  78. mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
  79. myRasterFileInfo.completeBaseName() );
  80. QFileInfo myLandsatRasterFileInfo( myLandsatFileName );
  81. mpLandsatRasterLayer = new QgsRasterLayer( myLandsatRasterFileInfo.filePath(),
  82. myLandsatRasterFileInfo.completeBaseName() );
  83. // Register the layer with the registry
  84. QgsMapLayerRegistry::instance()->addMapLayer( mpRasterLayer );
  85. QgsMapLayerRegistry::instance()->addMapLayer( mpLandsatRasterLayer );
  86. // add the test layer to the maprender
  87. mpMapRenderer = new QgsMapRenderer();
  88. QStringList myLayers;
  89. myLayers << mpRasterLayer->getLayerID();
  90. mpMapRenderer->setLayerSet( myLayers );
  91. mReport += "<h1>Raster Layer Tests</h1>\n";
  92. }
  93. //runs after all tests
  94. void TestQgsRasterLayer::cleanupTestCase()
  95. {
  96. QString myReportFile = QDir::tempPath() + QDir::separator() + "rastertest.html";
  97. QFile myFile( myReportFile );
  98. if ( myFile.open( QIODevice::WriteOnly ) )
  99. {
  100. QTextStream myQTextStream( &myFile );
  101. myQTextStream << mReport;
  102. myFile.close();
  103. QDesktopServices::openUrl( "file://" + myReportFile );
  104. }
  105. }
  106. void TestQgsRasterLayer::isValid()
  107. {
  108. QVERIFY( mpRasterLayer->isValid() );
  109. mpMapRenderer->setExtent( mpRasterLayer->extent() );
  110. QVERIFY( render( "raster" ) );
  111. }
  112. void TestQgsRasterLayer::pseudoColor()
  113. {
  114. mpRasterLayer->setDrawingStyle( QgsRasterLayer::SingleBandPseudoColor );
  115. mpRasterLayer->setColorShadingAlgorithm( QgsRasterLayer::PseudoColorShader );
  116. mpRasterLayer->setContrastEnhancementAlgorithm(
  117. QgsContrastEnhancement::StretchToMinimumMaximum, false );
  118. mpRasterLayer->setMinimumValue( mpRasterLayer->grayBandName(), 0.0, false );
  119. mpRasterLayer->setMaximumValue( mpRasterLayer->grayBandName(), 10.0 );
  120. mpMapRenderer->setExtent( mpRasterLayer->extent() );
  121. QVERIFY( render( "raster_pseudo" ) );
  122. }
  123. void TestQgsRasterLayer::landsatBasic()
  124. {
  125. QStringList myLayers;
  126. myLayers << mpLandsatRasterLayer->getLayerID();
  127. mpMapRenderer->setLayerSet( myLayers );
  128. mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
  129. QVERIFY( render( "landsat_basic" ) );
  130. }
  131. void TestQgsRasterLayer::landsatBasic875Qml()
  132. {
  133. //a qml that orders the rgb bands as 8,7,5
  134. QStringList myLayers;
  135. myLayers << mpLandsatRasterLayer->getLayerID();
  136. mpMapRenderer->setLayerSet( myLayers );
  137. mpMapRenderer->setExtent( mpLandsatRasterLayer->extent() );
  138. QVERIFY( setQml( "875" ) );
  139. QVERIFY( render( "landsat_875" ) );
  140. }
  141. void TestQgsRasterLayer::checkDimensions()
  142. {
  143. QVERIFY( mpRasterLayer->width() == 10 );
  144. QVERIFY( mpRasterLayer->height() == 10 );
  145. // regression check for ticket #832
  146. // note bandStatistics call is base 1
  147. QVERIFY( mpRasterLayer->bandStatistics( 1 ).elementCount == 100 );
  148. }
  149. void TestQgsRasterLayer::buildExternalOverviews()
  150. {
  151. //before we begin delete any old ovr file (if it exists)
  152. //and make a copy of the landsat raster into the temp dir
  153. QString myTempPath = QDir::tempPath() + QDir::separator();
  154. QFile::remove( myTempPath + "landsat.tif.ovr" );
  155. QFile::copy( mTestDataDir + "landsat.tif", myTempPath + "landsat.tif" );
  156. QFileInfo myRasterFileInfo( myTempPath + "landsat.tif" );
  157. QgsRasterLayer * mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
  158. myRasterFileInfo.completeBaseName() );
  159. //
  160. // Ok now we can go on to test
  161. //
  162. bool myInternalFlag = false;
  163. QgsRasterLayer::RasterPyramidList myPyramidList = mypLayer->buildPyramidList();
  164. for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
  165. {
  166. //mark to be pyramided
  167. myPyramidList[myCounterInt].build = true;
  168. }
  169. //now actually make the pyramids
  170. QString myResult = mypLayer->buildPyramids(
  171. myPyramidList,
  172. "NEAREST",
  173. myInternalFlag
  174. );
  175. qDebug( "%s", myResult.toLocal8Bit().constData() );
  176. //
  177. // Lets verify we have pyramids now...
  178. //
  179. myPyramidList = mypLayer->buildPyramidList();
  180. for ( int myCounterInt = 0; myCounterInt < myPyramidList.count(); myCounterInt++ )
  181. {
  182. //mark to be pyramided
  183. QVERIFY( myPyramidList.at( myCounterInt ).exists );
  184. }
  185. //
  186. // And that they were indeed in an external file...
  187. //
  188. QVERIFY( QFile::exists( myTempPath + "landsat.tif.ovr" ) );
  189. //cleanup
  190. delete mypLayer;
  191. }
  192. void TestQgsRasterLayer::registry()
  193. {
  194. QString myTempPath = QDir::tempPath() + QDir::separator();
  195. QFile::remove( myTempPath + "landsat.tif.ovr" );
  196. QFile::copy( mTestDataDir + "landsat.tif", myTempPath + "landsat.tif" );
  197. QFileInfo myRasterFileInfo( myTempPath + "landsat.tif" );
  198. QgsRasterLayer * mypLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
  199. myRasterFileInfo.completeBaseName() );
  200. QgsMapLayerRegistry::instance()->addMapLayer( mypLayer, false );
  201. QgsMapLayerRegistry::instance()->removeMapLayer( mypLayer->getLayerID() );
  202. //cleanup
  203. //delete mypLayer;
  204. }
  205. //
  206. // Helper methods
  207. //
  208. bool TestQgsRasterLayer::render( QString theTestType )
  209. {
  210. mReport += "<h2>" + theTestType + "</h2>\n";
  211. QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
  212. QString myTestDataDir = myDataDir + QDir::separator();
  213. QgsRenderChecker myChecker;
  214. myChecker.setExpectedImage( myTestDataDir + "expected_" + theTestType + ".png" );
  215. myChecker.setMapRenderer( mpMapRenderer );
  216. bool myResultFlag = myChecker.runTest( theTestType );
  217. mReport += "\n\n\n" + myChecker.report();
  218. return myResultFlag;
  219. }
  220. bool TestQgsRasterLayer::setQml( QString theType )
  221. {
  222. //load a qml style and apply to our layer
  223. // huh? this is failing but shouldnt!
  224. //if (! mpLandsatRasterLayer->isValid() )
  225. //{
  226. // qDebug(" **** setQml -> mpLandsatRasterLayer is invalid");
  227. // return false;
  228. //}
  229. QString myFileName = mTestDataDir + "landsat_" + theType + ".qml";
  230. bool myStyleFlag = false;
  231. mpLandsatRasterLayer->loadNamedStyle( myFileName, myStyleFlag );
  232. if ( !myStyleFlag )
  233. {
  234. qDebug( " **** setQml -> mpLandsatRasterLayer is invalid" );
  235. qDebug( "Qml File :%s", myFileName.toLocal8Bit().constData() );
  236. }
  237. return myStyleFlag;
  238. }
  239. QTEST_MAIN( TestQgsRasterLayer )
  240. #include "moc_testqgsrasterlayer.cxx"