@@ -14,6 +14,10 @@ Email : nyall dot dawson at gmail dot com
14
14
***************************************************************************/
15
15
#include " qgstest.h"
16
16
17
+ #ifdef HAVE_OPENCL
18
+ #include " qgsopenclutils.h"
19
+ #endif
20
+
17
21
#include " qgsrastercalculator.h"
18
22
#include " qgsrastercalcnode.h"
19
23
#include " qgsrasterdataprovider.h"
@@ -59,22 +63,29 @@ class TestQgsRasterCalculator : public QObject
59
63
void findNodes ();
60
64
61
65
void testRasterEntries ();
66
+ void calcFormulasWithReprojectedLayers ();
62
67
63
68
private:
64
69
65
70
QgsRasterLayer *mpLandsatRasterLayer = nullptr ;
66
71
QgsRasterLayer *mpLandsatRasterLayer4326 = nullptr ;
67
72
};
68
73
74
+
69
75
void TestQgsRasterCalculator::initTestCase ()
70
76
{
71
77
//
72
78
// Runs once before any tests are run
73
79
//
74
- // init QGIS's paths - true means that all path will be inited from prefix
80
+ // Set up the QgsSettings environment
81
+ QCoreApplication::setOrganizationName ( QStringLiteral ( " QGIS" ) );
82
+ QCoreApplication::setOrganizationDomain ( QStringLiteral ( " qgis.org" ) );
83
+ QCoreApplication::setApplicationName ( QStringLiteral ( " QGIS-TEST" ) );
84
+
75
85
QgsApplication::init ();
76
86
QgsApplication::initQgis ();
77
87
88
+
78
89
QString testDataDir = QStringLiteral ( TEST_DATA_DIR ) + ' /' ; // defined in CmakeLists.txt
79
90
80
91
QString landsatFileName = testDataDir + " landsat.tif" ;
@@ -99,8 +110,13 @@ void TestQgsRasterCalculator::cleanupTestCase()
99
110
100
111
void TestQgsRasterCalculator::init ()
101
112
{
102
-
113
+ #ifdef HAVE_OPENCL
114
+ QgsOpenClUtils::setEnabled ( false );
115
+ // Reset to default in case some tests mess it up
116
+ QgsOpenClUtils::setSourcePath ( QDir ( QgsApplication::pkgDataPath () ).absoluteFilePath ( QStringLiteral ( " resources/opencl_programs" ) ) );
117
+ #endif
103
118
}
119
+
104
120
void TestQgsRasterCalculator::cleanup ()
105
121
{
106
122
@@ -676,12 +692,78 @@ void TestQgsRasterCalculator::toString()
676
692
return error;
677
693
return calcNode->toString ( cStyle );
678
694
};
679
- QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" + 2" ), false ), QString ( " \" raster@1\" + 2" ) );
680
- QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" + 2" ), true ), QString ( " \" raster@1\" + 2" ) );
681
- QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" ^ 3 + 2" ), false ), QString ( " \" raster@1\" ^3 + 2" ) );
682
- QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" ^ 3 + 2" ), true ), QString ( " pow( (float) ( \" raster@1\" ), (float) ( 3 ) ) + 2" ) );
683
- QCOMPARE ( _test ( QStringLiteral ( " atan(\" raster@1\" ) * cos( 3 + 2 )" ), false ), QString ( " atan( \" raster@1\" ) * cos( 3 + 2 )" ) );
684
- QCOMPARE ( _test ( QStringLiteral ( " atan(\" raster@1\" ) * cos( 3 + 2 )" ), true ), QString ( " atan( (float) ( \" raster@1\" ) ) * cos( (float) ( 3 + 2 ) )" ) );
695
+ QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" + 2" ), false ), QString ( " ( \" raster@1\" + 2 )" ) );
696
+ QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" + 2" ), true ), QString ( " ( \" raster@1\" + (float) ( 2 ) )" ) );
697
+ QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" ^ 3 + 2" ), false ), QString ( " ( \" raster@1\" ^3 + 2 )" ) );
698
+ QCOMPARE ( _test ( QStringLiteral ( " \" raster@1\" ^ 3 + 2" ), true ), QString ( " ( pow( \" raster@1\" , (float) ( 3 ) ) + (float) ( 2 ) )" ) );
699
+ QCOMPARE ( _test ( QStringLiteral ( " atan(\" raster@1\" ) * cos( 3 + 2 )" ), false ), QString ( " atan( \" raster@1\" ) * cos( ( 3 + 2 ) )" ) );
700
+ QCOMPARE ( _test ( QStringLiteral ( " atan(\" raster@1\" ) * cos( 3 + 2 )" ), true ), QString ( " atan( \" raster@1\" ) * cos( ( (float) ( 3 ) + (float) ( 2 ) ) )" ) );
701
+ QCOMPARE ( _test ( QStringLiteral ( " 0.5 * ( 1.4 * (\" raster@1\" + 2) )" ), false ), QString ( " 0.5 * 1.4 * ( \" raster@1\" + 2 )" ) );
702
+ QCOMPARE ( _test ( QStringLiteral ( " 0.5 * ( 1.4 * (\" raster@1\" + 2) )" ), true ), QString ( " (float) ( 0.5 ) * (float) ( 1.4 ) * ( \" raster@1\" + (float) ( 2 ) )" ) );
703
+ }
704
+
705
+ void TestQgsRasterCalculator::calcFormulasWithReprojectedLayers ()
706
+ {
707
+ QgsRasterCalculatorEntry entry1;
708
+ entry1.bandNumber = 1 ;
709
+ entry1.raster = mpLandsatRasterLayer;
710
+ entry1.ref = QStringLiteral ( " landsat@1" );
711
+
712
+ QgsRasterCalculatorEntry entry2;
713
+ entry2.bandNumber = 2 ;
714
+ entry2.raster = mpLandsatRasterLayer4326;
715
+ entry2.ref = QStringLiteral ( " landsat_4326@2" );
716
+
717
+ QVector<QgsRasterCalculatorEntry> entries;
718
+ entries << entry1 << entry2;
719
+
720
+ QgsCoordinateReferenceSystem crs;
721
+ crs.createFromId ( 32633 , QgsCoordinateReferenceSystem::EpsgCrsId );
722
+ QgsRectangle extent ( 783235 , 3348110 , 783350 , 3347960 );
723
+
724
+
725
+ auto _chk = [ = ]( const QString & formula, const std::vector<float > &values, bool useOpenCL )
726
+ {
727
+
728
+ #ifdef HAVE_OPENCL
729
+ if ( ! QgsOpenClUtils::available () )
730
+ return ;
731
+ QgsOpenClUtils::setEnabled ( useOpenCL );
732
+ #endif
733
+
734
+ QTemporaryFile tmpFile;
735
+ tmpFile.open (); // fileName is not available until open
736
+ QString tmpName = tmpFile.fileName ();
737
+ tmpFile.close ();
738
+ QgsRasterCalculator rc ( formula,
739
+ tmpName,
740
+ QStringLiteral ( " GTiff" ),
741
+ extent, crs, 2 , 3 , entries );
742
+ QCOMPARE ( static_cast < int >( rc.processCalculation () ), 0 );
743
+ // open output file and check results
744
+ QgsRasterLayer *result = new QgsRasterLayer ( tmpName, QStringLiteral ( " result" ) );
745
+ QCOMPARE ( result->width (), 2 );
746
+ QCOMPARE ( result->height (), 3 );
747
+ QgsRasterBlock *block = result->dataProvider ()->block ( 1 , extent, 2 , 3 );
748
+ qDebug () << block->value ( 0 , 0 ) << block->value ( 0 , 1 ) << block->value ( 1 , 0 ) << block->value ( 1 , 1 ) << block->value ( 2 , 0 ) << block->value ( 2 , 1 );
749
+ const float epsilon { 0 .0001f };
750
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 0 , 0 ) ) - values[0 ] ) / values[0 ] ) < epsilon );
751
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 0 , 1 ) ) - values[1 ] ) / values[1 ] ) < epsilon );
752
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 1 , 0 ) ) - values[2 ] ) / values[2 ] ) < epsilon );
753
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 1 , 1 ) ) - values[3 ] ) / values[3 ] ) < epsilon );
754
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 2 , 0 ) ) - values[4 ] ) / values[4 ] ) < epsilon );
755
+ QVERIFY ( std::abs ( ( static_cast <float >( block->value ( 2 , 1 ) ) - values[5 ] ) / values[5 ] ) < epsilon );
756
+ delete result;
757
+ delete block;
758
+ };
759
+
760
+ _chk ( QStringLiteral ( " \" landsat@1\" + \" landsat_4326@2\" " ), {264.0 , 263.0 , 264.0 , 264.0 , 266.0 , 261.0 }, false );
761
+ _chk ( QStringLiteral ( " \" landsat@1\" + \" landsat_4326@2\" " ), {264.0 , 263.0 , 264.0 , 264.0 , 266.0 , 261.0 }, true );
762
+ _chk ( QStringLiteral ( " \" landsat@1\" ^2 + 3 + \" landsat_4326@2\" " ), {15767 , 15766 , 15519 , 15767 , 15769 , 15516 }, false );
763
+ _chk ( QStringLiteral ( " \" landsat@1\" ^2 + 3 + \" landsat_4326@2\" " ), {15767 , 15766 , 15519 , 15767 , 15769 , 15516 }, true );
764
+ _chk ( QStringLiteral ( " 0.5*((2*\" landsat@1\" +1)-sqrt((2*\" landsat@1\" +1)^2-8*(\" landsat@1\" -\" landsat_4326@2\" )))" ), {-0.111504 , -0.103543 , -0.128448 , -0.111504 , -0.127425 , -0.104374 }, false );
765
+ _chk ( QStringLiteral ( " 0.5*((2*\" landsat@1\" +1)-sqrt((2*\" landsat@1\" +1)^2-8*(\" landsat@1\" -\" landsat_4326@2\" )))" ), {-0.111504 , -0.103543 , -0.128448 , -0.111504 , -0.127425 , -0.104374 }, true );
766
+
685
767
}
686
768
687
769
0 commit comments