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