AlgorithmParametersSerializer.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 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 #ifndef __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
21 #define __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
22 
23 #include "Config.h"
24 
25 #include<string>
26 #include<map>
27 #include<vector>
28 #include<complex>
29 
30 #include <boost/numeric/ublas/matrix.hpp>
31 
32 namespace te
33 {
34  namespace rp
35  {
36  /*!
37  \class AlgorithmParametersSerializer
38  \brief A class to standardize algorithm parameters serialization.
39  */
41  {
42  public:
43 
45 
47 
49 
51  const AlgorithmParametersSerializer& other );
52 
53  /*!
54  \brief Set a non-empty algorithm name.
55  \param The new non-empty algorithm name.
56  */
57  void setAlgorithmName( const std::string& name );
58 
59  /*!
60  \brief Add a new parameter.
61  \param name A new non-empty parameter name.
62  \param value A new parameter value.
63  \return true if ok, false on errors.
64  */
65  bool addParameter( const std::string& name, const std::string& value );
66 
67  /*!
68  \brief Add a new parameter.
69  \param name A new non-empty parameter name.
70  \param value A new parameter value.
71  \return true if ok, false on errors.
72  */
73  template< typename ValueType >
74  bool addParameter( const std::string& name, const ValueType& value )
75  {
76  try
77  {
78  return addParameter( name, toString( value ) );
79  }
80  catch(...)
81  {
82  return false;
83  }
84  }
85 
86  /*!
87  \brief Add a new multivalued parameter.
88  \param name A new non-empty parameter name.
89  \param value A new parameter values.
90  \return true if ok, false on errors.
91  */
92  bool addMultivaluedParameter( const std::string& name,
93  const std::vector< std::string >& values );
94 
95  /*!
96  \brief Add a new multivalued parameter.
97  \param name A new non-empty parameter name.
98  \param value A new parameter values.
99  \return true if ok, false on errors.
100  */
101  template< typename ValueType >
102  bool addMultivaluedParameter( const std::string& name,
103  const std::vector< ValueType >& values )
104  {
105  const std::size_t valuesSize = values.size();
106  std::vector< std::string > stringValues;
107 
108  try
109  {
110  for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
111  {
112  stringValues.push_back( toString( values[ valuesIdx ] ) );
113  }
114  }
115  catch(...)
116  {
117  return false;
118  }
119 
120  return addMultivaluedParameter( name, stringValues );
121  }
122 
123  /*!
124  \brief Add a new multivalued parameter in a matrix form.
125  \param name A new non-empty parameter name.
126  \param value A new parameter values in a matrix form.
127  \return true if ok, false on errors.
128  */
129  template< typename ValueType >
130  bool addMultivaluedParameter( const std::string& name,
131  const boost::numeric::ublas::matrix< ValueType >& values )
132  {
133  const std::size_t valuesSize1 = values.size1();
134  const std::size_t valuesSize2 = values.size2();
135  std::size_t values2Idx = 0;
136  std::vector< std::string > stringValues;
137 
138  try
139  {
140  for( std::size_t values1Idx = 0 ; values1Idx < valuesSize1 ; ++values1Idx )
141  {
142  for( values2Idx = 0 ; values2Idx < valuesSize2 ; ++values2Idx )
143  {
144  stringValues.push_back( toString( values( values1Idx, values2Idx ) ) );
145  }
146  }
147  }
148  catch(...)
149  {
150  return false;
151  }
152 
153  return addMultivaluedParameter( name, stringValues );
154  }
155 
156  /*!
157  \brief Add sets of new multivalued parameters.
158  \param name A new non-empty parameter name.
159  \param values Parameter values.
160  \return true if ok, false on errors.
161  */
162  template< typename ValueType >
163  bool addMultivaluedParameters( const std::string& name,
164  const std::vector< std::vector< ValueType > >& values )
165  {
166  const std::size_t valuesSize = values.size();
167 
168  for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
169  {
170  if( ! addMultivaluedParameter( name, values[ valuesIdx ] ) )
171  {
172  return false;
173  }
174  }
175 
176  return true;
177  }
178 
179  /*!
180  \brief Add sets of new multivalued parameters in a matrix form.
181  \param name A new non-empty parameter name.
182  \param values Parameter values.
183  \return true if ok, false on errors.
184  */
185  template< typename ValueType >
186  bool addMultivaluedParameters( const std::string& name,
187  const std::vector< boost::numeric::ublas::matrix< ValueType > >& values )
188  {
189  const std::size_t valuesSize = values.size();
190 
191  for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
192  {
193  if( ! addMultivaluedParameter( name, values[ valuesIdx ] ) )
194  {
195  return false;
196  }
197  }
198 
199  return true;
200  }
201 
202  /*!
203  \brief Returns all serialized parameters formatted for raster metadata.
204  \param metadata The output metadata.
205  */
206  void getMetaData( std::map<std::string, std::string>& metadata ) const;
207 
208  /*!
209  \brief Reset to the initial empty state.
210  */
211  void reset();
212 
213  protected :
214 
215  typedef std::pair< std::string, std::vector< std::string > > ParsContPairT;
216 
217  typedef std::multimap<std::string, std::vector< std::string > > ParsContT;
218 
219  template< typename ValueType >
220  std::string toString( const std::complex< ValueType >& value ) const
221  {
222  return std::to_string( value.real() ) + "+" +
223  std::to_string( value.imag() ) + "i";
224  }
225 
226  template< typename ValueType >
227  std::string toString( const ValueType& value ) const
228  {
229  return std::to_string( value );
230  }
231 
232  private :
233 
234  std::string m_algoName;
235 
237 
238  };
239  } // end namespace rp
240 } // end namespace te
241 
242 #endif // __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
243 
A class to standardize algorithm parameters serialization.
std::pair< std::string, std::vector< std::string > > ParsContPairT
bool addMultivaluedParameter(const std::string &name, const std::vector< std::string > &values)
Add a new multivalued parameter.
AlgorithmParametersSerializer(const AlgorithmParametersSerializer &other)
AlgorithmParametersSerializer & operator=(const AlgorithmParametersSerializer &other)
std::string toString(const ValueType &value) const
bool addMultivaluedParameter(const std::string &name, const boost::numeric::ublas::matrix< ValueType > &values)
Add a new multivalued parameter in a matrix form.
void getMetaData(std::map< std::string, std::string > &metadata) const
Returns all serialized parameters formatted for raster metadata.
bool addParameter(const std::string &name, const ValueType &value)
Add a new parameter.
std::string toString(const std::complex< ValueType > &value) const
bool addMultivaluedParameters(const std::string &name, const std::vector< boost::numeric::ublas::matrix< ValueType > > &values)
Add sets of new multivalued parameters in a matrix form.
std::multimap< std::string, std::vector< std::string > > ParsContT
void reset()
Reset to the initial empty state.
bool addParameter(const std::string &name, const std::string &value)
Add a new parameter.
bool addMultivaluedParameters(const std::string &name, const std::vector< std::vector< ValueType > > &values)
Add sets of new multivalued parameters.
bool addMultivaluedParameter(const std::string &name, const std::vector< ValueType > &values)
Add a new multivalued parameter.
void setAlgorithmName(const std::string &name)
Set a non-empty algorithm name.
TerraLib.
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
Proxy configuration file for TerraView (see terraview_config.h).