Skip to content

Commit 1219a0f

Browse files
committed
[rastercalc] Consolidate duplicate code
1 parent f42f640 commit 1219a0f

File tree

2 files changed

+57
-225
lines changed

2 files changed

+57
-225
lines changed

src/analysis/raster/qgsrastermatrix.cpp

+54-224
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,54 @@ bool QgsRasterMatrix::oneArgumentOperation( OneArgOperator op )
264264
return true;
265265
}
266266

267+
double QgsRasterMatrix::calculateTwoArgumentOp( TwoArgOperator op, double arg1, double arg2 ) const
268+
{
269+
switch ( op )
270+
{
271+
case opPLUS:
272+
return arg1 + arg2;
273+
case opMINUS:
274+
return arg1 - arg2;
275+
case opMUL:
276+
return arg1 * arg2;
277+
case opDIV:
278+
if ( arg2 == 0 )
279+
{
280+
return mNodataValue;
281+
}
282+
else
283+
{
284+
return arg1 / arg2;
285+
}
286+
case opPOW:
287+
if ( !testPowerValidity( arg1, arg2 ) )
288+
{
289+
return mNodataValue;
290+
}
291+
else
292+
{
293+
return qPow( arg1, arg2 );
294+
}
295+
case opEQ:
296+
return ( arg1 == arg2 ? 1.0 : 0.0 );
297+
case opNE:
298+
return ( arg1 == arg2 ? 0.0 : 1.0 );
299+
case opGT:
300+
return ( arg1 > arg2 ? 1.0 : 0.0 );
301+
case opLT:
302+
return ( arg1 < arg2 ? 1.0 : 0.0 );
303+
case opGE:
304+
return ( arg1 >= arg2 ? 1.0 : 0.0 );
305+
case opLE:
306+
return ( arg1 <= arg2 ? 1.0 : 0.0 );
307+
case opAND:
308+
return ( arg1 && arg2 ? 1.0 : 0.0 );
309+
case opOR:
310+
return ( arg1 || arg2 ? 1.0 : 0.0 );
311+
}
312+
return mNodataValue;
313+
}
314+
267315
bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMatrix& other )
268316
{
269317
if ( isNumber() && other.isNumber() ) //operation on two 1x1 matrices
@@ -272,63 +320,10 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
272320
if ( mData[0] == mNodataValue || other.number() == other.nodataValue() )
273321
{
274322
mData[0] = mNodataValue;
275-
return true;
276323
}
277-
switch ( op )
324+
else
278325
{
279-
case opPLUS:
280-
mData[0] = number() + other.number();
281-
break;
282-
case opMINUS:
283-
mData[0] = number() - other.number();
284-
break;
285-
case opMUL:
286-
mData[0] = number() * other.number();
287-
break;
288-
case opDIV:
289-
if ( other.number() == 0 )
290-
{
291-
mData[0] = mNodataValue;
292-
}
293-
else
294-
{
295-
mData[0] = number() / other.number();
296-
}
297-
break;
298-
case opPOW:
299-
if ( !testPowerValidity( mData[0], other.number() ) )
300-
{
301-
mData[0] = mNodataValue;
302-
}
303-
else
304-
{
305-
mData[0] = qPow( mData[0], other.number() );
306-
}
307-
break;
308-
case opEQ:
309-
mData[0] = mData[0] == other.number() ? 1.0 : 0.0;
310-
break;
311-
case opNE:
312-
mData[0] = mData[0] == other.number() ? 0.0 : 1.0;
313-
break;
314-
case opGT:
315-
mData[0] = mData[0] > other.number() ? 1.0 : 0.0;
316-
break;
317-
case opLT:
318-
mData[0] = mData[0] < other.number() ? 1.0 : 0.0;
319-
break;
320-
case opGE:
321-
mData[0] = mData[0] >= other.number() ? 1.0 : 0.0;
322-
break;
323-
case opLE:
324-
mData[0] = mData[0] <= other.number() ? 1.0 : 0.0;
325-
break;
326-
case opAND:
327-
mData[0] = mData[0] && other.number() ? 1.0 : 0.0;
328-
break;
329-
case opOR:
330-
mData[0] = mData[0] || other.number() ? 1.0 : 0.0;
331-
break;
326+
mData[0] = calculateTwoArgumentOp( op, mData[0], other.number() );
332327
}
333328
return true;
334329
}
@@ -349,62 +344,7 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
349344
}
350345
else
351346
{
352-
switch ( op )
353-
{
354-
case opPLUS:
355-
mData[i] = value1 + value2;
356-
break;
357-
case opMINUS:
358-
mData[i] = value1 - value2;
359-
break;
360-
case opMUL:
361-
mData[i] = value1 * value2;
362-
break;
363-
case opDIV:
364-
if ( value2 == 0 )
365-
{
366-
mData[i] = mNodataValue;
367-
}
368-
else
369-
{
370-
mData[i] = value1 / value2;
371-
}
372-
break;
373-
case opPOW:
374-
if ( !testPowerValidity( value1, value2 ) )
375-
{
376-
mData[i] = mNodataValue;
377-
}
378-
else
379-
{
380-
mData[i] = pow( value1, value2 );
381-
}
382-
break;
383-
case opEQ:
384-
mData[i] = value1 == value2 ? 1.0 : 0.0;
385-
break;
386-
case opNE:
387-
mData[i] = value1 == value2 ? 0.0 : 1.0;
388-
break;
389-
case opGT:
390-
mData[i] = value1 > value2 ? 1.0 : 0.0;
391-
break;
392-
case opLT:
393-
mData[i] = value1 < value2 ? 1.0 : 0.0;
394-
break;
395-
case opGE:
396-
mData[i] = value1 >= value2 ? 1.0 : 0.0;
397-
break;
398-
case opLE:
399-
mData[i] = value1 <= value2 ? 1.0 : 0.0;
400-
break;
401-
case opAND:
402-
mData[i] = value1 && value2 ? 1.0 : 0.0;
403-
break;
404-
case opOR:
405-
mData[i] = value1 || value2 ? 1.0 : 0.0;
406-
break;
407-
}
347+
mData[i] = calculateTwoArgumentOp( op, value1, value2 );
408348
}
409349
}
410350
return true;
@@ -437,62 +377,7 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
437377
continue;
438378
}
439379

440-
switch ( op )
441-
{
442-
case opPLUS:
443-
mData[i] = value + matrix[i];
444-
break;
445-
case opMINUS:
446-
mData[i] = value - matrix[i];
447-
break;
448-
case opMUL:
449-
mData[i] = value * matrix[i];
450-
break;
451-
case opDIV:
452-
if ( matrix[i] == 0 )
453-
{
454-
mData[i] = mNodataValue;
455-
}
456-
else
457-
{
458-
mData[i] = value / matrix[i] ;
459-
}
460-
break;
461-
case opPOW:
462-
if ( !testPowerValidity( value, matrix[i] ) )
463-
{
464-
mData[i] = mNodataValue;
465-
}
466-
else
467-
{
468-
mData[i] = qPow( value, matrix[i] );
469-
}
470-
break;
471-
case opEQ:
472-
mData[i] = value == matrix[i] ? 1.0 : 0.0;
473-
break;
474-
case opNE:
475-
mData[i] = value == matrix[i] ? 0.0 : 1.0;
476-
break;
477-
case opGT:
478-
mData[i] = value > matrix[i] ? 1.0 : 0.0;
479-
break;
480-
case opLT:
481-
mData[i] = value < matrix[i] ? 1.0 : 0.0;
482-
break;
483-
case opGE:
484-
mData[i] = value >= matrix[i] ? 1.0 : 0.0;
485-
break;
486-
case opLE:
487-
mData[i] = value <= matrix[i] ? 1.0 : 0.0;
488-
break;
489-
case opAND:
490-
mData[i] = value && matrix[i] ? 1.0 : 0.0;
491-
break;
492-
case opOR:
493-
mData[i] = value || matrix[i] ? 1.0 : 0.0;
494-
break;
495-
}
380+
mData[i] = calculateTwoArgumentOp( op, value, matrix[i] );
496381
}
497382
return true;
498383
}
@@ -517,68 +402,13 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
517402
continue;
518403
}
519404

520-
switch ( op )
521-
{
522-
case opPLUS:
523-
mData[i] = mData[i] + value;
524-
break;
525-
case opMINUS:
526-
mData[i] = mData[i] - value;
527-
break;
528-
case opMUL:
529-
mData[i] = mData[i] * value;
530-
break;
531-
case opDIV:
532-
if ( value == 0 )
533-
{
534-
mData[i] = mNodataValue;
535-
}
536-
else
537-
{
538-
mData[i] = mData[i] / value;
539-
}
540-
break;
541-
case opPOW:
542-
if ( !testPowerValidity( mData[i], value ) )
543-
{
544-
mData[i] = mNodataValue;
545-
}
546-
else
547-
{
548-
mData[i] = qPow( mData[i], value );
549-
}
550-
break;
551-
case opEQ:
552-
mData[i] = mData[i] == value ? 1.0 : 0.0;
553-
break;
554-
case opNE:
555-
mData[i] = mData[i] == value ? 0.0 : 1.0;
556-
break;
557-
case opGT:
558-
mData[i] = mData[i] > value ? 1.0 : 0.0;
559-
break;
560-
case opLT:
561-
mData[i] = mData[i] < value ? 1.0 : 0.0;
562-
break;
563-
case opGE:
564-
mData[i] = mData[i] >= value ? 1.0 : 0.0;
565-
break;
566-
case opLE:
567-
mData[i] = mData[i] <= value ? 1.0 : 0.0;
568-
break;
569-
case opAND:
570-
mData[i] = mData[i] && value ? 1.0 : 0.0;
571-
break;
572-
case opOR:
573-
mData[i] = mData[i] || value ? 1.0 : 0.0;
574-
break;
575-
}
405+
mData[i] = calculateTwoArgumentOp( op, mData[i], value );
576406
}
577407
return true;
578408
}
579409
}
580410

581-
bool QgsRasterMatrix::testPowerValidity( double base, double power )
411+
bool QgsRasterMatrix::testPowerValidity( double base, double power ) const
582412
{
583413
if (( base == 0 && power < 0 ) || ( base < 0 && ( power - floor( power ) ) > 0 ) )
584414
{

src/analysis/raster/qgsrastermatrix.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ class ANALYSIS_EXPORT QgsRasterMatrix
115115

116116
/**+,-,*,/,^,<,>,<=,>=,=,!=, and, or*/
117117
bool twoArgumentOperation( TwoArgOperator op, const QgsRasterMatrix& other );
118+
double calculateTwoArgumentOp( TwoArgOperator op, double arg1, double arg2 ) const;
119+
118120
/*sqrt, sin, cos, tan, asin, acos, atan*/
119121
bool oneArgumentOperation( OneArgOperator op );
120-
bool testPowerValidity( double base, double power );
122+
bool testPowerValidity( double base, double power ) const;
121123
};
122124

123125
#endif // QGSRASTERMATRIX_H

0 commit comments

Comments
 (0)