Orfeo Toolbox  4.0
otbOGRLayerWrapper.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
19 /*===========================================================================*/
20 /*===============================[ Includes ]================================*/
21 /*===========================================================================*/
22 #include "otbOGRLayerWrapper.h"
23 
24 #include <cassert>
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
27 #include "ogrsf_frmts.h" // OGRDataSource & OGRLayer
28 
30 
31 /*===========================================================================*/
32 /*======================[ Construction & Destruction ]=======================*/
33 /*===========================================================================*/
34 namespace { // Anonymous namespace
40  struct LeaveAloneDeleter
41  {
42  void operator()(OGRLayer*) const {}
43  };
44 } // Anonymous namespace
45 
46 
47 otb::ogr::Layer::Layer(OGRLayer* layer, bool modifiable)
48 : m_Layer(layer, LeaveAloneDeleter())
49  , m_Modifiable(modifiable)
50 #if 0
51  , m_DataSource(datasource)
52 #endif
53 {
54 }
55 
56 otb::ogr::Layer::Layer(OGRLayer* layer, OGRDataSource& sourceInChargeOfLifeTime, bool modifiable)
57 : m_Layer(layer, boost::bind(&OGRDataSource::ReleaseResultSet, boost::ref(sourceInChargeOfLifeTime), _1))
58  , m_Modifiable(modifiable)
59 {
60  assert(layer && "A null OGRlayer cannot belong to an OGRDataSource" );
61  // OGR always refuses "delete 0". *sigh*
62 }
63 
64 /*===========================================================================*/
65 /*===============================[ Features ]================================*/
66 /*===========================================================================*/
67 int otb::ogr::Layer::GetFeatureCount(bool doForceComputation) const
68 {
69  assert(m_Layer && "Can't ask the features count on an invalid layer");
70  return m_Layer->GetFeatureCount(doForceComputation);
71 }
72 
74 {
75  assert(m_Layer && "OGRLayer not initialized");
76  OGRFeature * f = m_Layer->GetNextFeature();
77  return f;
78 }
79 
81 {
82  assert(m_Layer && "OGRLayer not initialized");
83  m_Layer->ResetReading();
84  return iterator(*this);
85 }
86 
88 {
89  assert(m_Layer && "OGRLayer not initialized");
90  m_Layer->ResetReading();
91  return const_iterator(*const_cast <Layer*>(this));
92 }
93 
95 {
96  assert(m_Layer && "OGRLayer not initialized");
97  m_Layer->SetNextByIndex(index);
98  return iterator(*this);
99 }
100 
102 {
103  assert(m_Layer && "OGRLayer not initialized");
104  m_Layer->SetNextByIndex(index);
105  return const_iterator(*const_cast <Layer*>(this));
106 }
107 
109 {
110  assert(m_Layer && "OGRLayer not initialized");
111 
112  if (!m_Modifiable)
113  {
114  itkGenericExceptionMacro(<< "Cannot create a new feature in the layer <"
115  <<GetName()<<">: layer is not modifiable");
116  }
117 
118  const OGRErr res = m_Layer->CreateFeature(&feature.ogr());
119  if (res != OGRERR_NONE)
120  {
121  itkGenericExceptionMacro(<< "Cannot create a new feature in the layer <"
122  <<GetName()<<">: " << CPLGetLastErrorMsg());
123  }
124 }
125 
127 {
128  assert(m_Layer && "OGRLayer not initialized");
129 
130  if (!m_Modifiable)
131  {
132  itkGenericExceptionMacro(<< "Cannot create a new feature in the layer <"
133  <<GetName()<<">: layer is not modifiable");
134  }
135 
136  const OGRErr res = m_Layer->DeleteFeature(nFID);
137  if (res != OGRERR_NONE)
138  {
139  itkGenericExceptionMacro(<< "Cannot delete the feature <"<<nFID<<"> in the layer <"
140  <<GetName()<<">: " << CPLGetLastErrorMsg());
141  }
142 }
143 
145 {
146  assert(m_Layer && "OGRLayer not initialized");
147  if (nFID == OGRNullFID)
148  {
149  itkGenericExceptionMacro(<< "Invalid feature null id GetFeature() in the layer <"<<GetName()<<">.");
150  }
151  const Feature feat = m_Layer->GetFeature(nFID);
152  return feat;
153 }
154 
156 {
157  assert(m_Layer && "OGRLayer not initialized");
158 
159  if (!m_Modifiable)
160  {
161  itkGenericExceptionMacro(<< "Cannot create a new feature in the layer <"
162  <<GetName()<<">: layer is not modifiable");
163  }
164 
165  const OGRErr res = m_Layer->SetFeature(&feature.ogr());
166  if (res != OGRERR_NONE)
167  {
168  itkGenericExceptionMacro(<< "Cannot update a feature in the layer <"
169  <<GetName()<<">: " << CPLGetLastErrorMsg());
170  }
171 }
172 
173 /*===========================================================================*/
174 /*=================================[ Misc ]==================================*/
175 /*===========================================================================*/
176 std::string otb::ogr::Layer::GetName() const
177 {
178  assert(m_Layer && "null layer");
179 #if GDAL_VERSION_NUM >= 1800
180  return m_Layer->GetName();
181 #else
182  return GetLayerDefn().GetName();
183 #endif
184 }
185 
186 OGREnvelope otb::ogr::Layer::GetExtent(bool force/* = false */) const
187 {
188  assert(m_Layer && "OGRLayer not initialized");
189  OGREnvelope sExtent;
190  const OGRErr res = m_Layer->GetExtent(&sExtent,force);
191  if(res != OGRERR_NONE)
192  {
193  itkGenericExceptionMacro(<< "Cannot retrieve extent of layer <"
194  <<GetName()<<">: " << CPLGetLastErrorMsg());
195  }
196  return sExtent;
197 }
198 
199 void otb::ogr::Layer::GetExtent(double& ulx, double& uly, double& lrx, double& lry, bool force) const
200 {
201  const OGREnvelope sExtent = GetExtent(force);
202  ulx = sExtent.MinX;
203  uly = sExtent.MinY;
204  lrx = sExtent.MaxX;
205  lry = sExtent.MaxY;
206 }
207 
209 {
210  assert(m_Layer && "OGRLayer not initialized");
211  return *m_Layer;
212 }
213 
214 void otb::ogr::Layer::PrintSelf(std::ostream& os, itk::Indent indent) const
215 {
216  os << indent << "+";
217  if (m_Layer) // in case for odd reason the layer that should exist can't be found
218  {
219  os << "Layer <" << GetName() << "> of "<< OGRGeometryTypeToName(GetGeomType()) <<"\n";
220  indent = indent.GetNextIndent();
221  BOOST_FOREACH(Feature f, *this)
222  {
223  f.PrintSelf(os, indent);
224  }
225  }
226  else
227  {
228  os << "null Layer\n";
229  }
230 }
231 
232 /*===========================================================================*/
233 /*============================[ Spatial Filter ]=============================*/
234 /*===========================================================================*/
235 
236 OGRGeometry const* otb::ogr::Layer::GetSpatialFilter() const
237 {
238  assert(m_Layer && "OGRLayer not initialized");
239  OGRGeometry* spatialFilter = m_Layer->GetSpatialFilter();
240  return spatialFilter;
241 }
242 
243 void otb::ogr::Layer::SetSpatialFilter(OGRGeometry const* spatialFilter)
244 {
245  assert(m_Layer && "OGRLayer not initialized");
246  // const_cast because OGR is not 100% const-correct
247  m_Layer->SetSpatialFilter(const_cast <OGRGeometry*>(spatialFilter));
248 }
249 
251  double dfMinX, double dfMinY, double dfMaxX, double dfMaxY)
252 {
253  assert(m_Layer && "OGRLayer not initialized");
254  m_Layer->SetSpatialFilterRect(dfMinX, dfMinY, dfMaxX, dfMaxY);
255 }
256 
257 OGRSpatialReference const* otb::ogr::Layer::GetSpatialRef() const
258 {
259  assert(m_Layer && "OGRLayer not initialized");
260  return m_Layer->GetSpatialRef();
261 }
262 
264 {
265  assert(m_Layer && "OGRLayer not initialized");
266  char * wkt = 0;
267  OGRSpatialReference const* srs = GetSpatialRef();
268  if(srs)
269  {
270  const OGRErr res = srs->exportToWkt(&wkt);
271 
272  if(res != OGRERR_NONE)
273  {
274  itkGenericExceptionMacro(<< "Cannot convert spatial reference to wkt string for layer <"
275  <<GetName()<<">: " << CPLGetLastErrorMsg());
276  }
277 
278  assert(wkt);
279  const std::string stringWkt(wkt);
280  // According to documentation, argument of exportToWkt() should be freed
281  CPLFree(wkt);
282  return stringWkt;
283  }
284 
285  return "";
286 }
287 
288 /*===========================================================================*/
289 /*==========================[ Feature Definition ]===========================*/
290 /*===========================================================================*/
291 OGRFeatureDefn & otb::ogr::Layer::GetLayerDefn() const
292 {
293  assert(m_Layer && "OGRLayer not initialized");
294  return *const_cast <OGRLayer*>(m_Layer.get())->GetLayerDefn();
295 }
296 
298  FieldDefn const& field, bool bApproxOK/* = true */)
299 {
300  assert(m_Layer && "OGRLayer not initialized");
301 
302  if (!m_Modifiable)
303  {
304  itkGenericExceptionMacro(<< "Cannot create a new field in the layer <"
305  <<GetName()<<">: layer is not modifiable");
306  }
307 
308  const OGRErr res = m_Layer->CreateField(&field.ogr(), bApproxOK);
309  if (res != OGRERR_NONE)
310  {
311  itkGenericExceptionMacro(<< "Cannot create a field in the layer <"
312  <<GetName()<<">: " << CPLGetLastErrorMsg());
313  }
314 }
315 
316 void otb::ogr::Layer::DeleteField(size_t fieldIndex)
317 {
318  assert(m_Layer && "OGRLayer not initialized");
319 
320  if (!m_Modifiable)
321  {
322  itkGenericExceptionMacro(<< "Cannot delete field in the layer <"
323  <<GetName()<<">: layer is not modifiable");
324  }
325 
326 #if GDAL_VERSION_NUM < 1900
327  itkGenericExceptionMacro("OGRLayer::DeleteField is not supported by OGR v"
328  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
329 #else
330  const OGRErr res = m_Layer->DeleteField(int(fieldIndex));
331  if (res != OGRERR_NONE)
332  {
333  itkGenericExceptionMacro(<< "Cannot delete the "<<fieldIndex << "th field in the layer <"
334  <<GetName() <<">: " << CPLGetLastErrorMsg());
335  }
336 #endif
337 }
338 
340  size_t fieldIndex, FieldDefn const& newFieldDefn, int nFlags)
341 {
342  assert(m_Layer && "OGRLayer not initialized");
343 
344  if (!m_Modifiable)
345  {
346  itkGenericExceptionMacro(<< "Cannot alter field definition in the layer <"
347  <<GetName()<<">: layer is not modifiable");
348  }
349 
350 #if GDAL_VERSION_NUM < 1900
351  itkGenericExceptionMacro("OGRLayer::AlterFieldDefn is not supported by OGR v"
352  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
353 #else
354  const OGRErr res = m_Layer->AlterFieldDefn(
355  int(fieldIndex),
356  &newFieldDefn.ogr(),
357  nFlags);
358  if (res != OGRERR_NONE)
359  {
360  itkGenericExceptionMacro(<< "Cannot alter the "<<fieldIndex << "th field in the layer <"
361  <<GetName() <<">: " << CPLGetLastErrorMsg());
362  }
363 #endif
364 }
365 
366 void otb::ogr::Layer::ReorderField(size_t oldPos, size_t newPos)
367 {
368  assert(m_Layer && "OGRLayer not initialized");
369 
370  if (!m_Modifiable)
371  {
372  itkGenericExceptionMacro(<< "Cannot reorder fields in the layer <"
373  <<GetName()<<">: layer is not modifiable");
374  }
375 
376 #if GDAL_VERSION_NUM < 1900
377  itkGenericExceptionMacro("OGRLayer::ReorderField is not supported by OGR v"
378  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
379 #else
380  const OGRErr res = m_Layer->ReorderField(int(oldPos), int(newPos));
381  if (res != OGRERR_NONE)
382  {
383  itkGenericExceptionMacro(<< "Cannot move the "<<oldPos << "th field to the "
384  << newPos << "th position in the layer <" <<GetName() <<">: " << CPLGetLastErrorMsg());
385  }
386 #endif
387 }
388 
390 {
391  assert(m_Layer && "OGRLayer not initialized");
392 
393  if (!m_Modifiable)
394  {
395  itkGenericExceptionMacro(<< "Cannot reorder fields in the layer <"
396  <<GetName()<<">: layer is not modifiable");
397  }
398 
399 #if GDAL_VERSION_NUM < 1900
400  itkGenericExceptionMacro("OGRLayer::ReorderField is not supported by OGR v"
401  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
402 #else
403  const OGRErr res = m_Layer->ReorderFields(map);
404  if (res != OGRERR_NONE)
405  {
406  itkGenericExceptionMacro(<< "Cannot reorder the fields of the layer <"
407  <<GetName() <<">: " << CPLGetLastErrorMsg());
408  }
409 #endif
410 }
411 
412 void otb::ogr::Layer::SetIgnoredFields(char const** fieldNames)
413 {
414  assert(m_Layer && "OGRLayer not initialized");
415 #if GDAL_VERSION_NUM >= 1900
416  const OGRErr res = m_Layer->SetIgnoredFields(fieldNames);
417  if (res != OGRERR_NONE)
418  {
419  itkGenericExceptionMacro(<< "Cannot set fields to ignore on the layer <"
420  <<GetName() <<">: " << CPLGetLastErrorMsg());
421  }
422 #else
423  itkGenericExceptionMacro("OGRLayer::SetIgnoredFields is not supported by OGR v"
424  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
425 #endif
426 }
427 
428 OGRwkbGeometryType otb::ogr::Layer::GetGeomType() const
429 {
430  assert(m_Layer && "OGRLayer not initialized");
431 #if GDAL_VERSION_NUM < 1800
432  return GetLayerDefn().GetGeomType();
433 #else
434  return m_Layer->GetGeomType();
435 #endif
436 }
437 
438 bool otb::ogr::operator==(Layer const& lhs, Layer const& rhs)
439 {
440  const bool equal = lhs.m_Layer.get() == rhs.m_Layer.get();
441  return equal;
442 }

Generated at Sat Mar 8 2014 16:12:04 for Orfeo Toolbox with doxygen 1.8.3.1