Skip to content

Commit 8586feb

Browse files
author
mhugent
committed
[FEATURE]: AND and OR operator for raster calculator
git-svn-id: http://svn.osgeo.org/qgis/trunk@14892 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0c5fa88 commit 8586feb

File tree

9 files changed

+154
-11
lines changed

9 files changed

+154
-11
lines changed

src/analysis/raster/qgsrastercalclexer.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ raster_band_ref ({raster_ref_char}+)@{dig}
5858
"acos" { rasterlval.op = QgsRasterCalcNode::opACOS; return FUNCTION;}
5959
"atan" { rasterlval.op = QgsRasterCalcNode::opATAN; return FUNCTION;}
6060

61+
"AND" { return AND; }
62+
"OR" { return OR; }
6163
"!=" { return NE; }
6264
"<=" { return LE; }
6365
">=" { return GE; }

src/analysis/raster/qgsrastercalcnode.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ QgsRasterCalcNode::QgsRasterCalcNode( const QString& rasterName ): mType( tRaste
1919

2020
QgsRasterCalcNode::~QgsRasterCalcNode()
2121
{
22-
if( mLeft )
22+
if ( mLeft )
2323
{
2424
delete mLeft;
2525
}
26-
if( mRight )
26+
if ( mRight )
2727
{
2828
delete mRight;
2929
}
@@ -34,10 +34,10 @@ bool QgsRasterCalcNode::calculate( QMap<QString, QgsRasterMatrix*>& rasterData,
3434
//if type is raster ref: return a copy of the corresponding matrix
3535

3636
//if type is operator, call the proper matrix operations
37-
if( mType == tRasterRef )
37+
if ( mType == tRasterRef )
3838
{
3939
QMap<QString, QgsRasterMatrix*>::iterator it = rasterData.find( mRasterName );
40-
if( it == rasterData.end() )
40+
if ( it == rasterData.end() )
4141
{
4242
return false;
4343
}
@@ -48,20 +48,20 @@ bool QgsRasterCalcNode::calculate( QMap<QString, QgsRasterMatrix*>& rasterData,
4848
result.setData(( *it )->nColumns(), ( *it )->nRows(), data, ( *it )->nodataValue() );
4949
return true;
5050
}
51-
else if( mType == tOperator )
51+
else if ( mType == tOperator )
5252
{
5353
QgsRasterMatrix leftMatrix, rightMatrix;
5454
QgsRasterMatrix resultMatrix;
55-
if( !mLeft || !mLeft->calculate( rasterData, leftMatrix ) )
55+
if ( !mLeft || !mLeft->calculate( rasterData, leftMatrix ) )
5656
{
5757
return false;
5858
}
59-
if( mRight && !mRight->calculate( rasterData, rightMatrix ) )
59+
if ( mRight && !mRight->calculate( rasterData, rightMatrix ) )
6060
{
6161
return false;
6262
}
6363

64-
switch( mOperator )
64+
switch ( mOperator )
6565
{
6666
case opPLUS:
6767
leftMatrix.add( rightMatrix );
@@ -96,6 +96,12 @@ bool QgsRasterCalcNode::calculate( QMap<QString, QgsRasterMatrix*>& rasterData,
9696
case opLE:
9797
leftMatrix.lesserEqual( rightMatrix );
9898
break;
99+
case opAND:
100+
leftMatrix.logicalAnd( rightMatrix );
101+
break;
102+
case opOR:
103+
leftMatrix.logicalOr( rightMatrix );
104+
break;
99105
case opSQRT:
100106
leftMatrix.squareRoot();
101107
break;
@@ -125,7 +131,7 @@ bool QgsRasterCalcNode::calculate( QMap<QString, QgsRasterMatrix*>& rasterData,
125131
result.setData( newNColumns, newNRows, leftMatrix.takeData(), leftMatrix.nodataValue() );
126132
return true;
127133
}
128-
else if( mType == tNumber )
134+
else if ( mType == tNumber )
129135
{
130136
float* data = new float[1];
131137
data[0] = mNumber;

src/analysis/raster/qgsrastercalcnode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class ANALYSIS_EXPORT QgsRasterCalcNode
5555
opLT, // <
5656
opGE, // >=
5757
opLE, // <=
58+
opAND,
59+
opOR
5860
};
5961

6062
QgsRasterCalcNode();

src/analysis/raster/qgsrastercalcparser.yy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
%type <node> root
5656
%type <node> raster_exp
5757

58+
%left AND
59+
%left OR
5860
%left NE
5961
%left GE
6062
%left LE
@@ -71,6 +73,8 @@ root: raster_exp{}
7173

7274
raster_exp:
7375
FUNCTION '(' raster_exp ')' { $$ = new QgsRasterCalcNode($1, $3, 0); joinTmpNodes($$, $3, 0);}
76+
| raster_exp AND raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opAND, $1, $3 ); joinTmpNodes($$,$1,$3); }
77+
| raster_exp OR raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opOR, $1, $3 ); joinTmpNodes($$,$1,$3); }
7478
| raster_exp '=' raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opEQ, $1, $3 ); joinTmpNodes($$,$1,$3); }
7579
| raster_exp NE raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opNE, $1, $3 ); joinTmpNodes($$,$1,$3); }
7680
| raster_exp '>' raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opGT, $1, $3 ); joinTmpNodes($$, $1, $3); }

src/analysis/raster/qgsrastermatrix.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ bool QgsRasterMatrix::lesserEqual( const QgsRasterMatrix& other )
122122
return twoArgumentOperation( opLE, other );
123123
}
124124

125+
bool QgsRasterMatrix::logicalAnd( const QgsRasterMatrix& other )
126+
{
127+
return twoArgumentOperation( opAND, other );
128+
}
129+
130+
bool QgsRasterMatrix::logicalOr( const QgsRasterMatrix& other )
131+
{
132+
return twoArgumentOperation( opOR, other );
133+
}
134+
125135
bool QgsRasterMatrix::squareRoot()
126136
{
127137
return oneArgumentOperation( opSQRT );
@@ -266,6 +276,12 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
266276
case opLE:
267277
mData[0] = mData[0] <= other.number() ? 1.0f : 0.0f;
268278
break;
279+
case opAND:
280+
mData[0] = mData[0] && other.number() ? 1.0f : 0.0f;
281+
break;
282+
case opOR:
283+
mData[0] = mData[0] || other.number() ? 1.0f : 0.0f;
284+
break;
269285
}
270286
return true;
271287
}
@@ -335,6 +351,12 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
335351
case opLE:
336352
mData[i] = value1 <= value2 ? 1.0f : 0.0f;
337353
break;
354+
case opAND:
355+
mData[i] = value1 && value2 ? 1.0f : 0.0f;
356+
break;
357+
case opOR:
358+
mData[i] = value1 || value2 ? 1.0f : 0.0f;
359+
break;
338360
}
339361
}
340362
}
@@ -417,6 +439,12 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
417439
case opLE:
418440
mData[i] = value <= matrix[i] ? 1.0f : 0.0f;
419441
break;
442+
case opAND:
443+
mData[i] = value && matrix[i] ? 1.0f : 0.0f;
444+
break;
445+
case opOR:
446+
mData[i] = value || matrix[i] ? 1.0f : 0.0f;
447+
break;
420448
}
421449
}
422450
return true;
@@ -491,6 +519,12 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMa
491519
case opLE:
492520
mData[i] = mData[i] <= value ? 1.0f : 0.0f;
493521
break;
522+
case opAND:
523+
mData[i] = mData[i] && value ? 1.0f : 0.0f;
524+
break;
525+
case opOR:
526+
mData[i] = mData[i] || value ? 1.0f : 0.0f;
527+
break;
494528
}
495529
}
496530
return true;

src/analysis/raster/qgsrastermatrix.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ANALYSIS_EXPORT QgsRasterMatrix
3535
opLT, // <
3636
opGE, // >=
3737
opLE, // <=
38+
opAND,
39+
opOR
3840
};
3941

4042
enum OneArgOperator
@@ -85,6 +87,8 @@ class ANALYSIS_EXPORT QgsRasterMatrix
8587
bool lesserThan( const QgsRasterMatrix& other );
8688
bool greaterEqual( const QgsRasterMatrix& other );
8789
bool lesserEqual( const QgsRasterMatrix& other );
90+
bool logicalAnd( const QgsRasterMatrix& other );
91+
bool logicalOr( const QgsRasterMatrix& other );
8892

8993
bool squareRoot();
9094
bool sinus();
@@ -100,7 +104,7 @@ class ANALYSIS_EXPORT QgsRasterMatrix
100104
float* mData;
101105
double mNodataValue;
102106

103-
/**+,-,*,/,^,<,>,<=,>=,=,!=*/
107+
/**+,-,*,/,^,<,>,<=,>=,=,!=, and, or*/
104108
bool twoArgumentOperation( TwoArgOperator op, const QgsRasterMatrix& other );
105109
/*sqrt, sin, cos, tan, asin, acos, atan*/
106110
bool oneArgumentOperation( OneArgOperator op );

src/app/qgsrastercalcdialog.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,38 @@ void QgsRasterCalcDialog::on_mCloseBracketPushButton_clicked()
389389
{
390390
mExpressionTextEdit->insertPlainText( " ) " );
391391
}
392+
393+
void QgsRasterCalcDialog::on_mLessButton_clicked()
394+
{
395+
mExpressionTextEdit->insertPlainText( " < " );
396+
}
397+
398+
void QgsRasterCalcDialog::on_mGreaterButton_clicked()
399+
{
400+
mExpressionTextEdit->insertPlainText( " > " );
401+
}
402+
403+
void QgsRasterCalcDialog::on_mEqualButton_clicked()
404+
{
405+
mExpressionTextEdit->insertPlainText( " = " );
406+
}
407+
408+
void QgsRasterCalcDialog::on_mLesserEqualButton_clicked()
409+
{
410+
mExpressionTextEdit->insertPlainText( " <= " );
411+
}
412+
413+
void QgsRasterCalcDialog::on_mGreaterEqualButton_clicked()
414+
{
415+
mExpressionTextEdit->insertPlainText( " >= " );
416+
}
417+
418+
void QgsRasterCalcDialog::on_mAndButton_clicked()
419+
{
420+
mExpressionTextEdit->insertPlainText( " AND " );
421+
}
422+
423+
void QgsRasterCalcDialog::on_mOrButton_clicked()
424+
{
425+
mExpressionTextEdit->insertPlainText( " OR " );
426+
}

src/app/qgsrastercalcdialog.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcDialogBase
6868
void on_mATanButton_clicked();
6969
void on_mOpenBracketPushButton_clicked();
7070
void on_mCloseBracketPushButton_clicked();
71+
void on_mLessButton_clicked();
72+
void on_mGreaterButton_clicked();
73+
void on_mEqualButton_clicked();
74+
void on_mLesserEqualButton_clicked();
75+
void on_mGreaterEqualButton_clicked();
76+
void on_mAndButton_clicked();
77+
void on_mOrButton_clicked();
7178

7279
private:
7380
//insert available GDAL drivers that support the create() option

src/ui/qgsrastercalcdialogbase.ui

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>651</width>
10-
<height>512</height>
10+
<height>518</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -323,6 +323,55 @@
323323
</property>
324324
</widget>
325325
</item>
326+
<item row="2" column="0">
327+
<widget class="QPushButton" name="mLessButton">
328+
<property name="text">
329+
<string>&lt;</string>
330+
</property>
331+
</widget>
332+
</item>
333+
<item row="2" column="1">
334+
<widget class="QPushButton" name="mGreaterButton">
335+
<property name="text">
336+
<string>&gt;</string>
337+
</property>
338+
</widget>
339+
</item>
340+
<item row="2" column="2">
341+
<widget class="QPushButton" name="mEqualButton">
342+
<property name="text">
343+
<string>=</string>
344+
</property>
345+
</widget>
346+
</item>
347+
<item row="2" column="6">
348+
<widget class="QPushButton" name="mOrButton">
349+
<property name="text">
350+
<string>OR</string>
351+
</property>
352+
</widget>
353+
</item>
354+
<item row="2" column="5">
355+
<widget class="QPushButton" name="mAndButton">
356+
<property name="text">
357+
<string>AND</string>
358+
</property>
359+
</widget>
360+
</item>
361+
<item row="2" column="3">
362+
<widget class="QPushButton" name="mLesserEqualButton">
363+
<property name="text">
364+
<string>&lt;=</string>
365+
</property>
366+
</widget>
367+
</item>
368+
<item row="2" column="4">
369+
<widget class="QPushButton" name="mGreaterEqualButton">
370+
<property name="text">
371+
<string>&gt;=</string>
372+
</property>
373+
</widget>
374+
</item>
326375
</layout>
327376
</widget>
328377
</item>

0 commit comments

Comments
 (0)