All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ArithmeticOperations.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/ArithmeticOperations.h
22  \brief Performs arithmetic operation over raster data.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
26 #define __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
27 
28 #include "Algorithm.h"
29 
30 #include <map>
31 #include <memory>
32 #include <stack>
33 #include <string>
34 #include <vector>
35 
36 #include <boost/shared_ptr.hpp>
37 
38 namespace te
39 {
40  namespace rst
41  {
42  class Raster;
43  }
44 
45  namespace rp
46  {
47  /*!
48  \class ArithmeticOperations
49 
50  \brief Performs arithmetic operation over raster data.
51 
52  \details The expression (arithmetic stringIt) must be formed by a combination of the following elements,
53  separated by spaces:
54 
55  Operators: +, -, *, /
56 
57  Real Numbers (negative numbers must follow the form "-1.0")
58 
59  Raster bands: R0:1, R0:1, R1:0, .... (R0:1 is a reference to the first raster - with index 0 - from feeder, second band - with index 1).
60 
61  \note Reference: TerraLib 4 - Image Processing Module
62 
63  \ingroup rp_gen
64  */
66  {
67  public:
68 
69  /*!
70  \class InputParameters
71  \brief ArithmeticOperations input parameters
72  */
74  {
75  public:
76 
77  std::vector< boost::shared_ptr<te::rst::Raster> > m_rasterVec; //!< Input rasters vector.
78 
79  std::string m_arithmeticString; //!< Arithmetic string.
80 
81  bool m_normalize; //!< Output values normalization will be performed to fit the allowed values range (default:false).
82 
83  bool m_enableProgress; //!< Enable/Disable the progress interface (default:false).
84 
86 
88 
89  ~InputParameters();
90 
91  //overload
92  void reset() throw( te::rp::Exception );
93 
94  //overload
95  const InputParameters& operator=( const InputParameters& params );
96 
97  //overload
98  AbstractParameters* clone() const;
99  };
100 
101  /*!
102  \class OutputParameters
103  \brief ArithmeticOperations output parameters
104  */
106  {
107  public:
108 
109  std::string m_rType; //!< Output raster data source type (as described in te::raster::RasterFactory ).
110 
111  std::map< std::string, std::string > m_rInfo; //!< The necessary information to create the output rasters (as described in te::raster::RasterFactory).
112 
113  std::auto_ptr< te::rst::Raster > m_outputRasterPtr; //!< The generated output registered raster.
114 
116 
118 
119  ~OutputParameters();
120 
121  //overload
122  void reset() throw( te::rp::Exception );
123 
124  //overload
125  const OutputParameters& operator=( const OutputParameters& params );
126 
127  //overload
128  AbstractParameters* clone() const;
129  };
130 
132 
134 
135  //overload
136  bool execute( AlgorithmOutputParameters& outputParams ) throw( te::rp::Exception );
137 
138  //overload
139  void reset() throw( te::rp::Exception );
140 
141  //overload
142  bool initialize( const AlgorithmInputParameters& inputParams ) throw( te::rp::Exception );
143 
144  bool isInitialized() const;
145 
146  protected:
147 
148  /*!
149  \class ExecStackElement
150  \brief Execution stack element.
151  */
153  {
154  public :
155 
156  bool m_isRaster; //!< true if this is a raster pointer element.
157 
158  bool m_isRealNumber; //!< true if this is a real number element.
159 
160  double m_realNumberValue; //!< Real number value.
161 
162  unsigned int m_rasterBand; //!< Raster band index.
163 
164  boost::shared_ptr<te::rst::Raster> m_raster; //!< Raster pointer.
165 
167  : m_isRaster( false ), m_isRealNumber( false ), m_realNumberValue( 0 ),
168  m_rasterBand( 0 )
169  {};
170 
172  };
173 
174  typedef std::stack< ExecStackElement > ExecStackT; //!< Execution stack type definition.
175 
177 
178  bool m_isInitialized; //!< Tells if this instance is initialized.
179 
180  /*!
181  \brief Execute the automata parsing the given input string.
182  \param aStr The input arithmetic expression string.
183  \param inRasters Input rasters pointers.
184  \param outRaster Output raster pointer (pre-initiated).
185  \param generateOutput If true, the output raster data will be generated, if false only the automata execution will be performed..
186  */
187  bool executeString( const std::string& aStr,
188  std::vector< boost::shared_ptr<te::rst::Raster> > inRasters,
189  boost::shared_ptr<te::rst::Raster>& outRaster,
190  bool generateOutput ) const;
191 
192  /*!
193  \brief Convert the input tokens vector from the infix notation to postfix notation.
194  \param input The input tokens vector.
195  \param output The output tokens vector.
196  */
197  void inFix2PostFix( const std::vector< std::string >& input,
198  std::vector< std::string >& output ) const;
199 
200  /*!
201  \brief Print tokens to stout.
202  \param input The input tokens vector.
203  */
204  void printTokens( const std::vector< std::string >& input ) const;
205 
206  /*!
207  \brief Returns true if the given token is an operator.
208  \param inputToken Input token.
209  \return Returns true if the given token is an operator.
210  */
211  bool isOperator( const std::string& inputToken ) const;
212 
213  /*!
214  \brief Returns true if the given token is a binary operator.
215  \param inputToken Input token.
216  \return Returns true if the given token is a binary operator.
217  */
218  bool isBinaryOperator( const std::string& inputToken ) const;
219 
220  /*!
221  \brief Returns true if the given token is a unary operator.
222  \param inputToken Input token.
223  \return Returns true if the given token is a unary operator.
224  */
225  bool isUnaryOperator( const std::string& inputToken ) const;
226 
227  /*!
228  \brief Returns true if operator1 has greater of equal precedence over operator2.
229  \param operator1 Operator1 input token.
230  \param operator2 Operator2 input token.
231  \return Returns true if operator1 has greater of equal precedence over operator2.
232  */
233  bool op1HasGreaterOrEqualPrecedence( const std::string& operator1,
234  const std::string& operator2 ) const;
235 
236  /*!
237  \brief Returns true if the given token is a raster data token.
238  \param token Input token.
239  \param rasterIdx The output converted raster index value.
240  \param bandIdx The output converted band index value.
241  \return Returns true if the given token is a raster data token.
242  */
243  bool isRasterBandToken( const std::string& token, unsigned int& rasterIdx,
244  unsigned int& bandIdx ) const;
245 
246  /*!
247  \brief Execute the given binary operator using the current given execution stack.
248  \param token Operator token.
249  \param execStack Execution stack.
250  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
251  \return true if OK, false on errors..
252  */
253  bool execBinaryOperator( const std::string& token, ExecStackT&
254  execStack, bool generateOutput ) const;
255 
256  /*!
257  \brief Execute the given unary operator using the current given execution stack.
258  \param token Operator token.
259  \param execStack Execution stack.
260  \param generateOutput if true the execution will generate valid output data, if false only dummy stack elements will be generated.
261  \return true if OK, false on errors..
262  */
263  bool execUnaryOperator( const std::string& token, ExecStackT&
264  execStack, bool generateOutput ) const;
265 
266  /*!
267  \brief Returns true if the given token is a real number.
268  \param token Input token.
269  \param realValue The output converted value.
270  \return Returns true if the given token is a real number.
271  */
272  bool isRealNumberToken( const std::string& token, double& realValue ) const;
273 
274  /*!
275  \brief Allocate a new RAM memory raster.
276  \param nLines Number of raster lines.
277  \param nCols Number of raster columns.
278  \param rasterPtr The output raster pointer.
279  \return Returns true if OK, false on errors.
280  */
281  bool allocResultRaster( unsigned int nLines, unsigned int nCols,
282  boost::shared_ptr<te::rst::Raster>& rasterPtr ) const;
283 
284  /*!
285  \brief Split the input string into a vector of token strings
286  \param inputStr The input string.
287  \param outTokens The generated output tokens vector.
288  */
289  void getTokensStrs( const std::string& inputStr,
290  std::vector< std::string >& outTokens ) const;
291  };
292  } // end namespace rp
293 } // end namespace te
294 
295 #endif // __TERRALIB_RP_INTERNAL_ARITHMETICOPERATIONS_H
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
bool m_isInitialized
Tells if this instance is initialized.
Raster Processing algorithm output parameters base interface.
bool m_normalize
Output values normalization will be performed to fit the allowed values range (default:false).
ArithmeticOperations::InputParameters m_inputParameters
Input execution parameters.
std::map< std::string, std::string > m_rInfo
The necessary information to create the output rasters (as described in te::raster::RasterFactory).
std::vector< boost::shared_ptr< te::rst::Raster > > m_rasterVec
Input rasters vector.
bool m_enableProgress
Enable/Disable the progress interface (default:false).
bool m_isRaster
true if this is a raster pointer element.
std::stack< ExecStackElement > ExecStackT
Execution stack type definition.
Raster Processing algorithm base interface.
Definition: Algorithm.h:41
ArithmeticOperations output parameters.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:111
Raster Processing algorithm base interface class.
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
The generated output registered raster.
Raster Processing algorithm input parameters base interface.
Performs arithmetic operation over raster data.
bool m_isRealNumber
true if this is a real number element.
ArithmeticOperations input parameters.
boost::shared_ptr< te::rst::Raster > m_raster
Raster pointer.
std::string m_arithmeticString
Arithmetic string.