All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AttributeConverters.cpp
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 /*!
21  \file terralib/dataaccess/dataset/AttributeConverters.cpp
22 
23  \brief Definition of attribute converter and a set of them.
24 */
25 
26 // TerraLib
27 #include "../../datatype/DataConverterManager.h"
28 #include "../../datatype/SimpleData.h"
29 #include "../../geometry/Point.h"
30 #include "../../geometry/PointM.h"
31 #include "../../geometry/PointZ.h"
32 #include "../../geometry/PointZM.h"
33 #include "AttributeConverters.h"
34 #include "DataSet.h"
35 
36 // STL
37 #include <cassert>
38 #include <memory>
39 
40 te::dt::AbstractData* te::da::GenericAttributeConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int dstType)
41 {
42  assert(indexes.size() == 1);
43 
44  if(dataset->isNull(indexes[0]))
45  return 0;
46 
47  // Gets the data from input data set
48  std::auto_ptr<te::dt::AbstractData> data(dataset->getValue(indexes[0]));
49  assert(data.get());
50 
51  // Source and Destination Types
52  int srcType = data->getTypeCode();
53  if(srcType == dstType) // Need conversion?
54  return data.release();
55 
56  // Try get a data type converter
57  const te::dt::DataTypeConverter& converter = te::dt::DataConverterManager::getInstance().get(srcType, dstType);
58 
59  // Converts the original data
60  te::dt::AbstractData* convertedData = converter(data.get());
61  assert(convertedData);
62 
63  return convertedData;
64 }
65 
66 te::dt::AbstractData* te::da::XYToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
67 {
68  assert(dataset);
69  assert(indexes.size() == 2);
70 
71  double x = dataset->getDouble(indexes[0]);
72  double y = dataset->getDouble(indexes[1]);
73 
74  return new te::gm::Point(x, y);
75 }
76 
77 te::dt::AbstractData* te::da::XYZToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
78 {
79  assert(dataset);
80  assert(indexes.size() == 3);
81 
82  double x = dataset->getDouble(indexes[0]);
83  double y = dataset->getDouble(indexes[1]);
84  double z = dataset->getDouble(indexes[2]);
85 
86  return new te::gm::PointZ(x, y, z);
87 }
88 
89 te::dt::AbstractData* te::da::XYMToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
90 {
91  assert(dataset);
92  assert(indexes.size() == 3);
93 
94  double x = dataset->getDouble(indexes[0]);
95  double y = dataset->getDouble(indexes[1]);
96  double m = dataset->getDouble(indexes[2]);
97 
98  return new te::gm::PointM(x, y, m);
99 }
100 
101 te::dt::AbstractData* te::da::XYZMToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
102 {
103  assert(dataset);
104  assert(indexes.size() == 4);
105 
106  double x = dataset->getDouble(indexes[0]);
107  double y = dataset->getDouble(indexes[1]);
108  double z = dataset->getDouble(indexes[2]);
109  double m = dataset->getDouble(indexes[3]);
110 
111  return new te::gm::PointZM(x, y, z, m);
112 }
113 
114 te::dt::AbstractData* te::da::PointToXConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
115 {
116  assert(dataset);
117  assert(indexes.size() == 1);
118 
119  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
120 
121  return new te::dt::Double(pt->getX());
122 }
123 
124 te::dt::AbstractData* te::da::PointToYConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
125 {
126  assert(dataset);
127  assert(indexes.size() == 1);
128 
129  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
130 
131  return new te::dt::Double(pt->getY());
132 }
133 
134 te::dt::AbstractData* te::da::PointToZConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
135 {
136  assert(dataset);
137  assert(indexes.size() == 1);
138 
139  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
140 
141  return new te::dt::Double(pt->getZ());
142 }
143 
144 te::dt::AbstractData* te::da::PointToMConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
145 {
146  assert(dataset);
147  assert(indexes.size() == 1);
148 
149  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
150 
151  return new te::dt::Double(pt->getM());
152 }
153 
154 te::dt::AbstractData* te::da::TupleToStringConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
155 {
156  assert(dataset);
157 
158  std::string result = "[ ";
159 
160  std::vector<std::size_t>::const_iterator it;
161  for(it = indexes.begin(); it != indexes.end(); ++it)
162  result += dataset->getAsString(*it) + " ";
163 
164  result += "]";
165 
166  return new te::dt::String(result);
167 }
168 
169 te::dt::AbstractData* te::da::CharEncodingConverter::operator()(DataSet* dataset, const std::vector<std::size_t>& indexes, int dstType)
170 {
171  assert(dataset);
172  assert(indexes.size() == 1);
173 
174  std::size_t pos = indexes[0];
175 
176  assert(dataset->getPropertyDataType(pos) == te::dt::STRING_TYPE);
177  assert(dstType == te::dt::STRING_TYPE);
178 
179  std::string value = dataset->getString(pos);
180  te::common::CharEncoding fromCode = dataset->getPropertyCharEncoding(pos);
181 
182  if(fromCode == m_toCode)
183  return new te::dt::String(value);
184 
186  return new te::dt::String(value);
187 
188 #ifdef TERRALIB_CHARENCODING_ENABLED
189  try
190  {
191  std::string result = te::common::CharEncodingConv::convert(value, fromCode, m_toCode);
192  return new te::dt::String(result);
193  }
194  catch(...)
195  {
196  return new te::dt::String(value);
197  }
198 #else
199  return new te::dt::String(value);
200 #endif
201 }
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
TEDATAACCESSEXPORT te::dt::AbstractData * XYMToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
CharEncoding
Supported charsets (character encoding).
TEDATAACCESSEXPORT te::dt::AbstractData * PointToMConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
TEDATAACCESSEXPORT te::dt::AbstractData * GenericAttributeConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual int getTypeCode() const =0
It returns the data type code associated to the data value.
static std::string convert(const std::string &src, const CharEncoding &fromCode, const CharEncoding &toCode)
An static method that converts the source string to a target charset.
TEDATAACCESSEXPORT te::dt::AbstractData * PointToXConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
A point with a z-coordinate value and an associated measurement.
Definition: PointZM.h:51
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
A point with an associated measure.
Definition: PointM.h:51
A point with z-coordinate value.
Definition: PointZ.h:51
static DataConverterManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
virtual te::common::CharEncoding getPropertyCharEncoding(std::size_t i) const =0
It returns the property character encoding at position pos.
TEDATAACCESSEXPORT te::dt::AbstractData * PointToZConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
Definition: DataSet.cpp:218
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
TEDATAACCESSEXPORT te::dt::AbstractData * XYToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
Definition of attribute converter and a set of them.
te::dt::AbstractData * operator()(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
TEDATAACCESSEXPORT te::dt::AbstractData * XYZToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
te::common::CharEncoding m_toCode
boost::function1< AbstractData *, AbstractData * > DataTypeConverter
The definition of the data type converter.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
TEDATAACCESSEXPORT te::dt::AbstractData * PointToYConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
TEDATAACCESSEXPORT te::dt::AbstractData * XYZMToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
Definition: DataSet.cpp:151
TEDATAACCESSEXPORT te::dt::AbstractData * TupleToStringConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)