Orfeo Toolbox  4.0
otbOGRFeatureWrapper.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 "otbOGRFeatureWrapper.h"
23 #include <boost/bind.hpp>
24 #include <boost/make_shared.hpp>
25 #include "ogr_feature.h"
26 #include "cpl_error.h"
27 #include "itkMacro.h"
28 
29 #include "otbOGRFieldWrapper.h"
30 /*===========================================================================*/
31 /*======================[ Construction / Destruction ]=======================*/
32 /*===========================================================================*/
33 otb::ogr::Feature::Feature(OGRFeatureDefn & definition)
34 : m_Feature(
35  OGRFeature::CreateFeature(&definition),
36  boost::bind(&OGRFeature::DestroyFeature, _1))
37 {
39 }
40 
41 otb::ogr::Feature::Feature(OGRFeature * feature)
42 {
43  if (feature)
44  {
45  m_Feature.reset(feature, boost::bind(&OGRFeature::DestroyFeature, _1));
46  }
47  // else default is perfect -> delete null
48 }
49 
51 {
52 }
53 
55 {
56  const Feature res(m_Feature->Clone());
57  return res;
58 }
59 
60 void otb::ogr::Feature::UncheckedSetFrom(Feature const& rhs, bool mustForgive)
61 {
62  const OGRErr res = m_Feature->SetFrom(&rhs.ogr(), mustForgive);
63  if (res != OGRERR_NONE)
64  {
65  itkGenericExceptionMacro(<<"Cannot assign from another feature: " << CPLGetLastErrorMsg());
66  }
67 }
68 
69 void otb::ogr::Feature::UncheckedSetFrom(Feature const& rhs, int * map, bool mustForgive)
70 {
71 #if GDAL_VERSION_NUM >= 1900
72  const OGRErr res = m_Feature->SetFrom(&rhs.ogr(), map, mustForgive);
73  if (res != OGRERR_NONE)
74  {
75  itkGenericExceptionMacro(<<"Cannot assign from another feature: " << CPLGetLastErrorMsg());
76  }
77 #else
78  itkGenericExceptionMacro("OGRLayer::SetFrom(feature, fieldmap, forgive) is not supported by OGR v"
79  << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
80 #endif
81 }
82 
83 /*===========================================================================*/
84 /*=================================[ Misc ]==================================*/
85 /*===========================================================================*/
86 void otb::ogr::Feature::UncheckedPrintSelf(std::ostream & os, itk::Indent indent) const
87 {
88  const size_t nbFields = m_Feature->GetFieldCount();
89  os << indent << "+";
90  os << " " << nbFields << " fields\n";
91  indent = indent.GetNextIndent();
92  for (size_t i=0; i!=nbFields; ++i)
93  {
94  assert(ogr().GetFieldDefnRef(i) && "No definition associated to the i-th field");
95  Field const& field = (*this)[i];
96  field.PrintSelf(os, indent);
97  }
98 }
99 
101 {
102  // special case: they may be null (end() mark)
103 
104  // OGR is not const correct ...
105  OGRFeature * l = const_cast<OGRFeature*>(lhs.m_Feature.get());
106  OGRFeature * r = const_cast<OGRFeature*>(rhs.m_Feature.get());
107  return
108  (l == r ) // incl. ==0
109  ||
110  (l && r && l->Equal(r)) // must be non-null to compare them with Equal
111 ;
112 }
113 
114 /*===========================================================================*/
115 /*================================[ Fields ]=================================*/
116 /*===========================================================================*/
118  return ogr().GetFieldCount();
119 }
120 
122 {
123  Field field(*this, index);
124  return field;
125 }
126 
128 {
129  const int index = GetFieldIndex(name);
130  return this->operator[](index);
131 }
132 
134 {
135  return FieldDefn(*m_Feature->GetFieldDefnRef(index));
136 }
137 
139 {
140  const int index = GetFieldIndex(name);
141  if (index < 0)
142  {
143  itkGenericExceptionMacro(<<"no field named <"<<name<<">");
144  }
145  return this->GetFieldDefn(index);
146 }
147 
148 int otb::ogr::Feature::UncheckedGetFieldIndex(std::string const& name) const
149 {
150  const int index = m_Feature->GetFieldIndex(name.c_str());
151  if (index < 0)
152  {
153  itkGenericExceptionMacro(<<"No field named <"<<name<<"> in feature");
154  }
155  return index;
156 }
157 
158 /*===========================================================================*/
159 /*==============================[ Properties ]===============================*/
160 /*===========================================================================*/
162 {
163  return m_Feature->GetFID();
164 }
165 
167 {
168  const OGRErr res = m_Feature->SetFID(fid);
169  if (res != OGRERR_NONE)
170  {
171  itkGenericExceptionMacro(<<"Cannot Set FID to "<<fid<<" for feature: " << CPLGetLastErrorMsg());
172  }
173 }
174 
175 OGRFeatureDefn& otb::ogr::Feature::UncheckedGetDefn() const
176 {
177  return *m_Feature->GetDefnRef();
178 }
179 
180 /*===========================================================================*/
181 /*==============================[ Geometries ]===============================*/
182 /*===========================================================================*/
183 
185 {
186  OGRGeometry * g = geometry.get();
187  const OGRErr res = m_Feature->SetGeometryDirectly(g);
188  if (res != OGRERR_NONE)
189  {
190  itkGenericExceptionMacro(<<"Cannot set (directly) the geometry: " << CPLGetLastErrorMsg());
191  }
192  geometry.release(); // success => commit the transaction (after any exception thrown)
193 }
194 
196 {
197  OGRGeometry * g = m_Feature->StealGeometry();
198  return UniqueGeometryPtr(g);
199 }
200 
201 void otb::ogr::Feature::UncheckedSetGeometry(OGRGeometry const* geometry)
202 {
203  // OGR copies the input geometry => should have been const
204  const OGRErr res = m_Feature->SetGeometry(const_cast <OGRGeometry*>(geometry));
205  if (res != OGRERR_NONE)
206  {
207  itkGenericExceptionMacro(<<"Cannot set the geometry: " << CPLGetLastErrorMsg());
208  }
209 }
210 
211 OGRGeometry const* otb::ogr::Feature::UncheckedGetGeometry() const
212 {
213  return m_Feature->GetGeometryRef();
214 }

Generated at Sat Mar 8 2014 16:11:54 for Orfeo Toolbox with doxygen 1.8.3.1