All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VectorDataSet.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/terralib4/VectorDataSet.cpp
22 
23  \brief Implementation of a dataset for the TerraLib 4 driver.
24 */
25 
26 // TerraLib
27 #include "../common/ByteSwapUtils.h"
28 #include "../common/Globals.h"
29 #include "../common/StringUtils.h"
30 #include "../common/Translator.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../datatype/Array.h"
33 #include "../datatype/ByteArray.h"
34 #include "../datatype/DateTimeProperty.h"
35 #include "../datatype/SimpleData.h"
36 #include "../datatype/TimeInstant.h"
37 #include "../geometry/Envelope.h"
38 #include "../geometry/Geometry.h"
39 #include "../geometry/GeometryProperty.h"
40 #include "../geometry/WKBReader.h"
41 #include "VectorDataSet.h"
42 #include "DataSource.h"
43 #include "Exception.h"
44 #include "GeomReader.h"
45 #include "Utils.h"
46 
47 // Terralib 4.x
48 #include <terralib4/kernel/TeAttribute.h>
49 #include <terralib4/kernel/TeDatabase.h>
50 #include <terralib4/kernel/TeLayer.h>
51 
52 #include <terralib4/kernel/TeQuerierParams.h>
53 
54 // STL
55 #include <cassert>
56 #include <memory>
57 
58 // Boost
59 #include <boost/dynamic_bitset.hpp>
60 #include <boost/lexical_cast.hpp>
61 
63  : m_dt(0),
64  m_querier(0),
65  m_i(-1),
66  m_size(-1),
67  m_nCols(-1),
68  m_geomCol(-1),
69  m_hasGeometry(false),
70  m_layer(layer)
71 {
72  TeQuerierParams params(true, true);
73  params.setParams(m_layer);
74 
75  m_querier = new TeQuerier(params);
76 
77  m_querier->loadInstances();
78 
79  m_nCols = m_querier->getAttrList().size();
80 
81  m_size = m_querier->numElemInstances();
82 
83  m_dt = new te::da::DataSetType(layer->name());
84 
85  for(int i = 0; i != m_nCols; ++i)
86  {
87  std::auto_ptr<te::dt::Property> p(terralib4::Convert2T5(m_querier->getAttrList()[i].rep_));
88 
89  m_dt->add(p.release());
90  }
91 
92  if((m_layer->hasGeometry(TePOLYGONS) ||
93  m_layer->hasGeometry(TeLINES) ||
94  m_layer->hasGeometry(TePOINTS) ||
95  m_layer->hasGeometry(TeNODES) ||
96  m_layer->hasGeometry(TeCELLS)))
97  {
98  te::gm::GeometryProperty* gp = new te::gm::GeometryProperty("spatial_data");
99 
100  m_dt->add(gp);
101 
102  m_geomCol = m_nCols;
103 
104  ++m_nCols;
105  }
106 
107 }
108 
110 {
111  delete m_querier;
112  delete m_dt;
113 }
114 
116 {
117  throw;
118 }
119 
121 {
122  throw;
123 }
124 
125 std::auto_ptr<te::gm::Envelope> terralib4::VectorDataSet::getExtent(std::size_t i)
126 {
127  return terralib4::Convert2T5(m_layer->box());
128 }
129 
131 {
132  return m_nCols;
133 }
134 
136 {
137  return m_dt->getProperty(i)->getType();
138 }
139 
140 std::string terralib4::VectorDataSet::getPropertyName(std::size_t i) const
141 {
142  /*TeAttributeList list = m_querier->getAttrList();
143  return list[i].rep_.name_();*/
144  throw;
145 }
146 
148 {
149  return te::common::UTF8;
150 }
151 
153 {
154  throw;
155 }
156 
158 {
159  return !(size() > 0);
160 }
161 
163 {
164  throw;
165 }
166 
168 {
169  return m_size;
170 }
171 
173 {
174  return m_querier->fetchInstance(m_instance);
175 }
176 
178 {
179  return false;
180 }
181 
183 {
184  return false;
185 }
186 
188 {
189  return false;
190 }
191 
193 {
194  return false;
195 }
196 
197 bool terralib4::VectorDataSet::move(std::size_t /*i*/)
198 {
199  return false;
200 }
201 
203 {
204  return false;
205 }
206 
208 {
209  return false;
210 }
211 
213 {
214  return false;
215 }
216 
218 {
219  return false;
220 }
221 
222 char terralib4::VectorDataSet::getChar(std::size_t i) const
223 {
224  throw;
225 }
226 
227 unsigned char terralib4::VectorDataSet::getUChar(std::size_t i) const
228 {
229  throw;
230 }
231 
232 boost::int16_t terralib4::VectorDataSet::getInt16(std::size_t i) const
233 {
234  std::string val;
235  int ii = static_cast<int>(i);
236  m_instance.getPropertyValue(val, ii);
237 
238  return boost::lexical_cast<boost::int16_t>(val);
239 }
240 
241 boost::int32_t terralib4::VectorDataSet::getInt32(std::size_t i) const
242 {
243  std::string val;
244  int ii = static_cast<int>(i);
245  m_instance.getPropertyValue(val, ii);
246 
247  return boost::lexical_cast<boost::int32_t>(val);
248 }
249 
250 boost::int64_t terralib4::VectorDataSet::getInt64(std::size_t i) const
251 {
252  std::string val;
253  int ii = static_cast<int>(i);
254  m_instance.getPropertyValue(val, ii);
255 
256  return boost::lexical_cast<boost::int64_t>(val);
257 }
258 
259 bool terralib4::VectorDataSet::getBool(std::size_t i) const
260 {
261  std::string val;
262  int ii = static_cast<int>(i);
263  m_instance.getPropertyValue(val, ii);
264 
265  return boost::lexical_cast<bool>(val);
266 }
267 
268 float terralib4::VectorDataSet::getFloat(std::size_t i) const
269 {
270  return (float)getDouble(i);
271 }
272 
273 double terralib4::VectorDataSet::getDouble(std::size_t i) const
274 {
275  std::string val;
276  int ii = static_cast<int>(i);
277  m_instance.getPropertyValue(val, ii);
278 
279  return boost::lexical_cast<double>(val);
280 }
281 
282 std::string terralib4::VectorDataSet::getNumeric(std::size_t i) const
283 {
284  std::string val;
285  int ii = static_cast<int>(i);
286 
287  m_instance.getPropertyValue(val, ii);
288 
289  return val;
290 }
291 
292 std::string terralib4::VectorDataSet::getString(std::size_t i) const
293 {
294  std::string val;
295  int ii = static_cast<int>(i);
296 
297  m_instance.getPropertyValue(val, ii);
298 
299  return val;
300 }
301 
302 std::auto_ptr<te::dt::ByteArray> terralib4::VectorDataSet::getByteArray(std::size_t i) const
303 {
304  /*unsigned char * data;
305  long size;
306  m_result->getBlob(m_result->getAttribute(i).rep_.name_, data, size);
307 
308  return std::auto_ptr<te::dt::ByteArray>(new te::dt::ByteArray((char*)data, size));*/
309  throw;
310 }
311 
312 std::auto_ptr<te::gm::Geometry> terralib4::VectorDataSet::getGeometry(std::size_t i) const
313 {
314  assert(i == m_geomCol);
315 
316  std::vector<TeGeometry*> geoms;
317  m_instance.getGeometry(geoms);
318 
319  std::auto_ptr<te::gm::Geometry> t5g(terralib4::GeomReader::getGeometry(*geoms[0]));
320 
321  int srid = m_layer->projection()->epsgCode();
322  if(srid == 4979)
323  srid = 4326;
324 
325  t5g->setSRID(srid);
326 
327  return t5g;
328 }
329 
330 std::auto_ptr<te::rst::Raster> terralib4::VectorDataSet::getRaster(std::size_t i) const
331 {
332  throw;
333 }
334 
335 std::auto_ptr<te::dt::DateTime> terralib4::VectorDataSet::getDateTime(std::size_t i) const
336 {
337  //TeTime time = m_result->getDate(i);
338  //time.
339  throw;
340 }
341 
342 std::auto_ptr<te::dt::Array> terralib4::VectorDataSet::getArray(std::size_t i) const
343 {
344  return std::auto_ptr<te::dt::Array>(0);
345 }
346 
347 bool terralib4::VectorDataSet::isNull(std::size_t i) const
348 {
349  if(i == m_geomCol)
350  return false;
351 
352  if(getPropertyDataType(i) == te::dt::STRING_TYPE)
353  return false;
354 
355  std::string val;
356 
357  int ii = static_cast<int>(i);
358 
359  m_instance.getPropertyValue(val, ii);
360 
361  if(val.empty())
362  return true;
363 
364  return false;
365 }
bool moveLast()
It sets the dataset internal pointer to the last item in the collection.
std::size_t size() const
It returns the collection size, if it is known.
Geometric property.
VectorDataSet(TeLayer *layer)
CharEncoding
Supported charsets (character encoding).
boost::int32_t getInt32(std::size_t i) const
Method for retrieving a 32-bit integer attribute value (4 bytes long).
A class that models the description of a dataset.
Definition: DataSetType.h:72
te::common::AccessPolicy getAccessPolicy() const
It returns the read and write permission associated to the dataset.
static std::auto_ptr< te::gm::Geometry > getGeometry(const TeGeometry &geom)
Definition: GeomReader.cpp:170
std::size_t getNumProperties() const
It returns the number of properties that composes an item of the dataset.
bool isBeforeBegin() const
It tells if the dataset internal pointer is in a position before the first element of the collection ...
double getDouble(const std::string &value, std::vector< std::string > &sVector)
Definition: Utils.cpp:179
std::string getNumeric(std::size_t i) const
Method for retrieving a numeric attribute value.
unsigned char getUChar(std::size_t i) const
Method for retrieving an unsigned character attribute value (1 byte long).
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
char getChar(std::size_t i) const
Method for retrieving a signed character attribute value (1 byte long).
int getPropertyDataType(std::size_t i) const
It returns the underlying data type of the property at position pos.
std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
Method for retrieving a byte array.
bool move(std::size_t i)
It moves the dataset internal pointer to a given position.
boost::int16_t getInt16(std::size_t i) const
Method for retrieving a 16-bit integer attribute value (2 bytes long).
std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const
Method for retrieving a raster attribute value.
std::auto_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
Definition: Utils.cpp:53
std::string getPropertyName(std::size_t i) const
It returns the property name at position pos.
std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
Method for retrieving a geometric attribute value.
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
double getDouble(std::size_t i) const
Method for retrieving a double attribute value.
te::common::CharEncoding getPropertyCharEncoding(std::size_t i) const
It returns the property character encoding at position pos.
std::auto_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset.
std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
Method for retrieving a date and time attribute value.
bool isAfterEnd() const
It tells if the dataset internal pointer is on the sentinel position after the last element of the co...
std::string getString(std::size_t i) const
Method for retrieving a string value attribute.
std::string getDatasetNameOfProperty(std::size_t i) const
It returns the underlying dataset name of the property at position pos.
te::common::TraverseType getTraverseType() const
It returns the traverse type associated to the dataset.
void add(Constraint *c)
It adds a new constraint.
boost::int64_t getInt64(std::size_t i) const
Method for retrieving a 64-bit integer attribute value (8 bytes long).
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
std::auto_ptr< te::dt::Array > getArray(std::size_t i) const
Method for retrieving an array.
bool isConnected() const
It returns true if the dataset is connected and false if it is disconnected. A dataset can be connect...
bool moveFirst()
It moves the internal pointer to the first item in the collection.
bool isEmpty() const
It returns true if the collection is empty.
bool getBool(std::size_t i) const
Method for retrieving a boolean attribute value.
bool isNull(std::size_t i) const
It checks if the attribute value is NULL.
float getFloat(std::size_t i) const
Method for retrieving a float attribute value.
bool isAtBegin() const
It tells if the dataset internal pointer is on the first element of the collection or not...
An utility class for converting a TerraLib 4.x geometry to a TerraLib 5.
bool moveNext()
It moves the internal pointer to the next item of the collection.
bool isAtEnd() const
It tells if the dataset internal pointer is on the last element of the collection.
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
bool movePrevious()
It moves the internal pointer to the previous item of the collection.
te::da::DataSetType * m_dt