dataaccess/dataset/DataSet.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/DataSet.cpp
22 
23  \brief A dataset is the unit of information manipulated by the data access module of TerraLib.
24 */
25 
26 // TerraLib
27 #include "../../core/utils/HexUtils.h"
28 #include "../../core/translator/Translator.h"
29 #include "../../common/StringUtils.h"
30 #include "../../datatype/AbstractData.h"
31 #include "../../datatype/Array.h"
32 #include "../../datatype/ByteArray.h"
33 #include "../../datatype/DateTime.h"
34 #include "../../datatype/Property.h"
35 #include "../../datatype/SimpleData.h"
36 #include "../../raster/Raster.h"
37 #include "../utils/Utils.h"
38 #include "DataSet.h"
39 #include "DataSetType.h"
40 
41 // STL
42 #include <memory>
43 
44 // Boost
45 #include <boost/lexical_cast.hpp>
46 
47 char te::da::DataSet::getChar(const std::string& name) const
48 {
49  std::size_t i = GetPropertyPos(this, name);
50 
51  return getChar(i);
52 }
53 
54 unsigned char te::da::DataSet::getUChar(const std::string& name) const
55 {
56  std::size_t i = GetPropertyPos(this, name);
57 
58  return getUChar(i);
59 }
60 
61 boost::int16_t te::da::DataSet::getInt16(const std::string& name) const
62 {
63  std::size_t i = GetPropertyPos(this, name);
64 
65  return getInt16(i);
66 }
67 
68 boost::int32_t te::da::DataSet::getInt32(const std::string& name) const
69 {
70  std::size_t i = GetPropertyPos(this, name);
71 
72  return getInt32(i);
73 }
74 
75 boost::int64_t te::da::DataSet::getInt64(const std::string& name) const
76 {
77  std::size_t i = GetPropertyPos(this, name);
78 
79  return getInt64(i);
80 }
81 
82 bool te::da::DataSet::getBool(const std::string& name) const
83 {
84  std::size_t i = GetPropertyPos(this, name);
85 
86  return getBool(i);
87 }
88 
89 float te::da::DataSet::getFloat(const std::string& name) const
90 {
91  std::size_t i = GetPropertyPos(this, name);
92 
93  return getFloat(i);
94 }
95 
96 double te::da::DataSet::getDouble(const std::string& name) const
97 {
98  std::size_t i = GetPropertyPos(this, name);
99 
100  return getDouble(i);
101 }
102 
103 std::string te::da::DataSet::getNumeric(const std::string& name) const
104 {
105  std::size_t i = GetPropertyPos(this, name);
106 
107  return getNumeric(i);
108 }
109 
110 std::string te::da::DataSet::getString(const std::string& name) const
111 {
112  std::size_t i = GetPropertyPos(this, name);
113 
114  return getString(i);
115 }
116 
117 std::unique_ptr<te::dt::ByteArray> te::da::DataSet::getByteArray(const std::string& name) const
118 {
119  std::size_t i = GetPropertyPos(this, name);
120 
121  return getByteArray(i);
122 }
123 
124 std::unique_ptr<te::gm::Geometry> te::da::DataSet::getGeometry(const std::string& name) const
125 {
126  std::size_t i = GetPropertyPos(this, name);
127 
128  return getGeometry(i);
129 }
130 
131 std::unique_ptr<te::rst::Raster> te::da::DataSet::getRaster(const std::string& name) const
132 {
133  std::size_t i = GetPropertyPos(this, name);
134 
135  return getRaster(i);
136 }
137 
138 std::unique_ptr<te::dt::DateTime> te::da::DataSet::getDateTime(const std::string& name) const
139 {
140  std::size_t i = GetPropertyPos(this, name);
141 
142  return getDateTime(i);
143 }
144 
145 std::unique_ptr<te::dt::Array> te::da::DataSet::getArray(const std::string& name) const
146 {
147  std::size_t i = GetPropertyPos(this, name);
148 
149  return getArray(i);
150 }
151 
152 std::unique_ptr<te::dt::AbstractData> te::da::DataSet::getValue(std::size_t i) const
153 {
154  switch(getPropertyDataType(i))
155  {
156  case te::dt::CHAR_TYPE:
157  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Char(getChar(i)));
158 
159  case te::dt::UCHAR_TYPE:
160  return std::unique_ptr<te::dt::AbstractData>(new te::dt::UChar(getUChar(i)));
161 
162  case te::dt::INT16_TYPE:
163  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Int16(getInt16(i)));
164 
165  case te::dt::UINT16_TYPE:
166  return std::unique_ptr<te::dt::AbstractData>(new te::dt::UInt16(getInt16(i)));
167 
168  case te::dt::INT32_TYPE:
169  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Int32(getInt32(i)));
170 
171  case te::dt::UINT32_TYPE:
172  return std::unique_ptr<te::dt::AbstractData>(new te::dt::UInt32(getInt32(i)));
173 
174  case te::dt::INT64_TYPE:
175  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Int64(getInt64(i)));
176 
177  case te::dt::UINT64_TYPE:
178  return std::unique_ptr<te::dt::AbstractData>(new te::dt::UInt64(getInt64(i)));
179 
181  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Boolean(getBool(i)));
182 
183  case te::dt::FLOAT_TYPE:
184  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Float(getFloat(i)));
185 
186  case te::dt::DOUBLE_TYPE:
187  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Double(getDouble(i)));
188 
190  return std::unique_ptr<te::dt::AbstractData>(new te::dt::Numeric(getNumeric(i)));
191 
192  case te::dt::STRING_TYPE:
193  return std::unique_ptr<te::dt::AbstractData>(new te::dt::String(getString(i)));
194 
196  return std::unique_ptr<te::dt::AbstractData>(getByteArray(i));
197 
199  return std::unique_ptr<te::dt::AbstractData>(getGeometry(i));
200 
202  return std::unique_ptr<te::dt::AbstractData>(getDateTime(i));
203 
204  case te::dt::RASTER_TYPE:
205  return std::unique_ptr<te::dt::AbstractData>(getRaster(i));
206 
207  default:
208  return std::unique_ptr<te::dt::AbstractData>();
209  }
210 }
211 
212 std::unique_ptr<te::dt::AbstractData> te::da::DataSet::getValue(const std::string& name) const
213 {
214  std::size_t i = GetPropertyPos(this, name);
215 
216  return getValue(i);
217 }
218 
219 std::string te::da::DataSet::getAsString(std::size_t i, int precision) const
220 {
221  std::string value;
222 
223  switch(getPropertyDataType(i))
224  {
225  case te::dt::CHAR_TYPE:
226  value = getChar(i);
227  break;
228 
229  case te::dt::UCHAR_TYPE:
230  value = getUChar(i);
231  break;
232 
233  case te::dt::INT16_TYPE:
234  value = boost::lexical_cast<std::string>(getInt16(i));
235  break;
236 
237  case te::dt::INT32_TYPE:
238  value = boost::lexical_cast<std::string>(getInt32(i));
239  break;
240 
241  case te::dt::INT64_TYPE:
242  value = boost::lexical_cast<std::string>(getInt64(i));
243  break;
244 
246  value = boost::lexical_cast<std::string>(getBool(i));
247  break;
248 
249  case te::dt::FLOAT_TYPE:
250  value = boost::lexical_cast<std::string>(getFloat(i));
251  break;
252 
253  case te::dt::DOUBLE_TYPE:
254  value = te::common::Convert2String(getDouble(i), precision);
255  break;
256 
258  value = getNumeric(i);
259  break;
260 
261  case te::dt::STRING_TYPE:
262  value = getString(i);
263  break;
264 
266  {
267  std::unique_ptr<te::dt::AbstractData> b(getByteArray(i));
268  value = b->toString();
269  }
270  break;
271 
273  {
274  std::unique_ptr<te::dt::AbstractData> g(getGeometry(i));
275  value = g->toString();
276  }
277  break;
278 
280  {
281  std::unique_ptr<te::dt::AbstractData> dTime(getDateTime(i));
282  value = dTime->toString();
283  }
284  break;
285 
286  case te::dt::ARRAY_TYPE:
287  {
288  std::unique_ptr<te::dt::Array> a(getArray(i));
289  value = a->toString();
290  }
291  break;
292 
293  case te::dt::RASTER_TYPE:
294  {
295  std::unique_ptr<te::dt::AbstractData> r(getRaster(i));
296  value = r->toString();
297  }
298  break;
299 
300  default:
301  break;
302  }
303 
304  return value;
305 }
306 
307 std::string te::da::DataSet::getAsString(const std::string& name, int precision) const
308 {
309  std::size_t numProperties = getNumProperties();
310 
311  std::size_t i = GetPropertyPos(this, name);
312 
313  if(i > numProperties)
314  throw te::common::Exception(TE_TR("Property not found."));
315 
316  return getAsString(i, precision);
317 }
318 
319 bool te::da::DataSet::isNull(const std::string& name) const
320 {
321  std::size_t i = GetPropertyPos(this, name);
322 
323  return isNull(i);
324 }
325 
virtual std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
virtual unsigned char getUChar(std::size_t i) const =0
Method for retrieving an unsigned character attribute value (1 byte long).
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.
virtual std::unique_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const =0
Method for retrieving a byte array.
SimpleData< unsigned char, UCHAR_TYPE > UChar
Definition: SimpleData.h:218
virtual std::unique_ptr< te::rst::Raster > getRaster(std::size_t i) const =0
Method for retrieving a raster attribute value.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
SimpleData< boost::int64_t, INT64_TYPE > Int64
Definition: SimpleData.h:223
SimpleData< boost::int32_t, INT32_TYPE > Int32
Definition: SimpleData.h:221
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
virtual std::unique_ptr< te::dt::Array > getArray(std::size_t i) const =0
Method for retrieving an array.
SimpleData< float, FLOAT_TYPE > Float
Definition: SimpleData.h:226
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
SimpleData< std::string, NUMERIC_TYPE > Numeric
Definition: SimpleData.h:228
int b
Definition: TsRtree.cpp:32
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
SimpleData< boost::uint64_t, UINT64_TYPE > UInt64
Definition: SimpleData.h:224
virtual std::string getAsString(std::size_t i, int precision=0) const
Method for retrieving a data value as a string plain representation.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
virtual std::unique_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
virtual char getChar(std::size_t i) const =0
Method for retrieving a signed character attribute value (1 byte long).
SimpleData< bool, BOOLEAN_TYPE > Boolean
Definition: SimpleData.h:225
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.
A class that models the description of a dataset.
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
SimpleData< char, CHAR_TYPE > Char
Definition: SimpleData.h:217
SimpleData< boost::uint16_t, UINT16_TYPE > UInt16
Definition: SimpleData.h:220
virtual bool getBool(std::size_t i) const =0
Method for retrieving a boolean attribute value.
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
SimpleData< boost::int16_t, INT16_TYPE > Int16
Definition: SimpleData.h:219
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
SimpleData< boost::uint32_t, UINT32_TYPE > UInt32
Definition: SimpleData.h:222