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 "../core/translator/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/MultiLineString.h"
41 #include "../geometry/MultiPoint.h"
42 #include "../geometry/MultiPolygon.h"
43 #include "../geometry/WKBReader.h"
44 #include "VectorDataSet.h"
45 #include "DataSource.h"
46 #include "Exception.h"
47 #include "GeomReader.h"
48 #include "Utils.h"
49 
50 // Terralib 4.x
51 #include <terralib4/kernel/TeAttribute.h>
52 #include <terralib4/kernel/TeDatabase.h>
53 #include <terralib4/kernel/TeLayer.h>
54 
55 #include <terralib4/kernel/TeQuerierParams.h>
56 
57 // STL
58 #include <cassert>
59 #include <memory>
60 
61 // Boost
62 #include <boost/dynamic_bitset.hpp>
63 #include <boost/lexical_cast.hpp>
64 
66  : m_dt(0),
67  m_querier(0),
68  m_i(-1),
69  m_size(-1),
70  m_nCols(-1),
71  m_geomCol(-1),
72  m_hasGeometry(false),
73  m_layer(layer)
74 {
75  TeQuerierParams params(true, true);
76  params.setParams(m_layer);
77 
78  m_querier = new TeQuerier(params);
79 
80  m_querier->loadInstances();
81 
82  m_nCols = (int)m_querier->getAttrList().size();
83 
84  m_size = m_querier->numElemInstances();
85 
86  m_dt = new te::da::DataSetType(layer->name());
87 
88  for(int i = 0; i != m_nCols; ++i)
89  {
90  std::unique_ptr<te::dt::Property> p(terralib4::Convert2T5(m_querier->getAttrList()[i].rep_));
91 
92  m_dt->add(p.release());
93  }
94 
95  if((m_layer->hasGeometry(TePOLYGONS) ||
96  m_layer->hasGeometry(TeLINES) ||
97  m_layer->hasGeometry(TePOINTS) ||
98  m_layer->hasGeometry(TeNODES) ||
99  m_layer->hasGeometry(TeCELLS)))
100  {
101  te::gm::GeometryProperty* gp = new te::gm::GeometryProperty("spatial_data");
102 
103  m_dt->add(gp);
104 
105  m_geomCol = m_nCols;
106 
107  ++m_nCols;
108  }
109 
110 }
111 
113 {
114  delete m_querier;
115  delete m_dt;
116 }
117 
119 {
120  throw;
121 }
122 
124 {
125  throw;
126 }
127 
128 std::unique_ptr<te::gm::Envelope> terralib4::VectorDataSet::getExtent(std::size_t)
129 {
130  return terralib4::Convert2T5(m_layer->box());
131 }
132 
134 {
135  return m_nCols;
136 }
137 
139 {
140  return m_dt->getProperty(i)->getType();
141 }
142 
143 std::string terralib4::VectorDataSet::getPropertyName(std::size_t) const
144 {
145  /*TeAttributeList list = m_querier->getAttrList();
146  return list[i].rep_.name_();*/
147  throw;
148 }
149 
151 {
152  throw;
153 }
154 
156 {
157  return !(size() > 0);
158 }
159 
161 {
162  throw;
163 }
164 
166 {
167  return m_size;
168 }
169 
171 {
172  return m_querier->fetchInstance(m_instance);
173 }
174 
176 {
177  return false;
178 }
179 
181 {
182  return false;
183 }
184 
186 {
187  return false;
188 }
189 
191 {
192  return false;
193 }
194 
196 {
197  return false;
198 }
199 
201 {
202  return false;
203 }
204 
206 {
207  return false;
208 }
209 
211 {
212  return false;
213 }
214 
216 {
217  return false;
218 }
219 
221 {
222  return false;
223 }
224 
225 char terralib4::VectorDataSet::getChar(std::size_t) const
226 {
227  throw;
228 }
229 
230 unsigned char terralib4::VectorDataSet::getUChar(std::size_t) const
231 {
232  throw;
233 }
234 
235 boost::int16_t terralib4::VectorDataSet::getInt16(std::size_t i) const
236 {
237  std::string val;
238  int ii = static_cast<int>(i);
239  m_instance.getPropertyValue(val, ii);
240 
241  return boost::lexical_cast<boost::int16_t>(val);
242 }
243 
244 boost::int32_t terralib4::VectorDataSet::getInt32(std::size_t i) const
245 {
246  std::string val;
247  int ii = static_cast<int>(i);
248  m_instance.getPropertyValue(val, ii);
249 
250  return boost::lexical_cast<boost::int32_t>(val);
251 }
252 
253 boost::int64_t terralib4::VectorDataSet::getInt64(std::size_t i) const
254 {
255  std::string val;
256  int ii = static_cast<int>(i);
257  m_instance.getPropertyValue(val, ii);
258 
259  return boost::lexical_cast<boost::int64_t>(val);
260 }
261 
262 bool terralib4::VectorDataSet::getBool(std::size_t i) const
263 {
264  std::string val;
265  int ii = static_cast<int>(i);
266  m_instance.getPropertyValue(val, ii);
267 
268  return boost::lexical_cast<bool>(val);
269 }
270 
271 float terralib4::VectorDataSet::getFloat(std::size_t i) const
272 {
273  return (float)getDouble(i);
274 }
275 
276 double terralib4::VectorDataSet::getDouble(std::size_t i) const
277 {
278  std::string val;
279  int ii = static_cast<int>(i);
280  m_instance.getPropertyValue(val, ii);
281 
283 
284  return boost::lexical_cast<double>(val);
285 }
286 
287 std::string terralib4::VectorDataSet::getNumeric(std::size_t i) const
288 {
289  std::string val;
290  int ii = static_cast<int>(i);
291 
292  m_instance.getPropertyValue(val, ii);
293 
295 
296  return val;
297 }
298 
299 std::string terralib4::VectorDataSet::getString(std::size_t i) const
300 {
301  std::string val;
302  int ii = static_cast<int>(i);
303 
304  m_instance.getPropertyValue(val, ii);
305 
306  return terralib4::Convert2Utf8(val);
307 }
308 
309 std::unique_ptr<te::dt::ByteArray> terralib4::VectorDataSet::getByteArray(std::size_t) const
310 {
311  /*unsigned char * data;
312  long size;
313  m_result->getBlob(m_result->getAttribute(i).rep_.name_, data, size);
314 
315  return std::unique_ptr<te::dt::ByteArray>(new te::dt::ByteArray((char*)data, size));*/
316  throw;
317 }
318 
319 std::unique_ptr<te::gm::Geometry> terralib4::VectorDataSet::getGeometry(std::size_t i) const
320 {
321  assert(i == m_geomCol);
322 
323  std::vector<TeGeometry*> geoms;
324  m_instance.getGeometry(geoms);
325 
326  int srid = m_layer->projection()->epsgCode();
327  if (srid == 4979)
328  srid = 4326;
329 
331 
332  std::unique_ptr<te::gm::Geometry> result;
333 
334  if (gType == te::gm::PolygonType || gType == te::gm::MultiPolygonType)
335  {
336  result.reset(new te::gm::MultiPolygon(0, te::gm::MultiPolygonType, srid));
337  }
338  else if (gType == te::gm::LineStringType || gType == te::gm::MultiLineStringType)
339  {
340  result.reset(new te::gm::MultiLineString(0, te::gm::MultiLineStringType, srid));
341  }
342  else if (gType == te::gm::PointType || gType == te::gm::MultiPointType)
343  {
344  result.reset(new te::gm::MultiPoint(0, te::gm::MultiPointType, srid));
345  }
346  else if (gType == te::gm::GeometryCollectionType)
347  {
349  }
350 
351 
352  for (std::size_t i = 0; i < geoms.size(); ++i)
353  {
354  te::gm::GeometryCollection* aux = dynamic_cast<te::gm::GeometryCollection*>(result.get());
355 
356  std::unique_ptr<te::gm::Geometry> t5g(terralib4::GeomReader::getGeometry(*geoms[i]));
357 
358  aux->add(t5g.release());
359  }
360 
361  result->setSRID(srid);
362 
363  return result;
364 }
365 
366 std::unique_ptr<te::rst::Raster> terralib4::VectorDataSet::getRaster(std::size_t) const
367 {
368  throw;
369 }
370 
371 std::unique_ptr<te::dt::DateTime> terralib4::VectorDataSet::getDateTime(std::size_t) const
372 {
373  //TeTime time = m_result->getDate(i);
374  //time.
375  throw;
376 }
377 
378 std::unique_ptr<te::dt::Array> terralib4::VectorDataSet::getArray(std::size_t) const
379 {
380  return std::unique_ptr<te::dt::Array>();
381 }
382 
383 bool terralib4::VectorDataSet::isNull(std::size_t i) const
384 {
385  if(i == m_geomCol)
386  return false;
387 
389  return false;
390 
391  std::string val;
392 
393  int ii = static_cast<int>(i);
394 
395  m_instance.getPropertyValue(val, ii);
396 
397  if(val.empty())
398  return true;
399 
400  return false;
401 }
Property * getProperty(std::size_t i) const
It returns the i-th property.
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
bool moveLast()
It sets the dataset internal pointer to the last item in the collection.
std::unique_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
Method for retrieving a byte array.
std::size_t size() const
It returns the collection size, if it is known.
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
VectorDataSet(TeLayer *layer)
TETERRALIB4EXPORT std::string Convert2Utf8(const std::string &str)
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.
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 ...
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).
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.
bool move(std::size_t i)
It moves the dataset internal pointer to a given position.
std::unique_ptr< te::rst::Raster > getRaster(std::size_t i) const
Method for retrieving a raster attribute value.
boost::int16_t getInt16(std::size_t i) const
Method for retrieving a 16-bit integer attribute value (2 bytes long).
std::unique_ptr< te::dt::Property > Convert2T5(const TeAttributeRep &attRep)
It creates a valid TerraLib 5 property given a valid TerraLib 4.x attribute representation.
std::unique_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
Method for retrieving a date and time attribute value.
std::unique_ptr< te::dt::Array > getArray(std::size_t i) const
Method for retrieving an array.
std::string getPropertyName(std::size_t i) const
It returns the property name at position pos.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
double getDouble(std::size_t i) const
Method for retrieving a double attribute value.
static std::unique_ptr< te::gm::Geometry > getGeometry(const TeGeometry &geom)
Definition: GeomReader.cpp:170
Implements the DataSource class for the TerraLib 4.x Data Access Driver.
std::unique_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset.
te::gm::GeomType GetMinimalRepresentation(const std::vector< TeGeometry * > &geoms)
std::unique_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
Method for retrieving a geometric attribute value.
te::gm::Polygon * p
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.
void CheckDecimalSeparator(std::string &value)
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.
int getType() const
It returns the property data type.
Definition: Property.h:161
void add(Constraint *c)
It adds a new constraint.
MultiLineString is a MultiCurve whose elements are LineStrings.
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.
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.
void add(Geometry *g)
It adds the geometry into 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.
Utilitary functions for dealing with TerraLib 5 and 4.x conversion.
float getFloat(std::size_t i) const
Method for retrieving a float attribute value.
bool isPositionValid() const
It tells if the dataset internal pointer is on a valid position.
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.
It is a collection of other geometric objects.
bool isAtEnd() const
It tells if the dataset internal pointer is on the last element of the collection.
bool movePrevious()
It moves the internal pointer to the previous item of the collection.
te::da::DataSetType * m_dt