Loading...
Searching...
No Matches
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
32namespace 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.
125 \param name A new non-empty parameter name.
126 \param value A new parameter values.
127 \return true if ok, false on errors.
128 */
129 template< typename ValueType, typename KeyType >
130 bool addMultivaluedParameter( const std::string& name,
131 const std::map< KeyType, ValueType >& values )
132 {
133 const std::size_t valuesSize = values.size();
134 std::vector< std::string > stringValues;
135
136 try
137 {
138 typename std::map< KeyType, ValueType >::const_iterator it =
139 values.begin();
140 const typename std::map< KeyType, ValueType >::const_iterator itE =
141 values.end();
142
143 while( it != itE )
144 {
145 stringValues.push_back( toString( it->first ) );
146 stringValues.push_back( toString( it->second ) );
147 ++it;
148 }
149 }
150 catch(...)
151 {
152 return false;
153 }
154
155 return addMultivaluedParameter( name, stringValues );
156 }
157
158 /*!
159 \brief Add a new multivalued parameter in a matrix form.
160 \param name A new non-empty parameter name.
161 \param value A new parameter values in a matrix form.
162 \return true if ok, false on errors.
163 */
164 template< typename ValueType >
165 bool addMultivaluedParameter( const std::string& name,
166 const boost::numeric::ublas::matrix< ValueType >& values )
167 {
168 const std::size_t valuesSize1 = values.size1();
169 const std::size_t valuesSize2 = values.size2();
170 std::size_t values2Idx = 0;
171 std::vector< std::string > stringValues;
172
173 try
174 {
175 for( std::size_t values1Idx = 0 ; values1Idx < valuesSize1 ; ++values1Idx )
176 {
177 for( values2Idx = 0 ; values2Idx < valuesSize2 ; ++values2Idx )
178 {
179 stringValues.push_back( toString( values( values1Idx, values2Idx ) ) );
180 }
181 }
182 }
183 catch(...)
184 {
185 return false;
186 }
187
188 return addMultivaluedParameter( name, stringValues );
189 }
190
191 /*!
192 \brief Add sets of new multivalued parameters.
193 \param name A new non-empty parameter name.
194 \param values Parameter values.
195 \return true if ok, false on errors.
196 */
197 template< typename ValueType >
198 bool addMultivaluedParameters( const std::string& name,
199 const std::vector< std::vector< ValueType > >& values )
200 {
201 const std::size_t valuesSize = values.size();
202
203 for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
204 {
205 if( ! addMultivaluedParameter( name, values[ valuesIdx ] ) )
206 {
207 return false;
208 }
209 }
210
211 return true;
212 }
213
214 /*!
215 \brief Add sets of new multivalued parameters in a matrix form.
216 \param name A new non-empty parameter name.
217 \param values Parameter values.
218 \return true if ok, false on errors.
219 */
220 template< typename ValueType >
221 bool addMultivaluedParameters( const std::string& name,
222 const std::vector< boost::numeric::ublas::matrix< ValueType > >& values )
223 {
224 const std::size_t valuesSize = values.size();
225
226 for( std::size_t valuesIdx = 0 ; valuesIdx < valuesSize ; ++valuesIdx )
227 {
228 if( ! addMultivaluedParameter( name, values[ valuesIdx ] ) )
229 {
230 return false;
231 }
232 }
233
234 return true;
235 }
236
237 /*!
238 \brief Returns all serialized parameters formatted for raster metadata.
239 \param metadata The output metadata.
240 */
241 void getMetaData( std::map<std::string, std::string>& metadata ) const;
242
243 /*!
244 \brief Reset to the initial empty state.
245 */
246 void reset();
247
248 protected :
249
250 typedef std::pair< std::string, std::vector< std::string > > ParsContPairT;
251
252 typedef std::multimap<std::string, std::vector< std::string > > ParsContT;
253
254 template< typename ValueType >
255 std::string toString( const std::complex< ValueType >& value ) const
256 {
257 return std::to_string( value.real() ) + "+" +
258 std::to_string( value.imag() ) + "i";
259 }
260
261 std::string toString( const std::string& value ) const
262 {
263 return value;
264 }
265
266 template< typename ValueType >
267 std::string toString( const ValueType& value ) const
268 {
269 return std::to_string( value );
270 }
271
272 private :
273
274 std::string m_algoName;
275
277
278 };
279 } // end namespace rp
280} // end namespace te
281
282#endif // __TERRALIB_RP_INTERNAL_ALGORITHM_PARAMETERS_SERIALIZER_H
283
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.
std::string toString(const std::string &value) const
AlgorithmParametersSerializer(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 addMultivaluedParameter(const std::string &name, const std::map< KeyType, ValueType > &values)
Add a new multivalued parameter.
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.
AlgorithmParametersSerializer & operator=(const AlgorithmParametersSerializer &other)
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).