Index: src/analysis/raster/qgsrastermatrix.cpp =================================================================== --- src/analysis/raster/qgsrastermatrix.cpp (Revision 14475) +++ src/analysis/raster/qgsrastermatrix.cpp (Arbeitskopie) @@ -93,18 +93,48 @@ return twoArgumentOperation( opPOW, other ); } +bool QgsRasterMatrix::equal( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opEQ, other ); +} + +bool QgsRasterMatrix::notEqual( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opNE, other ); +} + +bool QgsRasterMatrix::greaterThan( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opGT, other ); +} + +bool QgsRasterMatrix::lesserThan( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opLT, other ); +} + +bool QgsRasterMatrix::greaterEqual( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opGE, other ); +} + +bool QgsRasterMatrix::lesserEqual( const QgsRasterMatrix& other ) +{ + return twoArgumentOperation( opLE, other ); +} + bool QgsRasterMatrix::squareRoot() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { double value = mData[i]; - if ( value >= 0 ) + if( value >= 0 ) { mData[i] = sqrt( value ); } @@ -118,13 +148,13 @@ bool QgsRasterMatrix::sinus() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = sin( mData[i] ); } @@ -133,13 +163,13 @@ bool QgsRasterMatrix::asinus() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = asin( mData[i] ); } @@ -148,13 +178,13 @@ bool QgsRasterMatrix::cosinus() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = cos( mData[i] ); } @@ -163,13 +193,13 @@ bool QgsRasterMatrix::acosinus() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = acos( mData[i] ); } @@ -178,13 +208,13 @@ bool QgsRasterMatrix::tangens() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = tan( mData[i] ); } @@ -193,13 +223,13 @@ bool QgsRasterMatrix::atangens() { - if ( !mData ) + if( !mData ) { return false; } int nEntries = mColumns * mRows; - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = atan( mData[i] ); } @@ -208,9 +238,9 @@ bool QgsRasterMatrix::twoArgumentOperation( TwoArgOperator op, const QgsRasterMatrix& other ) { - if ( isNumber() && other.isNumber() ) + if( isNumber() && other.isNumber() ) { - switch ( op ) + switch( op ) { case opPLUS: mData[0] = number() + other.number(); @@ -222,7 +252,7 @@ mData[0] = number() * other.number(); break; case opDIV: - if ( other.number() == 0 ) + if( other.number() == 0 ) { mData[0] = -10000; } @@ -232,7 +262,7 @@ } break; case opPOW: - if ( !testPowerValidity( mData[0], ( float ) other.number() ) ) + if( !testPowerValidity( mData[0], ( float ) other.number() ) ) { mData[0] = -10000; } @@ -241,38 +271,56 @@ mData[0] = pow( mData[0], ( float ) other.number() ); } break; + case opEQ: + mData[0] = ( mData[0] == other.number() ? 1 : 0 ); + break; + case opNE: + mData[0] = ( mData[0] == other.number() ? 0 : 1 ); + break; + case opGT: + mData[0] = ( mData[0] > other.number() ? 1 : 0 ); + break; + case opLT: + mData[0] = ( mData[0] < other.number() ? 1 : 0 ); + break; + case opGE: + mData[0] = ( mData[0] >= other.number() ? 1 : 0 ); + break; + case opLE: + mData[0] = ( mData[0] <= other.number() ? 1 : 0 ); + break; } return true; } //two matrices - if ( !isNumber() && !other.isNumber() ) + if( !isNumber() && !other.isNumber() ) { float* matrix = other.mData; int nEntries = mColumns * mRows; - switch ( op ) + switch( op ) { case opPLUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] + matrix[i]; } break; case opMINUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] - matrix[i]; } break; case opMUL: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] * matrix[i]; } break; case opDIV: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { - if ( matrix[i] == 0 ) + if( matrix[i] == 0 ) { mData[i] = -10000; } @@ -283,9 +331,9 @@ } break; case opPOW: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { - if ( !testPowerValidity( mData[i], matrix[i] ) ) + if( !testPowerValidity( mData[i], matrix[i] ) ) { mData[i] = -10000; } @@ -295,11 +343,47 @@ } } break; + case opEQ: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] == matrix[i] ? 1 : 0 ); + } + break; + case opNE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] == matrix[i] ? 0 : 1 ); + } + break; + case opGT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] > matrix[i] ? 1 : 0 ); + } + break; + case opLT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] < matrix[i] ? 1 : 0 ); + } + break; + case opGE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] >= matrix[i] ? 1 : 0 ); + } + break; + case opLE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] <= matrix[i] ? 1 : 0 ); + } + break; } return true; } double value = 0; - if ( isNumber() ) + if( isNumber() ) { float* matrix = other.mData; int nEntries = other.nColumns() * other.nRows(); @@ -307,30 +391,30 @@ delete[] mData; mData = new float[nEntries]; mColumns = other.nColumns(); mRows = other.nRows(); - switch ( op ) + switch( op ) { case opPLUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = value + matrix[i]; } break; case opMINUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = value - matrix[i]; } break; case opMUL: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = value * matrix[i]; } break; case opDIV: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { - if ( matrix[i] == 0 ) + if( matrix[i] == 0 ) { mData[i] = -10000; } @@ -341,9 +425,9 @@ } break; case opPOW: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { - if ( !testPowerValidity( value, matrix[i] ) ) + if( !testPowerValidity( value, matrix[i] ) ) { mData[i] = -10000; } @@ -353,52 +437,88 @@ } } break; + case opEQ: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value == matrix[i] ? 1 : 0 ); + } + break; + case opNE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value == matrix[i] ? 0 : 1 ); + } + break; + case opGT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value > matrix[i] ? 1 : 0 ); + } + break; + case opLT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value < matrix[i] ? 1 : 0 ); + } + break; + case opGE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value >= matrix[i] ? 1 : 0 ); + } + break; + case opLE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( value <= matrix[i] ? 1 : 0 ); + } + break; } } - else + else //this matrix is a real matrix and the other a number { value = other.number(); int nEntries = mColumns * mRows; - switch ( op ) + switch( op ) { case opPLUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] + value; } break; case opMINUS: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] - value; } break; case opMUL: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] * value; } break; case opDIV: - if ( value == 0 ) + if( value == 0 ) { - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = -10000; } } else { - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { mData[i] = mData[i] / value; } } break; case opPOW: - for ( int i = 0; i < nEntries; ++i ) + for( int i = 0; i < nEntries; ++i ) { - if ( !testPowerValidity( mData[i], value ) ) + if( !testPowerValidity( mData[i], value ) ) { mData[i] = -10000; } @@ -408,6 +528,42 @@ } } break; + case opEQ: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] == value ? 1 : 0 ); + } + break; + case opNE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] == value ? 0 : 1 ); + } + break; + case opGT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] > value ? 1 : 0 ); + } + break; + case opLT: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] < value ? 1 : 0 ); + } + break; + case opGE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] >= value ? 1 : 0 ); + } + break; + case opLE: + for( int i = 0; i < nEntries; ++i ) + { + mData[i] = ( mData[i] <= value ? 1 : 0 ); + } + break; } } return true; @@ -415,7 +571,7 @@ bool QgsRasterMatrix::testPowerValidity( double base, double power ) { - if (( base == 0 && power < 0 ) || ( power < 0 && ( power - floor( power ) ) > 0 ) ) + if(( base == 0 && power < 0 ) || ( power < 0 && ( power - floor( power ) ) > 0 ) ) { return false; } Index: src/analysis/raster/qgsrastercalcnode.cpp =================================================================== --- src/analysis/raster/qgsrastercalcnode.cpp (Revision 14475) +++ src/analysis/raster/qgsrastercalcnode.cpp (Arbeitskopie) @@ -18,11 +18,11 @@ QgsRasterCalcNode::~QgsRasterCalcNode() { - if ( mLeft ) + if( mLeft ) { delete mLeft; } - if ( mRight ) + if( mRight ) { delete mRight; } @@ -33,10 +33,10 @@ //if type is raster ref: return a copy of the corresponding matrix //if type is operator, call the proper matrix operations - if ( mType == tRasterRef ) + if( mType == tRasterRef ) { QMap::iterator it = rasterData.find( mRasterName ); - if ( it == rasterData.end() ) + if( it == rasterData.end() ) { return false; } @@ -47,20 +47,20 @@ result.setData(( *it )->nColumns(), ( *it )->nRows(), data ); return true; } - else if ( mType == tOperator ) + else if( mType == tOperator ) { QgsRasterMatrix leftMatrix, rightMatrix; QgsRasterMatrix resultMatrix; - if ( !mLeft || !mLeft->calculate( rasterData, leftMatrix ) ) + if( !mLeft || !mLeft->calculate( rasterData, leftMatrix ) ) { return false; } - if ( mRight && !mRight->calculate( rasterData, rightMatrix ) ) + if( mRight && !mRight->calculate( rasterData, rightMatrix ) ) { return false; } - switch ( mOperator ) + switch( mOperator ) { case opPLUS: leftMatrix.add( rightMatrix ); @@ -77,6 +77,24 @@ case opPOW: leftMatrix.power( rightMatrix ); break; + case opEQ: + leftMatrix.equal( rightMatrix ); + break; + case opNE: + leftMatrix.notEqual( rightMatrix ); + break; + case opGT: + leftMatrix.greaterThan( rightMatrix ); + break; + case opLT: + leftMatrix.lesserThan( rightMatrix ); + break; + case opGE: + leftMatrix.greaterEqual( rightMatrix ); + break; + case opLE: + leftMatrix.lesserEqual( rightMatrix ); + break; case opSQRT: leftMatrix.squareRoot(); break; @@ -106,7 +124,7 @@ result.setData( newNColumns, newNRows, leftMatrix.takeData() ); return true; } - else if ( mType == tNumber ) + else if( mType == tNumber ) { float* data = new float[1]; data[0] = mNumber; @@ -118,7 +136,7 @@ QgsRasterCalcNode* QgsRasterCalcNode::parseRasterCalcString( const QString& str, QString& parserErrorMsg ) { - extern QgsRasterCalcNode* localParseRasterCalcString( const QString& str, QString& parserErrorMsg ); + extern QgsRasterCalcNode* localParseRasterCalcString( const QString & str, QString & parserErrorMsg ); return localParseRasterCalcString( str, parserErrorMsg ); } Index: src/analysis/raster/qgsrastermatrix.h =================================================================== --- src/analysis/raster/qgsrastermatrix.h (Revision 14475) +++ src/analysis/raster/qgsrastermatrix.h (Arbeitskopie) @@ -29,6 +29,12 @@ opMUL, opDIV, opPOW, + opEQ, // = + opNE, // != resp. <> + opGT, // > + opLT, // < + opGE, // >= + opLE, // <= }; enum OneArgOperator @@ -39,7 +45,7 @@ opTAN, opASIN, opACOS, - opATAN + opATAN, }; /**Takes ownership of data array*/ @@ -70,6 +76,12 @@ bool multiply( const QgsRasterMatrix& other ); bool divide( const QgsRasterMatrix& other ); bool power( const QgsRasterMatrix& other ); + bool equal( const QgsRasterMatrix& other ); + bool notEqual( const QgsRasterMatrix& other ); + bool greaterThan( const QgsRasterMatrix& other ); + bool lesserThan( const QgsRasterMatrix& other ); + bool greaterEqual( const QgsRasterMatrix& other ); + bool lesserEqual( const QgsRasterMatrix& other ); bool squareRoot(); bool sinus(); Index: src/analysis/raster/qgsrastercalcnode.h =================================================================== --- src/analysis/raster/qgsrastercalcnode.h (Revision 14475) +++ src/analysis/raster/qgsrastercalcnode.h (Arbeitskopie) @@ -48,7 +48,13 @@ opTAN, opASIN, opACOS, - opATAN + opATAN, + opEQ, // = + opNE, //!= + opGT, // > + opLT, // < + opGE, // >= + opLE, // <= }; QgsRasterCalcNode(); Index: src/analysis/raster/qgsrastercalclexer.ll =================================================================== --- src/analysis/raster/qgsrastercalclexer.ll (Revision 14475) +++ src/analysis/raster/qgsrastercalclexer.ll (Arbeitskopie) @@ -58,8 +58,13 @@ "acos" { rasterlval.op = QgsRasterCalcNode::opACOS; return FUNCTION;} "atan" { rasterlval.op = QgsRasterCalcNode::opATAN; return FUNCTION;} -[+-/*^] { return yytext[0]; } +"!=" { return NE; } +"<=" { return LE; } +">=" { return GE; } +[=><+-/*^] { return yytext[0]; } + + [()] { return yytext[0]; } {number} { rasterlval.number = atof(rastertext); return NUMBER; } Index: src/analysis/raster/qgsrastercalcparser.yy =================================================================== --- src/analysis/raster/qgsrastercalcparser.yy (Revision 14475) +++ src/analysis/raster/qgsrastercalcparser.yy (Arbeitskopie) @@ -55,6 +55,11 @@ %type root %type raster_exp +%left NE +%left GE +%left LE + +%left '=' '<' '>' %left '+' '-' %left '*' '/' %left '^' @@ -66,6 +71,12 @@ raster_exp: FUNCTION '(' raster_exp ')' { $$ = new QgsRasterCalcNode($1, $3, 0); joinTmpNodes($$, $3, 0);} + | raster_exp '=' raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opEQ, $1, $3 ); joinTmpNodes($$,$1,$3); } + | raster_exp NE raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opNE, $1, $3 ); joinTmpNodes($$,$1,$3); } + | raster_exp '>' raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opGT, $1, $3 ); joinTmpNodes($$, $1, $3); } + | raster_exp '<' raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opLT, $1, $3 ); joinTmpNodes($$, $1, $3); } + | raster_exp GE raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opGE, $1, $3 ); joinTmpNodes($$, $1, $3); } + | raster_exp LE raster_exp { $$ = new QgsRasterCalcNode( QgsRasterCalcNode::opLE, $1, $3 ); joinTmpNodes($$, $1, $3); } | raster_exp '^' raster_exp { $$ = new QgsRasterCalcNode(QgsRasterCalcNode::opPOW, $1, $3); joinTmpNodes($$,$1,$3); } | raster_exp '*' raster_exp { $$ = new QgsRasterCalcNode(QgsRasterCalcNode::opMUL, $1, $3); joinTmpNodes($$,$1,$3); } | raster_exp '/' raster_exp { $$ = new QgsRasterCalcNode(QgsRasterCalcNode::opDIV, $1, $3); joinTmpNodes($$,$1,$3); }