All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetItem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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/memory/DataSetItem.cpp
22 
23  \brief An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../dataaccess/dataset/DataSetType.h"
29 #include "../dataaccess/utils/Utils.h"
30 #include "../datatype/AbstractData.h"
31 #include "../datatype/ByteArray.h"
32 #include "../datatype/DateTime.h"
33 #include "../datatype/Property.h"
34 #include "../datatype/SimpleData.h"
35 #include "../geometry/Geometry.h"
36 #include "../raster/Grid.h"
37 #include "../raster/RasterProperty.h"
38 
39 #include "DataSet.h"
40 #include "DataSetItem.h"
41 #include "Exception.h"
42 #include "Raster.h"
43 
44 // STL
45 #include <cassert>
46 
48  : m_parent(parent)
49 {
50  if(parent == 0)
51  return;
52 
53  std::size_t nproperties = parent->getNumProperties();
54 
55  m_data.resize(nproperties, 0);
56 }
57 
59  : m_parent(rhs.m_parent)
60 {
61  m_data.reserve(rhs.getNumProperties());
62 
63  for(std::size_t i = 0; i < rhs.getNumProperties(); ++i)
64  {
65  if(rhs.isNull(i) == false)
66  m_data.push_back(rhs.getValue(i));
67  else
68  m_data.push_back(0);
69  }
70 }
71 
73 {
74 }
75 
77 {
78  assert(getNumProperties() == rhs.getNumProperties());
79 
80  if(this != &rhs)
81  {
82  m_parent = rhs.m_parent;
83  m_data = rhs.m_data;
84  }
85 
86  return *this;
87 }
88 
89 std::auto_ptr<te::mem::DataSetItem> te::mem::DataSetItem::clone() const
90 {
91  return std::auto_ptr<te::mem::DataSetItem>(new DataSetItem(*this));
92 }
93 
95 {
96  return const_cast<te::da::DataSet*>(m_parent);
97 }
98 
100 {
101  return m_parent->getNumProperties();
102 }
103 
105 {
106  return m_parent->getPropertyDataType(pos);
107 }
108 
109 std::string te::mem::DataSetItem::getPropertyName(std::size_t pos) const
110 {
111  return m_parent->getPropertyName(pos);
112 }
113 
114 char te::mem::DataSetItem::getChar(std::size_t i) const
115 {
116  return static_cast<const te::dt::Char*>(&m_data[i])->getValue();
117 }
118 
119 void te::mem::DataSetItem::setChar(std::size_t i, char value)
120 {
121  m_data.replace(i, new te::dt::Char(value));
122 }
123 
124 void te::mem::DataSetItem::setChar(const std::string& name, char value)
125 {
126  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
127  setChar(pos, value);
128 }
129 
130 unsigned char te::mem::DataSetItem::getUChar(std::size_t i) const
131 {
132  return static_cast<const te::dt::UChar*>(&m_data[i])->getValue();
133 }
134 
135 void te::mem::DataSetItem::setUChar(std::size_t i, unsigned char value)
136 {
137  m_data.replace(i, new te::dt::UChar(value));
138 }
139 
140 void te::mem::DataSetItem::setUChar(const std::string& name, unsigned char value)
141 {
142  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
143  setUChar(pos, value);
144 }
145 
146 boost::int16_t te::mem::DataSetItem::getInt16(std::size_t i) const
147 {
148  return static_cast<const te::dt::Int16*>(&m_data[i])->getValue();
149 }
150 
151 void te::mem::DataSetItem::setInt16(std::size_t i, boost::int16_t value)
152 {
153  m_data.replace(i, new te::dt::Int16(value));
154 }
155 
156 void te::mem::DataSetItem::setInt16(const std::string& name, boost::int16_t value)
157 {
158  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
159  setInt16(pos, value);
160 }
161 
162 boost::int32_t te::mem::DataSetItem::getInt32(std::size_t i) const
163 {
164  return static_cast<const te::dt::Int32*>(&m_data[i])->getValue();
165 }
166 
167 void te::mem::DataSetItem::setInt32(std::size_t i, boost::int32_t value)
168 {
169  m_data.replace(i, new te::dt::Int32(value));
170 }
171 
172 void te::mem::DataSetItem::setInt32(const std::string& name, boost::int32_t value)
173 {
174  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
175  setInt32(pos, value);
176 }
177 
178 boost::int64_t te::mem::DataSetItem::getInt64(std::size_t i) const
179 {
180  return static_cast<const te::dt::Int64*>(&m_data[i])->getValue();
181 }
182 
183 void te::mem::DataSetItem::setInt64(std::size_t i, boost::int64_t value)
184 {
185  m_data.replace(i, new te::dt::Int64(value));
186 }
187 
188 void te::mem::DataSetItem::setInt64(const std::string& name, boost::int64_t value)
189 {
190  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
191  setInt64(pos, value);
192 }
193 
194 bool te::mem::DataSetItem::getBool(std::size_t i) const
195 {
196  return static_cast<const te::dt::Boolean*>(&m_data[i])->getValue();
197 }
198 
199 void te::mem::DataSetItem::setBool(std::size_t i, bool value)
200 {
201  m_data.replace(i, new te::dt::Boolean(value));
202 }
203 
204 void te::mem::DataSetItem::setBool(const std::string& name, bool value)
205 {
206  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
207  setBool(pos, value);
208 }
209 
210 float te::mem::DataSetItem::getFloat(std::size_t i) const
211 {
212  return static_cast<const te::dt::Float*>(&m_data[i])->getValue();
213 }
214 
215 void te::mem::DataSetItem::setFloat(std::size_t i, float value)
216 {
217  m_data.replace(i, new te::dt::Float(value));
218 }
219 
220 void te::mem::DataSetItem::setFloat(const std::string& name, float value)
221 {
222  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
223  setFloat(pos, value);
224 }
225 
226 double te::mem::DataSetItem::getDouble(std::size_t i) const
227 {
228  return static_cast<const te::dt::Double*>(&m_data[i])->getValue();
229 }
230 
231 void te::mem::DataSetItem::setDouble(std::size_t i, double value)
232 {
233  m_data.replace(i, new te::dt::Double(value));
234 }
235 
236 void te::mem::DataSetItem::setDouble(const std::string& name, double value)
237 {
238  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
239  setDouble(pos, value);
240 }
241 
242 std::string te::mem::DataSetItem::getNumeric(std::size_t i) const
243 {
244  return static_cast<const te::dt::Numeric*>(&m_data[i])->getValue();
245 }
246 
247 void te::mem::DataSetItem::setNumeric(std::size_t i, const std::string& value)
248 {
249  m_data.replace(i, new te::dt::Numeric(value));
250 }
251 
252 void te::mem::DataSetItem::setNumeric(const std::string& name, const std::string& value)
253 {
254  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
255  setNumeric(pos, value);
256 }
257 
258 std::string te::mem::DataSetItem::getString(std::size_t i) const
259 {
260  return static_cast<const te::dt::String*>(&m_data[i])->getValue();
261 }
262 
263 void te::mem::DataSetItem::setString(std::size_t i, const std::string& value)
264 {
265  m_data.replace(i, new te::dt::String(value));
266 }
267 
268 void te::mem::DataSetItem::setString(const std::string& name, const std::string& value)
269 {
270  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
271  setString(pos, value);
272 }
273 
274 std::auto_ptr<te::dt::ByteArray> te::mem::DataSetItem::getByteArray(std::size_t i) const
275 {
276  te::dt::ByteArray* b = new te::dt::ByteArray(*static_cast<const te::dt::ByteArray*>(&m_data[i]));
277  return std::auto_ptr<te::dt::ByteArray>(b);
278 }
279 
281 {
282  m_data.replace(i, value);
283 }
284 
285 void te::mem::DataSetItem::setByteArray(const std::string& name, te::dt::ByteArray* value)
286 {
287  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
288  setByteArray(pos, value);
289 }
290 
291 std::auto_ptr<te::gm::Geometry> te::mem::DataSetItem::getGeometry(std::size_t i) const
292 {
293  return std::auto_ptr<te::gm::Geometry>(static_cast<te::gm::Geometry*>(m_data[i].clone()));
294 }
295 
297 {
298  m_data.replace(i, value);
299 }
300 
301 void te::mem::DataSetItem::setGeometry(const std::string& name, te::gm::Geometry* value)
302 {
303  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
304  setGeometry(pos, value);
305 }
306 
307 std::auto_ptr<te::rst::Raster> te::mem::DataSetItem::getRaster(std::size_t i) const
308 {
309  return std::auto_ptr<te::rst::Raster>(static_cast<te::rst::Raster*>(m_data[i].clone()));
310 }
311 
313 {
314  m_data.replace(i, value);
315 }
316 
317 void te::mem::DataSetItem::setRaster(const std::string& name, te::rst::Raster* value)
318 {
319  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
320  setRaster(pos, value);
321 }
322 
323 std::auto_ptr<te::dt::DateTime> te::mem::DataSetItem::getDateTime(std::size_t i) const
324 {
325  return std::auto_ptr<te::dt::DateTime>(static_cast<te::dt::DateTime*>(m_data[i].clone()));
326 }
327 
329 {
330  m_data.replace(i, value);
331 }
332 
333 void te::mem::DataSetItem::setDateTime(const std::string& name, te::dt::DateTime* value)
334 {
335  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
336  setDateTime(pos, value);
337 }
338 
339 std::auto_ptr<te::dt::AbstractData> te::mem::DataSetItem::getValue(std::size_t i) const
340 {
341  switch(getPropertyDataType(i))
342  {
343  case te::dt::CHAR_TYPE:
344  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Char(getChar(i)));
345 
346  case te::dt::UCHAR_TYPE:
347  return std::auto_ptr<te::dt::AbstractData>(new te::dt::UChar(getUChar(i)));
348 
349  case te::dt::INT16_TYPE:
350  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Int16(getInt16(i)));
351 
352  case te::dt::UINT16_TYPE:
353  return std::auto_ptr<te::dt::AbstractData>(new te::dt::UInt16(getInt16(i)));
354 
355  case te::dt::INT32_TYPE:
356  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Int32(getInt32(i)));
357 
358  case te::dt::UINT32_TYPE:
359  return std::auto_ptr<te::dt::AbstractData>(new te::dt::UInt32(getInt32(i)));
360 
361  case te::dt::INT64_TYPE:
362  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Int64(getInt64(i)));
363 
364  case te::dt::UINT64_TYPE:
365  return std::auto_ptr<te::dt::AbstractData>(new te::dt::UInt64(getInt64(i)));
366 
368  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Boolean(getBool(i)));
369 
370  case te::dt::FLOAT_TYPE:
371  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Float(getFloat(i)));
372 
373  case te::dt::DOUBLE_TYPE:
374  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Double(getDouble(i)));
375 
377  return std::auto_ptr<te::dt::AbstractData>(new te::dt::Numeric(getNumeric(i)));
378 
379  case te::dt::STRING_TYPE:
380  return std::auto_ptr<te::dt::AbstractData>(new te::dt::String(getString(i)));
381 
383  return std::auto_ptr<te::dt::AbstractData>(getByteArray(i).release());
384 
386  return std::auto_ptr<te::dt::AbstractData>(getGeometry(i).release());
387 
389  return std::auto_ptr<te::dt::AbstractData>(getDateTime(i).release());
390 
391  case te::dt::RASTER_TYPE:
392  return std::auto_ptr<te::dt::AbstractData>(getRaster(i).release());
393 
394  default:
395  return std::auto_ptr<te::dt::AbstractData>(0);
396  }
397 }
398 
400 {
401  m_data.replace(i, value);
402 }
403 
404 void te::mem::DataSetItem::setValue(const std::string& name, te::dt::AbstractData* value)
405 {
406  std::size_t pos = te::da::GetPropertyPos(m_parent, name);
407  setValue(pos, value);
408 }
409 
410 bool te::mem::DataSetItem::isNull(std::size_t i) const
411 {
412  return boost::is_null(m_data.begin() + i);
413 }
bool getBool(std::size_t i) const
It returns the value of the i-th property.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
boost::int64_t getInt64(std::size_t i) const
It returns the value of the i-th property.
te::da::DataSet * getParent() const
It returns its parent.
Definition: DataSetItem.cpp:94
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
std::string getPropertyName(std::size_t pos) const
It returns the name of the pos-th property.
SimpleData< char, CHAR_TYPE > Char
Definition: SimpleData.h:217
void setBool(const std::string &name, bool value)
It sets the value of the property, indicating its name.
std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
It returns the value of the i-th property.
A raster class for memory.
SimpleData< boost::int64_t, INT64_TYPE > Int64
Definition: SimpleData.h:223
SimpleData< boost::uint32_t, UINT32_TYPE > UInt32
Definition: SimpleData.h:222
std::auto_ptr< DataSetItem > clone() const
It returns a clone of the DataSetItem.
Definition: DataSetItem.cpp:89
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
An exception class for the TerraLib In-Memory Data Access driver.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
void setUChar(std::size_t i, unsigned char value)
It sets the value of the i-th property.
void setInt64(std::size_t i, boost::int64_t value)
It sets the value of the i-th property.
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
boost::ptr_vector< boost::nullable< te::dt::AbstractData > > m_data
The data values of the dataset item.
Definition: DataSetItem.h:367
std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
It returns the value of the i-th property.
void setFloat(std::size_t i, float value)
It sets the value of the i-th property.
std::size_t getNumProperties() const
It returns the number of properties.
Definition: DataSetItem.cpp:99
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
~DataSetItem()
Destructor.
Definition: DataSetItem.cpp:72
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
DataSetItem & operator=(const DataSetItem &rhs)
Assignment operator.
Definition: DataSetItem.cpp:76
std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
It returns the value of the i-th property.
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
SimpleData< boost::int16_t, INT16_TYPE > Int16
Definition: SimpleData.h:219
void setNumeric(std::size_t i, const std::string &value)
It sets the value of the i-th property.
SimpleData< float, FLOAT_TYPE > Float
Definition: SimpleData.h:226
void setDateTime(std::size_t i, te::dt::DateTime *value)
It sets the value of the i-th property.
SimpleData< unsigned char, UCHAR_TYPE > UChar
Definition: SimpleData.h:218
void setRaster(std::size_t i, te::rst::Raster *value)
It sets the value of the i-th property.
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
boost::int16_t getInt16(std::size_t i) const
It returns the value of the i-th property.
char getChar(std::size_t i) const
It returns the value of the i-th property.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:447
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
bool isNull(std::size_t i) const
int getPropertyDataType(std::size_t pos) const
It returns the type of the pos-th property.
void setInt16(std::size_t i, boost::int16_t value)
It sets the value of the i-th property.
boost::int32_t getInt32(std::size_t i) const
It returns the value of the i-th property.
void setBool(std::size_t i, bool value)
It sets the value of the i-th property.
void setChar(std::size_t i, char value)
It sets the value of the i-th property.
SimpleData< boost::int32_t, INT32_TYPE > Int32
Definition: SimpleData.h:221
double getDouble(const std::string &value, std::vector< std::string > &sVector)
Definition: Utils.cpp:54
An abstract class for raster data strucutures.
Definition: Raster.h:70
float getFloat(std::size_t i) const
It returns the value of the i-th property.
const te::da::DataSet * m_parent
The parent dataset, if the item is associated to one.
Definition: DataSetItem.h:366
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
SimpleData< bool, BOOLEAN_TYPE > Boolean
Definition: SimpleData.h:225
std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const
It returns the value of the i-th property.
SimpleData< std::string, NUMERIC_TYPE > Numeric
Definition: SimpleData.h:228
SimpleData< boost::uint64_t, UINT64_TYPE > UInt64
Definition: SimpleData.h:224
A class for representing binary data.
Definition: ByteArray.h:51
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
SimpleData< boost::uint16_t, UINT16_TYPE > UInt16
Definition: SimpleData.h:220
DataSetItem(const te::da::DataSet *parent)
It creates a new item having the same schema as the parent dataset.
Definition: DataSetItem.cpp:47
std::string getString(std::size_t i) const
It returns the value of the i-th property.
std::auto_ptr< te::dt::AbstractData > getValue(std::size_t i) const
It returns the value of the i-th property.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111
std::string getNumeric(std::size_t i) const
It returns the value of the i-th property.
double getDouble(std::size_t i) const
It returns the value of the i-th property.
void setByteArray(std::size_t i, te::dt::ByteArray *value)
It sets the value of the i-th property.
unsigned char getUChar(std::size_t i) const
It returns the value of the i-th property.