All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/ogr/DataSet.cpp
22 
23  \brief Implementation of a DataSet for OGR data provider.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../dataaccess/dataset/DataSetType.h"
29 #include "../datatype/ByteArray.h"
30 #include "../datatype/DateTimeProperty.h"
31 #include "../datatype/StringProperty.h"
32 #include "../datatype/TimeInstant.h"
33 #include "../geometry/Envelope.h"
34 #include "../geometry/Geometry.h"
35 #include "../geometry/WKBReader.h"
36 #include "../srs/Config.h"
37 #include "DataSource.h"
38 #include "DataSet.h"
39 #include "Exception.h"
40 #include "Utils.h"
41 
42 // OGR
43 #include <ogrsf_frmts.h>
44 #include <ogr_core.h>
45 
46 // STL
47 #include <cassert>
48 #include <memory>
49 
50 te::ogr::DataSet::DataSet(OGRDataSource* dsrc, OGRLayer* layer) :
51 te::da::DataSet(),
52 m_dt(0),
53 m_ogrDs(dsrc),
54 m_layer(layer),
55 m_currentFeature(0),
56 m_i(-1),
57 m_wkbArray(0),
58 m_wkbArraySize(0),
59 m_srid(TE_UNKNOWN_SRS)
60 {
61  assert(layer);
62 
63  layer->ResetReading();
64 
65  m_dt = Convert2TerraLib(layer->GetLayerDefn());
66 
67  assert(m_dt);
68 
69  if(m_dt->hasGeom())
70  {
71  OGRSpatialReference* osrs = m_layer->GetSpatialRef();
72  if(osrs)
74  }
75 }
76 
78 {
79  OGRFeature::DestroyFeature(m_currentFeature);
80 
81  delete [] m_wkbArray;
82 
83  delete m_dt;
84 
85  m_ogrDs->ReleaseResultSet(m_layer);
86 
87  OGRDataSource::DestroyDataSource(m_ogrDs);
88 }
89 
91 {
92  return m_dt->size();
93 }
94 
95 int te::ogr::DataSet::getPropertyDataType(std::size_t pos) const
96 {
97  return m_dt->getProperty(pos)->getType();
98 }
99 
100 std::string te::ogr::DataSet::getPropertyName(std::size_t pos) const
101 {
102  return m_dt->getProperty(pos)->getName();
103 }
104 
106 {
107  te::dt::StringProperty* p = dynamic_cast<te::dt::StringProperty*>(m_dt->getProperty(i));
108  assert(p);
109 
110  return p->getCharEncoding();
111 }
112 
113 std::string te::ogr::DataSet::getDatasetNameOfProperty(std::size_t pos) const
114 {
115  return "";
116 }
117 
119 {
120  return !(size() > 0);
121 }
122 
123 std::auto_ptr<te::gm::Envelope> te::ogr::DataSet::getExtent(std::size_t /*i*/)
124 {
125  OGREnvelope psExtent;
126  m_layer->GetExtent(&psExtent);
127  te::gm::Envelope* env = Convert2TerraLib(&psExtent);
128  return std::auto_ptr<te::gm::Envelope>(env);
129 }
130 
131 std::size_t te::ogr::DataSet::size() const
132 {
133  return m_layer->GetFeatureCount();
134 }
135 
137 {
138  OGRFeature::DestroyFeature(m_currentFeature);
139 
140  m_currentFeature = m_layer->GetNextFeature();
141 
142  m_i++;
143  return m_currentFeature != 0;
144 }
145 
147 {
148  return move(m_i - 1);
149 }
150 
152 {
153  m_layer->ResetReading();
154  m_i = -1;
155  return true;
156 }
157 
159 {
160  m_layer->ResetReading();
161  m_i = -1;
162  return moveNext();
163 }
164 
166 {
167  int lastPos = m_layer->GetFeatureCount() - 1;
168  return move(lastPos);
169 }
170 
171 bool te::ogr::DataSet::move(std::size_t i)
172 {
173  if(m_i == i)
174  return true;
175 
176  int p = static_cast<int>(i);
177 
178  OGRErr error = m_layer->SetNextByIndex(p);
179 
180  m_i = p - 1;
181  if(error == OGRERR_NONE)
182  return moveNext();
183  else
184  return false;
185 }
186 
188 {
189  return m_i == 0;
190 }
191 
193 {
194  return m_i < 0;
195 }
196 
198 {
199  return m_i >= (int)size();
200 }
201 
203 {
204  return m_i > static_cast<int>(size());
205 }
206 
207 char te::ogr::DataSet::getChar(std::size_t /*i*/) const
208 {
209  throw te::common::Exception(TE_TR("OGR driver: getChar not supported."));
210 }
211 
212 unsigned char te::ogr::DataSet::getUChar(std::size_t /*i*/) const
213 {
214  throw te::common::Exception(TE_TR("OGR driver: getUChar not supported."));
215 }
216 
217 boost::int16_t te::ogr::DataSet::getInt16(std::size_t /*i*/) const
218 {
219  throw te::common::Exception(TE_TR("OGR driver: getInt16 not supported."));
220 }
221 
222 boost::int32_t te::ogr::DataSet::getInt32(std::size_t i) const
223 {
224  return m_currentFeature->GetFieldAsInteger(i);
225 }
226 
227 boost::int64_t te::ogr::DataSet::getInt64(std::size_t /*i*/) const
228 {
229  throw te::common::Exception(TE_TR("OGR driver: getInt64 not supported."));
230 }
231 
232 bool te::ogr::DataSet::getBool(std::size_t /*i*/) const
233 {
234  throw te::common::Exception(TE_TR("OGR driver: getBool not supported."));
235 }
236 
237 float te::ogr::DataSet::getFloat(std::size_t /*i*/) const
238 {
239  throw te::common::Exception(TE_TR("OGR driver: getFloat not supported."));
240 }
241 
242 double te::ogr::DataSet::getDouble(std::size_t i) const
243 {
244  return m_currentFeature->GetFieldAsDouble(i);
245 }
246 
247 std::string te::ogr::DataSet::getNumeric(std::size_t i) const
248 {
249  return m_currentFeature->GetFieldAsString(i);
250 }
251 
252 std::string te::ogr::DataSet::getString(std::size_t i) const
253 {
254  return m_currentFeature->GetFieldAsString(i);
255 }
256 
257 std::auto_ptr<te::dt::ByteArray> te::ogr::DataSet::getByteArray(std::size_t i) const
258 {
259  int size = 0;
260  GByte* bytes = m_currentFeature->GetFieldAsBinary(i, &size);
261 
262  te::dt::ByteArray* byteArray = new te::dt::ByteArray(size);
263  byteArray->copy((char*)bytes, size);
264 
265  return std::auto_ptr<te::dt::ByteArray>(byteArray);
266 }
267 
268 std::auto_ptr<te::gm::Geometry> te::ogr::DataSet::getGeometry(std::size_t /*i*/) const
269 {
270  char* wkb = (char*)getWKB();
271 
273  geom->setSRID(m_srid);
274 
275  return std::auto_ptr<te::gm::Geometry>(geom);
276 }
277 
278 std::auto_ptr<te::rst::Raster> te::ogr::DataSet::getRaster(std::size_t /*i*/) const
279 {
280  throw te::common::Exception(TE_TR("OGR driver: getRaster not supported."));
281 }
282 
283 std::auto_ptr<te::dt::DateTime> te::ogr::DataSet::getDateTime(std::size_t i) const
284 {
285  if(m_dt == 0)
286  return std::auto_ptr<te::dt::DateTime>(0);
287 
288  int pnYear,
289  pnMonth,
290  pnDay,
291  pnHour,
292  pnMinute,
293  pnSecond,
294  pnTZFlag;
295 
296  if(m_currentFeature->GetFieldAsDateTime(i, &pnYear, &pnMonth, &pnDay, &pnHour, &pnMinute, &pnSecond, &pnTZFlag) == FALSE)
297  return std::auto_ptr<te::dt::DateTime>(new te::dt::Date);
298 
299  te::dt::Property* p = m_dt->getProperty(i);
300  te::dt::DateTimeType subType = static_cast<te::dt::DateTimeProperty*>(p)->getSubType();
301  te::dt::DateTime* dateTime = 0;
302 
303  if(subType == te::dt::DATE)
304  {
305  dateTime = new te::dt::Date((unsigned short)pnYear, (unsigned short)pnMonth, (unsigned short)pnDay);
306  }
307  else if(subType == te::dt::TIME_DURATION)
308  {
309  dateTime = new te::dt::TimeDuration(pnHour, pnMinute, pnSecond);
310  }
311  else if(subType == te::dt::TIME_INSTANT)
312  {
313  te::dt::Date d(pnYear, pnMonth, pnDay);
314  te::dt::TimeDuration td(pnHour, pnMinute, pnSecond);
315  dateTime = new te::dt::TimeInstant(d, td);
316  }
317 
318  return std::auto_ptr<te::dt::DateTime>(dateTime);
319 }
320 
321 std::auto_ptr<te::dt::Array> te::ogr::DataSet::getArray(std::size_t /*i*/) const
322 {
323  return std::auto_ptr<te::dt::Array>(0); // Not supported by OGR library
324 }
325 
326 bool te::ogr::DataSet::isNull(std::size_t i) const
327 {
328  if(m_currentFeature->IsFieldSet(i) == 0)
329  return true;
330 
331  return false;
332 }
333 
334 const unsigned char* te::ogr::DataSet::getWKB() const
335 {
336  // The OGR library supports only one geometry field
337  OGRGeometry* geom = m_currentFeature->GetGeometryRef()->clone();
338 
339  if(geom == 0)
340  return 0;
341 
342  if(geom->getGeometryType() == wkbPolygon)
343  geom = OGRGeometryFactory::forceToMultiPolygon(geom);
344  else if(geom->getGeometryType() == wkbLineString)
345  geom = OGRGeometryFactory::forceToMultiLineString(geom);
346  else if(geom->getGeometryType() == wkbPoint)
347  geom = OGRGeometryFactory::forceToMultiPoint(geom);
348 
349  int wkbSize = geom->WkbSize();
350 
351  if(wkbSize > m_wkbArraySize)
352  {
353  m_wkbArraySize = wkbSize;
354  delete [] m_wkbArray;
355  m_wkbArray = new unsigned char[m_wkbArraySize];
356  }
357 
358  geom->exportToWkb(wkbNDR, m_wkbArray);
359 
360 // 2.5D geometries have a special code!
361  if(geom->getGeometryType() & 0x80000000)
362  {
363  unsigned int newcode = 0x0FFFFFFF & geom->getGeometryType();
364  newcode += 1000;
365  memcpy(m_wkbArray + 1, &newcode, sizeof(unsigned int));
366  }
367 
368  OGRGeometryFactory::destroyGeometry(geom);
369 
370  return (const unsigned char*)m_wkbArray;
371 }
An exception class for the OGR module.
bool isEmpty() const
It returns true if the collection is empty.
Definition: DataSet.cpp:118
std::size_t getNumProperties() const
It returns the number of properties that composes an item of the dataset.
Definition: DataSet.cpp:90
te::da::DataSetType * m_dt
DataSetType.
Definition: DataSet.h:156
std::string getDatasetNameOfProperty(std::size_t pos) const
It returns the underlying dataset name of the property at position pos.
Definition: DataSet.cpp:113
float getFloat(std::size_t i) const
Method for retrieving a float attribute value.
Definition: DataSet.cpp:237
te::common::CharEncoding getCharEncoding() const
It returns the string property character encoding.
CharEncoding
Supported charsets (character encoding).
std::string getPropertyName(std::size_t pos) const
It returns the property name at position pos.
Definition: DataSet.cpp:100
A class for data providers of OGR.
bool isBeforeBegin() const
It tells if the dataset internal pointer is in a position before the first element of the collection ...
Definition: DataSet.cpp:192
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
DataSet()
Default constructor.
Definition: DataSet.h:117
int m_srid
The SRS id associated to this dataset.
Definition: DataSet.h:166
unsigned char getUChar(std::size_t i) const
Method for retrieving an unsigned character attribute value (1 byte long).
Definition: DataSet.cpp:212
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
boost::int64_t getInt64(std::size_t i) const
Method for retrieving a 64-bit integer attribute value (8 bytes long).
Definition: DataSet.cpp:227
te::common::CharEncoding getPropertyCharEncoding(std::size_t i) const
It returns the property character encoding at position pos.
Definition: DataSet.cpp:105
bool isAfterEnd() const
It tells if the dataset internal pointer is on the sentinel position after the last element of the co...
Definition: DataSet.cpp:202
Implementation of a DataSet for OGR data provider.
Definition: DataSet.h:59
std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const
Method for retrieving a geometric attribute value.
Definition: DataSet.cpp:268
It models a property definition.
Definition: Property.h:59
A class to represent time instant.
Definition: TimeInstant.h:55
bool isAtEnd() const
It tells if the dataset internal pointer is on the last element of the collection.
Definition: DataSet.cpp:197
~DataSet()
Destructor.
Definition: DataSet.cpp:77
std::auto_ptr< te::dt::ByteArray > getByteArray(std::size_t i) const
Method for retrieving a byte array.
Definition: DataSet.cpp:257
std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const
Method for retrieving a raster attribute value.
Definition: DataSet.cpp:278
bool getBool(std::size_t i) const
Method for retrieving a boolean attribute value.
Definition: DataSet.cpp:232
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
double getDouble(std::size_t i) const
Method for retrieving a double attribute value.
Definition: DataSet.cpp:242
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
A base class for date data types.
Definition: Date.h:53
bool move(std::size_t i)
It moves the dataset internal pointer to a given position.
Definition: DataSet.cpp:171
std::string getNumeric(std::size_t i) const
Method for retrieving a numeric attribute value.
Definition: DataSet.cpp:247
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
Definition: Utils.cpp:55
boost::int16_t getInt16(std::size_t i) const
Method for retrieving a 16-bit integer attribute value (2 bytes long).
Definition: DataSet.cpp:217
The type for string types: FIXED_STRING, VAR_STRING or STRING.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
bool moveNext()
It moves the internal pointer to the next item of the collection.
Definition: DataSet.cpp:136
Utility functions for OGR support.
boost::int32_t getInt32(std::size_t i) const
Method for retrieving a 32-bit integer attribute value (4 bytes long).
Definition: DataSet.cpp:222
bool isNull(std::size_t i) const
It checks if the attribute value is NULL.
Definition: DataSet.cpp:326
const unsigned char * getWKB() const
Definition: DataSet.cpp:334
std::string getString(std::size_t i) const
Method for retrieving a string value attribute.
Definition: DataSet.cpp:252
A class to represent time duration with nano-second/micro-second resolution.
Definition: TimeDuration.h:51
OGRLayer * m_layer
Definition: DataSet.h:159
The type for date and time types: date, date period, date duration, time duration, time instant, time period, time instant with time zone or time period with time zone.
std::auto_ptr< te::gm::Envelope > getExtent(std::size_t i)
It computes the bounding rectangle for a spatial property of the dataset.
Definition: DataSet.cpp:123
Implementation of a DataSet for OGR data provider.
bool moveBeforeFirst()
It moves the internal pointer to a position before the first item in the collection.
Definition: DataSet.cpp:151
char getChar(std::size_t i) const
Method for retrieving a signed character attribute value (1 byte long).
Definition: DataSet.cpp:207
DateTimeType
The subtype of date and time type, based on ISO 8621.
Definition: Enums.h:45
int getPropertyDataType(std::size_t pos) const
It returns the underlying data type of the property at position pos.
Definition: DataSet.cpp:95
bool isAtBegin() const
It tells if the dataset internal pointer is on the first element of the collection or not...
Definition: DataSet.cpp:187
std::size_t size() const
It returns the collection size, if it is known.
Definition: DataSet.cpp:131
void copy(char *data, std::size_t size)
It copies the data from the given pointer to the byte array.
Definition: ByteArray.cpp:128
bool movePrevious()
It moves the internal pointer to the previous item of the collection.
Definition: DataSet.cpp:146
bool moveLast()
It sets the dataset internal pointer to the last item in the collection.
Definition: DataSet.cpp:165
std::auto_ptr< te::dt::Array > getArray(std::size_t i) const
Method for retrieving an array.
Definition: DataSet.cpp:321
virtual void setSRID(int srid)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
TEOGREXPORT int Convert2TerraLibProjection(OGRSpatialReference *osrs)
It converts the OGR Projection to TerraLib Projection.
Definition: Utils.cpp:126
A class for representing binary data.
Definition: ByteArray.h:51
static Geometry * read(const char *wkb)
It returns a valid geometry from a given WKB.
Definition: WKBReader.cpp:50
bool moveFirst()
It moves the internal pointer to the first item in the collection.
Definition: DataSet.cpp:158
std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const
Method for retrieving a date and time attribute value.
Definition: DataSet.cpp:283