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