QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrastercalcnode.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrastercalcnode.cpp
3  ---------------------
4  begin : October 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsrastercalcnode.h"
16 #include <cfloat>
17 
19  : mLeft( 0 )
20  , mRight( 0 )
21  , mNumber( 0 )
22 {
23 }
24 
26  : mType( tNumber )
27  , mLeft( 0 )
28  , mRight( 0 )
29  , mNumber( number )
30 {
31 }
32 
34  : mType( tOperator )
35  , mLeft( left )
36  , mRight( right )
37  , mNumber( 0 )
38  , mOperator( op )
39 {
40 }
41 
42 QgsRasterCalcNode::QgsRasterCalcNode( const QString& rasterName )
43  : mType( tRasterRef )
44  , mLeft( 0 )
45  , mRight( 0 )
46  , mNumber( 0 )
47  , mRasterName( rasterName )
48 {
49 }
50 
52 {
53  if ( mLeft )
54  {
55  delete mLeft;
56  }
57  if ( mRight )
58  {
59  delete mRight;
60  }
61 }
62 
63 bool QgsRasterCalcNode::calculate( QMap<QString, QgsRasterMatrix*>& rasterData, QgsRasterMatrix& result ) const
64 {
65  //if type is raster ref: return a copy of the corresponding matrix
66 
67  //if type is operator, call the proper matrix operations
68  if ( mType == tRasterRef )
69  {
70  QMap<QString, QgsRasterMatrix*>::iterator it = rasterData.find( mRasterName );
71  if ( it == rasterData.end() )
72  {
73  return false;
74  }
75 
76  int nEntries = ( *it )->nColumns() * ( *it )->nRows();
77  float* data = new float[nEntries];
78  memcpy( data, ( *it )->data(), nEntries * sizeof( float ) );
79  result.setData(( *it )->nColumns(), ( *it )->nRows(), data, ( *it )->nodataValue() );
80  return true;
81  }
82  else if ( mType == tOperator )
83  {
84  QgsRasterMatrix leftMatrix, rightMatrix;
85  QgsRasterMatrix resultMatrix;
86  if ( !mLeft || !mLeft->calculate( rasterData, leftMatrix ) )
87  {
88  return false;
89  }
90  if ( mRight && !mRight->calculate( rasterData, rightMatrix ) )
91  {
92  return false;
93  }
94 
95  switch ( mOperator )
96  {
97  case opPLUS:
98  leftMatrix.add( rightMatrix );
99  break;
100  case opMINUS:
101  leftMatrix.subtract( rightMatrix );
102  break;
103  case opMUL:
104  leftMatrix.multiply( rightMatrix );
105  break;
106  case opDIV:
107  leftMatrix.divide( rightMatrix );
108  break;
109  case opPOW:
110  leftMatrix.power( rightMatrix );
111  break;
112  case opEQ:
113  leftMatrix.equal( rightMatrix );
114  break;
115  case opNE:
116  leftMatrix.notEqual( rightMatrix );
117  break;
118  case opGT:
119  leftMatrix.greaterThan( rightMatrix );
120  break;
121  case opLT:
122  leftMatrix.lesserThan( rightMatrix );
123  break;
124  case opGE:
125  leftMatrix.greaterEqual( rightMatrix );
126  break;
127  case opLE:
128  leftMatrix.lesserEqual( rightMatrix );
129  break;
130  case opAND:
131  leftMatrix.logicalAnd( rightMatrix );
132  break;
133  case opOR:
134  leftMatrix.logicalOr( rightMatrix );
135  break;
136  case opSQRT:
137  leftMatrix.squareRoot();
138  break;
139  case opSIN:
140  leftMatrix.sinus();
141  break;
142  case opCOS:
143  leftMatrix.cosinus();
144  break;
145  case opTAN:
146  leftMatrix.tangens();
147  break;
148  case opASIN:
149  leftMatrix.asinus();
150  break;
151  case opACOS:
152  leftMatrix.acosinus();
153  break;
154  case opATAN:
155  leftMatrix.atangens();
156  case opSIGN:
157  leftMatrix.changeSign();
158  break;
159  default:
160  return false;
161  }
162  int newNColumns = leftMatrix.nColumns();
163  int newNRows = leftMatrix.nRows();
164  result.setData( newNColumns, newNRows, leftMatrix.takeData(), leftMatrix.nodataValue() );
165  return true;
166  }
167  else if ( mType == tNumber )
168  {
169  float* data = new float[1];
170  data[0] = mNumber;
171  result.setData( 1, 1, data, -FLT_MAX );
172  return true;
173  }
174  return false;
175 }
176 
177 QgsRasterCalcNode* QgsRasterCalcNode::parseRasterCalcString( const QString& str, QString& parserErrorMsg )
178 {
179  extern QgsRasterCalcNode* localParseRasterCalcString( const QString & str, QString & parserErrorMsg );
180  return localParseRasterCalcString( str, parserErrorMsg );
181 }
182