/
testqgsrastercalculator.cpp
689 lines (576 loc) · 25.7 KB
/
testqgsrastercalculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/***************************************************************************
testqgsrastercalculator.cpp
--------------------------------------
Date : Jun-2015
Copyright : (C) 2015 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include "qgsrastercalculator.h"
#include "qgsrastercalcnode.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterlayer.h"
#include "qgsrastermatrix.h"
#include "qgsapplication.h"
#include "qgsproject.h"
Q_DECLARE_METATYPE( QgsRasterCalcNode::Operator )
class TestQgsRasterCalculator : public QObject
{
Q_OBJECT
public:
TestQgsRasterCalculator() = default;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() ;// will be called before each testfunction is executed.
void cleanup() ;// will be called after every testfunction.
void dualOp_data();
void dualOp(); //test operators which operate on a left&right node
void singleOp_data();
void singleOp(); //test operators which operate on a single value
void singleOpMatrices(); // test single op using matrix
void dualOpNumberMatrix(); // test dual op run on number and matrix
void dualOpMatrixNumber(); // test dual op run on matrix and number
void dualOpMatrixMatrix(); // test dual op run on matrix and matrix
void rasterRefOp();
void dualOpRasterRaster(); //test dual op on raster ref and raster ref
void calcWithLayers();
void calcWithReprojectedLayers();
void errors();
void toString();
void findNodes();
void testRasterEntries();
private:
QgsRasterLayer *mpLandsatRasterLayer = nullptr;
QgsRasterLayer *mpLandsatRasterLayer4326 = nullptr;
};
void TestQgsRasterCalculator::initTestCase()
{
//
// Runs once before any tests are run
//
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
QString landsatFileName = testDataDir + "landsat.tif";
QFileInfo landsatRasterFileInfo( landsatFileName );
mpLandsatRasterLayer = new QgsRasterLayer( landsatRasterFileInfo.filePath(),
landsatRasterFileInfo.completeBaseName() );
QString landsat4326FileName = testDataDir + "landsat_4326.tif";
QFileInfo landsat4326RasterFileInfo( landsat4326FileName );
mpLandsatRasterLayer4326 = new QgsRasterLayer( landsat4326RasterFileInfo.filePath(),
landsat4326RasterFileInfo.completeBaseName() );
QgsProject::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpLandsatRasterLayer << mpLandsatRasterLayer4326 );
}
void TestQgsRasterCalculator::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsRasterCalculator::init()
{
}
void TestQgsRasterCalculator::cleanup()
{
}
void TestQgsRasterCalculator::dualOp_data()
{
QTest::addColumn< QgsRasterCalcNode::Operator >( "op" );
QTest::addColumn<double>( "left" );
QTest::addColumn<double>( "right" );
QTest::addColumn<double>( "expected" );
QTest::newRow( "opPlus" ) << QgsRasterCalcNode::opPLUS << 5.5 << 2.2 << 7.7;
QTest::newRow( "opMINUS" ) << QgsRasterCalcNode::opMINUS << 5.0 << 2.5 << 2.5;
QTest::newRow( "opMUL" ) << QgsRasterCalcNode::opMUL << 2.5 << 4.0 << 10.0;
QTest::newRow( "opDIV" ) << QgsRasterCalcNode::opDIV << 2.5 << 2.0 << 1.25;
QTest::newRow( "opDIV by 0" ) << QgsRasterCalcNode::opDIV << 2.5 << 0.0 << -9999.0;
QTest::newRow( "opPOW" ) << QgsRasterCalcNode::opPOW << 3.0 << 2.0 << 9.0;
QTest::newRow( "opPOW negative" ) << QgsRasterCalcNode::opPOW << 4.0 << -2.0 << 0.0625;
QTest::newRow( "opPOW sqrt" ) << QgsRasterCalcNode::opPOW << 4.0 << 0.5 << 2.0;
QTest::newRow( "opPOW complex" ) << QgsRasterCalcNode::opPOW << -2.0 << 0.5 << -9999.0;
QTest::newRow( "opEQ true" ) << QgsRasterCalcNode::opEQ << 1.0 << 1.0 << 1.0;
QTest::newRow( "opEQ false" ) << QgsRasterCalcNode::opEQ << 0.5 << 1.0 << 0.0;
QTest::newRow( "opNE equal" ) << QgsRasterCalcNode::opNE << 1.0 << 1.0 << 0.0;
QTest::newRow( "opNE not equal" ) << QgsRasterCalcNode::opNE << 0.5 << 1.0 << 1.0;
QTest::newRow( "opGT >" ) << QgsRasterCalcNode::opGT << 1.0 << 0.5 << 1.0;
QTest::newRow( "opGT =" ) << QgsRasterCalcNode::opGT << 0.5 << 0.5 << 0.0;
QTest::newRow( "opGT <" ) << QgsRasterCalcNode::opGT << 0.5 << 1.0 << 0.0;
QTest::newRow( "opLT >" ) << QgsRasterCalcNode::opLT << 1.0 << 0.5 << 0.0;
QTest::newRow( "opLT =" ) << QgsRasterCalcNode::opLT << 0.5 << 0.5 << 0.0;
QTest::newRow( "opLT <" ) << QgsRasterCalcNode::opLT << 0.5 << 1.0 << 1.0;
QTest::newRow( "opGE >" ) << QgsRasterCalcNode::opGE << 1.0 << 0.5 << 1.0;
QTest::newRow( "opGE =" ) << QgsRasterCalcNode::opGE << 0.5 << 0.5 << 1.0;
QTest::newRow( "opGE <" ) << QgsRasterCalcNode::opGE << 0.5 << 1.0 << 0.0;
QTest::newRow( "opLE >" ) << QgsRasterCalcNode::opLE << 1.0 << 0.5 << 0.0;
QTest::newRow( "opLE =" ) << QgsRasterCalcNode::opLE << 0.5 << 0.5 << 1.0;
QTest::newRow( "opLE <" ) << QgsRasterCalcNode::opLE << 0.5 << 1.0 << 1.0;
QTest::newRow( "opAND 0/0" ) << QgsRasterCalcNode::opAND << 0.0 << 0.0 << 0.0;
QTest::newRow( "opAND 0/1" ) << QgsRasterCalcNode::opAND << 0.0 << 1.0 << 0.0;
QTest::newRow( "opAND 1/0" ) << QgsRasterCalcNode::opAND << 1.0 << 0.0 << 0.0;
QTest::newRow( "opAND 1/1" ) << QgsRasterCalcNode::opAND << 1.0 << 1.0 << 1.0;
QTest::newRow( "opOR 0/0" ) << QgsRasterCalcNode::opOR << 0.0 << 0.0 << 0.0;
QTest::newRow( "opOR 0/1" ) << QgsRasterCalcNode::opOR << 0.0 << 1.0 << 1.0;
QTest::newRow( "opOR 1/0" ) << QgsRasterCalcNode::opOR << 1.0 << 0.0 << 1.0;
QTest::newRow( "opOR 1/1" ) << QgsRasterCalcNode::opOR << 1.0 << 1.0 << 1.0;
}
void TestQgsRasterCalculator::dualOp()
{
QFETCH( QgsRasterCalcNode::Operator, op );
QFETCH( double, left );
QFETCH( double, right );
QFETCH( double, expected );
QgsRasterCalcNode node( op, new QgsRasterCalcNode( left ), new QgsRasterCalcNode( right ) );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
qDebug() << "Result: " << result.number() << " expected: " << expected;
QCOMPARE( result.number(), expected );
}
void TestQgsRasterCalculator::singleOp_data()
{
QTest::addColumn< QgsRasterCalcNode::Operator >( "op" );
QTest::addColumn<double>( "value" );
QTest::addColumn<double>( "expected" );
QTest::newRow( "opSQRT" ) << QgsRasterCalcNode::opSQRT << 16.0 << 4.0;
QTest::newRow( "opSQRT negative" ) << QgsRasterCalcNode::opSQRT << -16.0 << -9999.0;
QTest::newRow( "opSIN 0" ) << QgsRasterCalcNode::opSIN << 0.0 << 0.0;
QTest::newRow( "opSIN pi/2" ) << QgsRasterCalcNode::opSIN << M_PI / 2.0 << 1.0;
QTest::newRow( "opCOS 0" ) << QgsRasterCalcNode::opCOS << 0.0 << 1.0;
QTest::newRow( "opCOS pi" ) << QgsRasterCalcNode::opCOS << M_PI << -1.0;
QTest::newRow( "opTAN 0" ) << QgsRasterCalcNode::opTAN << 0.0 << 0.0;
QTest::newRow( "opTAN pi" ) << QgsRasterCalcNode::opTAN << M_PI << 0.0;
QTest::newRow( "opASIN 0" ) << QgsRasterCalcNode::opASIN << 0.0 << 0.0;
QTest::newRow( "opASIN pi/2" ) << QgsRasterCalcNode::opASIN << 1.0 << M_PI / 2.0;
QTest::newRow( "opACOS 0" ) << QgsRasterCalcNode::opACOS << 1.0 << 0.0;
QTest::newRow( "opACOS pi/2" ) << QgsRasterCalcNode::opACOS << -1.0 << M_PI;
QTest::newRow( "opATAN 0" ) << QgsRasterCalcNode::opATAN << 0.0 << 0.0;
QTest::newRow( "opATAN 1.0" ) << QgsRasterCalcNode::opATAN << 1.0 << 0.7853981634;
QTest::newRow( "opSIGN +" ) << QgsRasterCalcNode::opSIGN << 1.0 << -1.0;
QTest::newRow( "opSIGN -" ) << QgsRasterCalcNode::opSIGN << -1.0 << 1.0;
QTest::newRow( "opLOG -1" ) << QgsRasterCalcNode::opLOG << -1.0 << -9999.0;
QTest::newRow( "opLOG 0" ) << QgsRasterCalcNode::opLOG << 0.0 << -9999.0;
QTest::newRow( "opLOG 1" ) << QgsRasterCalcNode::opLOG << 1.0 << 0.0;
QTest::newRow( "opLOG10 -1" ) << QgsRasterCalcNode::opLOG10 << -1.0 << -9999.0;
QTest::newRow( "opLOG10 0" ) << QgsRasterCalcNode::opLOG10 << 0.0 << -9999.0;
QTest::newRow( "opLOG10 1" ) << QgsRasterCalcNode::opLOG10 << 1.0 << 0.0;
QTest::newRow( "opLOG10 10" ) << QgsRasterCalcNode::opLOG10 << 10.0 << 1.0;
}
void TestQgsRasterCalculator::singleOp()
{
QFETCH( QgsRasterCalcNode::Operator, op );
QFETCH( double, value );
QFETCH( double, expected );
QgsRasterCalcNode node( op, new QgsRasterCalcNode( value ), nullptr );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
qDebug() << "Result: " << result.number() << " expected: " << expected;
QGSCOMPARENEAR( result.number(), expected, 0.0000000001 );
}
void TestQgsRasterCalculator::singleOpMatrices()
{
// test single op run on matrix
double *d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = 3.0;
d[3] = 4.0;
d[4] = 5.0;
d[5] = -1.0;
QgsRasterMatrix m( 2, 3, d, -1.0 );
QgsRasterCalcNode node( QgsRasterCalcNode::opSIGN, new QgsRasterCalcNode( &m ), nullptr );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], -d[0] );
QCOMPARE( result.data()[1], -d[1] );
QCOMPARE( result.data()[2], -d[2] );
QCOMPARE( result.data()[3], -d[3] );
QCOMPARE( result.data()[4], -d[4] );
QCOMPARE( result.data()[5], -9999.0 );
}
void TestQgsRasterCalculator::dualOpNumberMatrix()
{
// test dual op run on number and matrix
double *d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = 3.0;
d[3] = 4.0;
d[4] = 5.0;
d[5] = -1.0;
QgsRasterMatrix m( 2, 3, d, -1.0 );
QgsRasterCalcNode node( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( 5.0 ), new QgsRasterCalcNode( &m ) );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], 6.0 );
QCOMPARE( result.data()[1], 7.0 );
QCOMPARE( result.data()[2], 8.0 );
QCOMPARE( result.data()[3], 9.0 );
QCOMPARE( result.data()[4], 10.0 );
QCOMPARE( result.data()[5], -9999.0 );
//also check adding no data number
QgsRasterCalcNode nodeNoData( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( -9999 ), new QgsRasterCalcNode( &m ) );
QVERIFY( nodeNoData.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], -9999.0 );
}
void TestQgsRasterCalculator::dualOpMatrixNumber()
{
// test dual op run on matrix and number
double *d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = 3.0;
d[3] = 4.0;
d[4] = 5.0;
d[5] = -1.0;
QgsRasterMatrix m( 2, 3, d, -1.0 );
QgsRasterCalcNode node( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( &m ), new QgsRasterCalcNode( 5.0 ) );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], 6.0 );
QCOMPARE( result.data()[1], 7.0 );
QCOMPARE( result.data()[2], 8.0 );
QCOMPARE( result.data()[3], 9.0 );
QCOMPARE( result.data()[4], 10.0 );
QCOMPARE( result.data()[5], -9999.0 );
//also check adding no data number
QgsRasterCalcNode nodeNoData( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( &m ), new QgsRasterCalcNode( -9999 ) );
QVERIFY( nodeNoData.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], -9999.0 );
}
void TestQgsRasterCalculator::dualOpMatrixMatrix()
{
// test dual op run on matrix and matrix
double *d = new double[6];
d[0] = 1.0;
d[1] = 2.0;
d[2] = -2.0;
d[3] = -1.0; //nodata
d[4] = 5.0;
d[5] = -1.0; //nodata
QgsRasterMatrix m1( 2, 3, d, -1.0 );
double *d2 = new double[6];
d2[0] = -1.0;
d2[1] = -2.0; //nodata
d2[2] = 13.0;
d2[3] = -2.0; //nodata
d2[4] = 15.0;
d2[5] = -1.0;
QgsRasterMatrix m2( 2, 3, d2, -2.0 ); //different no data value
QgsRasterCalcNode node( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( &m1 ), new QgsRasterCalcNode( &m2 ) );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], 0.0 );
QCOMPARE( result.data()[1], -9999.0 );
QCOMPARE( result.data()[2], 11.0 );
QCOMPARE( result.data()[3], -9999.0 );
QCOMPARE( result.data()[4], 20.0 );
QCOMPARE( result.data()[5], -9999.0 );
}
void TestQgsRasterCalculator::rasterRefOp()
{
// test single op run on raster ref
QgsRasterCalcNode node( QgsRasterCalcNode::opSIGN, new QgsRasterCalcNode( QStringLiteral( "raster" ) ), nullptr );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QMap<QString, QgsRasterBlock *> rasterData;
//first test invalid raster ref
QVERIFY( !node.calculate( rasterData, result ) );
//now create raster ref
QgsRasterBlock m( Qgis::Float32, 2, 3 );
m.setNoDataValue( -1.0 );
m.setValue( 0, 0, 1.0 );
m.setValue( 0, 1, 2.0 );
m.setValue( 1, 0, 3.0 );
m.setValue( 1, 1, 4.0 );
m.setValue( 2, 0, 5.0 );
m.setValue( 2, 1, -1.0 );
rasterData.insert( QStringLiteral( "raster" ), &m );
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], -1.0 );
QCOMPARE( result.data()[1], -2.0 );
QCOMPARE( result.data()[2], -3.0 );
QCOMPARE( result.data()[3], -4.0 );
QCOMPARE( result.data()[4], -5.0 );
QCOMPARE( result.data()[5], -9999.0 );
}
void TestQgsRasterCalculator::dualOpRasterRaster()
{
// test dual op run on matrix and matrix
QgsRasterBlock m1( Qgis::Float32, 2, 3 );
m1.setNoDataValue( -1.0 );
m1.setValue( 0, 0, 1.0 );
m1.setValue( 0, 1, 2.0 );
m1.setValue( 1, 0, -2.0 );
m1.setValue( 1, 1, -1.0 ); //nodata
m1.setValue( 2, 0, 5.0 );
m1.setValue( 2, 1, -1.0 ); //nodata
QMap<QString, QgsRasterBlock *> rasterData;
rasterData.insert( QStringLiteral( "raster1" ), &m1 );
QgsRasterBlock m2( Qgis::Float32, 2, 3 );
m2.setNoDataValue( -2.0 ); //different no data value
m2.setValue( 0, 0, -1.0 );
m2.setValue( 0, 1, -2.0 ); //nodata
m2.setValue( 1, 0, 13.0 );
m2.setValue( 1, 1, -2.0 ); //nodata
m2.setValue( 2, 0, 15.0 );
m2.setValue( 2, 1, -1.0 );
rasterData.insert( QStringLiteral( "raster2" ), &m2 );
QgsRasterCalcNode node( QgsRasterCalcNode::opPLUS, new QgsRasterCalcNode( QStringLiteral( "raster1" ) ), new QgsRasterCalcNode( QStringLiteral( "raster2" ) ) );
QgsRasterMatrix result;
result.setNodataValue( -9999 );
QVERIFY( node.calculate( rasterData, result ) );
QCOMPARE( result.data()[0], 0.0 );
QCOMPARE( result.data()[1], -9999.0 );
QCOMPARE( result.data()[2], 11.0 );
QCOMPARE( result.data()[3], -9999.0 );
QCOMPARE( result.data()[4], 20.0 );
QCOMPARE( result.data()[5], -9999.0 );
}
void TestQgsRasterCalculator::calcWithLayers()
{
QgsRasterCalculatorEntry entry1;
entry1.bandNumber = 1;
entry1.raster = mpLandsatRasterLayer;
entry1.ref = QStringLiteral( "landsat@1" );
QgsRasterCalculatorEntry entry2;
entry2.bandNumber = 2;
entry2.raster = mpLandsatRasterLayer;
entry2.ref = QStringLiteral( "landsat@2" );
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1 << entry2;
QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );
QTemporaryFile tmpFile;
tmpFile.open(); // fileName is no avialable until open
QString tmpName = tmpFile.fileName();
tmpFile.close();
QgsRasterCalculator rc( QStringLiteral( "\"landsat@1\" + 2" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 0 );
//open output file and check results
QgsRasterLayer *result = new QgsRasterLayer( tmpName, QStringLiteral( "result" ) );
QCOMPARE( result->width(), 2 );
QCOMPARE( result->height(), 3 );
QgsRasterBlock *block = result->dataProvider()->block( 1, extent, 2, 3 );
QCOMPARE( block->value( 0, 0 ), 127.0 );
QCOMPARE( block->value( 0, 1 ), 127.0 );
QCOMPARE( block->value( 1, 0 ), 126.0 );
QCOMPARE( block->value( 1, 1 ), 127.0 );
QCOMPARE( block->value( 2, 0 ), 127.0 );
QCOMPARE( block->value( 2, 1 ), 126.0 );
delete result;
delete block;
//now try with 2 raster bands
QgsRasterCalculator rc2( QStringLiteral( "\"landsat@1\" + \"landsat@2\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc2.processCalculation() ), 0 );
//open output file and check results
result = new QgsRasterLayer( tmpName, QStringLiteral( "result" ) );
QCOMPARE( result->width(), 2 );
QCOMPARE( result->height(), 3 );
block = result->dataProvider()->block( 1, extent, 2, 3 );
QCOMPARE( block->value( 0, 0 ), 265.0 );
QCOMPARE( block->value( 0, 1 ), 263.0 );
QCOMPARE( block->value( 1, 0 ), 263.0 );
QCOMPARE( block->value( 1, 1 ), 264.0 );
QCOMPARE( block->value( 2, 0 ), 266.0 );
QCOMPARE( block->value( 2, 1 ), 261.0 );
delete result;
delete block;
}
void TestQgsRasterCalculator::calcWithReprojectedLayers()
{
QgsRasterCalculatorEntry entry1;
entry1.bandNumber = 1;
entry1.raster = mpLandsatRasterLayer;
entry1.ref = QStringLiteral( "landsat@1" );
QgsRasterCalculatorEntry entry2;
entry2.bandNumber = 2;
entry2.raster = mpLandsatRasterLayer4326;
entry2.ref = QStringLiteral( "landsat_4326@2" );
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1 << entry2;
QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );
QTemporaryFile tmpFile;
tmpFile.open(); // fileName is not available until open
QString tmpName = tmpFile.fileName();
tmpFile.close();
QgsRasterCalculator rc( QStringLiteral( "\"landsat@1\" + \"landsat_4326@2\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 0 );
//open output file and check results
QgsRasterLayer *result = new QgsRasterLayer( tmpName, QStringLiteral( "result" ) );
QCOMPARE( result->width(), 2 );
QCOMPARE( result->height(), 3 );
QgsRasterBlock *block = result->dataProvider()->block( 1, extent, 2, 3 );
QCOMPARE( block->value( 0, 0 ), 264.0 );
QCOMPARE( block->value( 0, 1 ), 263.0 );
QCOMPARE( block->value( 1, 0 ), 264.0 );
QCOMPARE( block->value( 1, 1 ), 264.0 );
QCOMPARE( block->value( 2, 0 ), 266.0 );
QCOMPARE( block->value( 2, 1 ), 261.0 );
delete result;
delete block;
}
void TestQgsRasterCalculator::findNodes()
{
std::unique_ptr< QgsRasterCalcNode > calcNode;
auto _test =
[ & ]( QString exp, const QgsRasterCalcNode::Type type ) -> QList<const QgsRasterCalcNode *>
{
QString error;
calcNode.reset( QgsRasterCalcNode::parseRasterCalcString( exp, error ) );
return calcNode->findNodes( type );
};
QCOMPARE( _test( QStringLiteral( "atan(\"raster@1\") * cos( 3 + 2 )" ), QgsRasterCalcNode::Type::tOperator ).length(), 4 );
QCOMPARE( _test( QStringLiteral( "\"raster@1\"" ), QgsRasterCalcNode::Type::tOperator ).length(), 0 );
QCOMPARE( _test( QStringLiteral( "\"raster@1\"" ), QgsRasterCalcNode::Type::tRasterRef ).length(), 1 );
QCOMPARE( _test( QStringLiteral( "\"raster@1\"" ), QgsRasterCalcNode::Type::tMatrix ).length(), 0 );
QCOMPARE( _test( QStringLiteral( "2 + 3" ), QgsRasterCalcNode::Type::tNumber ).length(), 2 );
QCOMPARE( _test( QStringLiteral( "2 + 3" ), QgsRasterCalcNode::Type::tOperator ).length(), 1 );
}
void TestQgsRasterCalculator::testRasterEntries()
{
// Create some test layers
QList<QgsMapLayer *> layers;
QgsRasterLayer *rlayer = new QgsRasterLayer( QStringLiteral( TEST_DATA_DIR ) + "/analysis/dem.tif", QStringLiteral( "dem" ) );
layers << rlayer;
// Duplicate name, same source
rlayer = new QgsRasterLayer( QStringLiteral( TEST_DATA_DIR ) + "/analysis/dem.tif", QStringLiteral( "dem" ) );
layers << rlayer;
// Duplicated name different source
rlayer = new QgsRasterLayer( QStringLiteral( TEST_DATA_DIR ) + "/analysis/dem_int16.tif", QStringLiteral( "dem" ) );
layers << rlayer;
// Different name and different source
rlayer = new QgsRasterLayer( QStringLiteral( TEST_DATA_DIR ) + "/analysis/slope.tif", QStringLiteral( "slope" ) );
layers << rlayer ;
// Different name and same source
rlayer = new QgsRasterLayer( QStringLiteral( TEST_DATA_DIR ) + "/analysis/slope.tif", QStringLiteral( "slope2" ) );
layers << rlayer ;
QgsProject::instance()->addMapLayers( layers );
QList<QgsRasterCalculatorEntry> availableRasterBands = QgsRasterCalculatorEntry::rasterEntries();
QMap<QString, QgsRasterCalculatorEntry> entryMap;
for ( const auto &rb : qgis::as_const( availableRasterBands ) )
{
entryMap[rb.ref] = rb;
}
QStringList keys( entryMap.keys() );
keys.sort();
QCOMPARE( keys.join( ',' ), QStringLiteral( "dem@1,dem_1@1,landsat@1,landsat_4326@1,slope2@1" ) );
}
void TestQgsRasterCalculator::errors( )
{
QgsRasterCalculatorEntry entry1;
entry1.bandNumber = 0; // bad band
entry1.raster = mpLandsatRasterLayer;
entry1.ref = QStringLiteral( "landsat@0" );
QVector<QgsRasterCalculatorEntry> entries;
entries << entry1;
QgsCoordinateReferenceSystem crs;
crs.createFromId( 32633, QgsCoordinateReferenceSystem::EpsgCrsId );
QgsRectangle extent( 783235, 3348110, 783350, 3347960 );
QTemporaryFile tmpFile;
tmpFile.open(); // fileName is not available until open
QString tmpName = tmpFile.fileName();
tmpFile.close();
QgsRasterCalculator rc( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 6 );
QCOMPARE( rc.lastError(), QStringLiteral( "Band number 0 is not valid for entry landsat@0" ) );
entry1.bandNumber = 10; // bad band
entries.clear();
entries << entry1;
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 6 );
QCOMPARE( rc.lastError(), QStringLiteral( "Band number 10 is not valid for entry landsat@0" ) );
// no raster
entry1.raster = nullptr;
entry1.bandNumber = 1;
entries.clear();
entries << entry1;
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 2 );
QCOMPARE( rc.lastError(), QStringLiteral( "No raster layer for entry landsat@0" ) );
// bad driver
entry1.raster = mpLandsatRasterLayer;
entry1.bandNumber = 1;
entries.clear();
entries << entry1;
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "xxxxx" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 1 );
QCOMPARE( rc.lastError(), QStringLiteral( "Could not obtain driver for xxxxx" ) );
// bad filename
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
QStringLiteral( "/goodluckwritinghere/blah/blah.tif" ),
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 1 );
QCOMPARE( rc.lastError(), QStringLiteral( "Could not create output /goodluckwritinghere/blah/blah.tif" ) );
// canceled
QgsFeedback feedback;
feedback.cancel();
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
QCOMPARE( static_cast< int >( rc.processCalculation( &feedback ) ), 3 );
QVERIFY( rc.lastError().isEmpty() );
}
void TestQgsRasterCalculator::toString()
{
auto _test = [ ]( QString exp, bool cStyle ) -> QString
{
QString error;
std::unique_ptr< QgsRasterCalcNode > calcNode( QgsRasterCalcNode::parseRasterCalcString( exp, error ) );
if ( ! error.isEmpty() )
return error;
return calcNode->toString( cStyle );
};
QCOMPARE( _test( QStringLiteral( "\"raster@1\" + 2" ), false ), QString( "\"raster@1\" + 2" ) );
QCOMPARE( _test( QStringLiteral( "\"raster@1\" + 2" ), true ), QString( "\"raster@1\" + 2" ) );
QCOMPARE( _test( QStringLiteral( "\"raster@1\" ^ 3 + 2" ), false ), QString( "\"raster@1\"^3 + 2" ) );
QCOMPARE( _test( QStringLiteral( "\"raster@1\" ^ 3 + 2" ), true ), QString( "pow( \"raster@1\", 3 ) + 2" ) );
QCOMPARE( _test( QStringLiteral( "atan(\"raster@1\") * cos( 3 + 2 )" ), false ), QString( "atan( \"raster@1\" ) * cos( 3 + 2 )" ) );
QCOMPARE( _test( QStringLiteral( "atan(\"raster@1\") * cos( 3 + 2 )" ), true ), QString( "atan( \"raster@1\" ) * cos( 3 + 2 )" ) );
}
QGSTEST_MAIN( TestQgsRasterCalculator )
#include "testqgsrastercalculator.moc"