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 "../../core/encoding/CharEncoding.h"
28 #include "../../datatype/DataConverterManager.h"
29 #include "../../datatype/SimpleData.h"
30 #include "../../geometry/Point.h"
31 #include "AttributeConverters.h"
32 #include "DataSet.h"
33 
34 // STL
35 #include <cassert>
36 #include <memory>
37 
38 te::dt::AbstractData* te::da::GenericAttributeConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int dstType)
39 {
40  assert(indexes.size() == 1);
41 
42  if(dataset->isNull(indexes[0]))
43  return nullptr;
44 
45  // Gets the data from input data set
46  std::unique_ptr<te::dt::AbstractData> data(dataset->getValue(indexes[0]));
47  if(!data.get())
48  return nullptr;
49 
50  // Source and Destination Types
51  int srcType = data->getTypeCode();
52  if(srcType == dstType) // Need conversion?
53  return data.release();
54 
55  // Try get a data type converter
56  const te::dt::DataTypeConverter& converter = te::dt::DataConverterManager::getInstance().get(srcType, dstType);
57 
58  // Converts the original data
59  te::dt::AbstractData* convertedData = converter(data.get());
60  assert(convertedData);
61 
62  return convertedData;
63 }
64 
65 te::dt::AbstractData* te::da::XYToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
66 {
67  assert(dataset);
68  assert(indexes.size() == 2);
69 
70  double x = dataset->getDouble(indexes[0]);
71  double y = dataset->getDouble(indexes[1]);
72 
73  te::gm::Point* p = new te::gm::Point();
74  p->setX(x);
75  p->setY(y);
76 
77  return p;
78 }
79 
80 te::dt::AbstractData* te::da::XYZToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
81 {
82  assert(dataset);
83  assert(indexes.size() == 3);
84 
85  double x = dataset->getDouble(indexes[0]);
86  double y = dataset->getDouble(indexes[1]);
87  double z = dataset->getDouble(indexes[2]);
88 
90  p->setZ(z);
91 
92  return p;
93 }
94 
95 te::dt::AbstractData* te::da::XYMToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
96 {
97  assert(dataset);
98  assert(indexes.size() == 3);
99 
100  double x = dataset->getDouble(indexes[0]);
101  double y = dataset->getDouble(indexes[1]);
102  double m = dataset->getDouble(indexes[2]);
103 
105  p->setM(m);
106 
107  return p;
108 }
109 
110 te::dt::AbstractData* te::da::XYZMToPointConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
111 {
112  assert(dataset);
113  assert(indexes.size() == 4);
114 
115  double x = dataset->getDouble(indexes[0]);
116  double y = dataset->getDouble(indexes[1]);
117  double z = dataset->getDouble(indexes[2]);
118  double m = dataset->getDouble(indexes[3]);
119 
121  p->setZ(z);
122  p->setM(m);
123 
124  return p;
125 }
126 
127 te::dt::AbstractData* te::da::PointToXConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
128 {
129  assert(dataset);
130  assert(indexes.size() == 1);
131 
132  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
133 
134  return new te::dt::Double(pt->getX());
135 }
136 
137 te::dt::AbstractData* te::da::PointToYConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
138 {
139  assert(dataset);
140  assert(indexes.size() == 1);
141 
142  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
143 
144  return new te::dt::Double(pt->getY());
145 }
146 
147 te::dt::AbstractData* te::da::PointToZConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
148 {
149  assert(dataset);
150  assert(indexes.size() == 1);
151 
152  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
153 
154  return new te::dt::Double(pt->getZ());
155 }
156 
157 te::dt::AbstractData* te::da::PointToMConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
158 {
159  assert(dataset);
160  assert(indexes.size() == 1);
161 
162  te::gm::Point* pt = static_cast<te::gm::Point*>(dataset->getGeometry(indexes[0]).get());
163 
164  return new te::dt::Double(pt->getM());
165 }
166 
167 te::dt::AbstractData* te::da::TupleToStringConverter(DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
168 {
169  assert(dataset);
170 
171  std::string result = "[ ";
172 
173  std::vector<std::size_t>::const_iterator it;
174  for(it = indexes.begin(); it != indexes.end(); ++it)
175  result += dataset->getAsString(*it) + " ";
176 
177  result += "]";
178 
179  return new te::dt::String(result);
180 }
181 
182 te::dt::AbstractData* te::da::CharEncodingConverter::operator()(DataSet* dataset, const std::vector<std::size_t>& indexes, int dstType)
183 {
184  assert(dataset);
185  assert(indexes.size() == 1);
186 
187  std::size_t pos = indexes[0];
188 
189  assert(dataset->getPropertyDataType(pos) == te::dt::STRING_TYPE);
190  assert(dstType == te::dt::STRING_TYPE);
191 
192  if (dataset->isNull(pos))
193  return nullptr;
194 
195  std::string value = dataset->getString(pos);
196 
198  return new te::dt::String(value);
199 
200  try
201  {
202  std::string result = te::core::CharEncoding::fromUTF8(value, m_toCode);
203  return new te::dt::String(result);
204  }
205  catch(...)
206  {
207  return new te::dt::String(value);
208  }
209 }
210 
212  DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/)
213 {
214  assert(indexes.size() == 1);
215 
216  if (dataset->isNull(indexes[0]))
217  return nullptr;
218 
219  // Gets the data from input data set
220  std::unique_ptr<te::gm::Geometry> geom(dataset->getGeometry(indexes[0]));
221  assert(geom.get());
222 
223  //set input srid
224  if (m_inputSRID != TE_UNKNOWN_SRS)
225  geom->setSRID(m_inputSRID);
226 
227  //convert if necessary
228  if (m_outputSRID != TE_UNKNOWN_SRS && m_inputSRID != TE_UNKNOWN_SRS && m_inputSRID != m_outputSRID)
229  geom->transform(m_outputSRID);
230 
231  return geom.release();
232 }
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
te::core::EncodingType m_toCode
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
TEDATAACCESSEXPORT te::dt::AbstractData * XYMToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
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)
TEDATAACCESSEXPORT te::dt::AbstractData * PointToXConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
static DataConverterManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
void setM(const double &m)
It sets the Point m-coordinate value.
Definition: Point.h:187
TEDATAACCESSEXPORT te::dt::AbstractData * PointToZConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
te::gm::Polygon * p
te::dt::AbstractData * operator()(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.
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 int getTypeCode() const =0
It returns the data type code associated to the data value.
A dataset is the unit of information manipulated by the data access module of TerraLib.
void setX(const double &x)
It sets the Point x-coordinate value.
Definition: Point.h:145
TEDATAACCESSEXPORT te::dt::AbstractData * XYZToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
boost::function1< AbstractData *, AbstractData * > DataTypeConverter
The definition of the data type converter.
void setY(const double &y)
It sets the Point y-coordinate value.
Definition: Point.h:159
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.
TEDATAACCESSEXPORT te::dt::AbstractData * XYZMToPointConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
void setZ(const double &z)
It sets the Point z-coordinate value.
Definition: Point.h:173
TEDATAACCESSEXPORT te::dt::AbstractData * TupleToStringConverter(DataSet *dataset, const std::vector< std::size_t > &indexes, int dstType)