Orfeo Toolbox  3.16
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  OGRGeometry const* g = GetGeometry();
99 }
100 
102 {
103  // special case: they may be null (end() mark)
104 
105  // OGR is not const correct ...
106  OGRFeature * l = const_cast<OGRFeature*>(lhs.m_Feature.get());
107  OGRFeature * r = const_cast<OGRFeature*>(rhs.m_Feature.get());
108  return
109  (l == r ) // incl. ==0
110  ||
111  (l && r && l->Equal(r)) // must be non-null to compare them with Equal
112 ;
113 }
114 
115 /*===========================================================================*/
116 /*================================[ Fields ]=================================*/
117 /*===========================================================================*/
119  return ogr().GetFieldCount();
120 }
121 
123 {
124  Field field(*this, index);
125  return field;
126 }
127 
129 {
130  const int index = GetFieldIndex(name);
131  return this->operator[](index);
132 }
133 
135 {
136  return FieldDefn(*m_Feature->GetFieldDefnRef(index));
137 }
138 
140 {
141  const int index = GetFieldIndex(name);
142  if (index < 0)
143  {
144  itkGenericExceptionMacro(<<"no field named <"<<name<<">");
145  }
146  return this->GetFieldDefn(index);
147 }
148 
149 int otb::ogr::Feature::UncheckedGetFieldIndex(std::string const& name) const
150 {
151  const int index = m_Feature->GetFieldIndex(name.c_str());
152  if (index < 0)
153  {
154  itkGenericExceptionMacro(<<"No field named <"<<name<<"> in feature");
155  }
156  return index;
157 }
158 
159 /*===========================================================================*/
160 /*==============================[ Properties ]===============================*/
161 /*===========================================================================*/
163 {
164  return m_Feature->GetFID();
165 }
166 
168 {
169  const OGRErr res = m_Feature->SetFID(fid);
170  if (res != OGRERR_NONE)
171  {
172  itkGenericExceptionMacro(<<"Cannot Set FID to "<<fid<<" for feature: " << CPLGetLastErrorMsg());
173  }
174 }
175 
176 OGRFeatureDefn& otb::ogr::Feature::UncheckedGetDefn() const
177 {
178  return *m_Feature->GetDefnRef();
179 }
180 
181 /*===========================================================================*/
182 /*==============================[ Geometries ]===============================*/
183 /*===========================================================================*/
184 
186 {
187  OGRGeometry * g = geometry.get();
188  const OGRErr res = m_Feature->SetGeometryDirectly(g);
189  if (res != OGRERR_NONE)
190  {
191  itkGenericExceptionMacro(<<"Cannot set (directly) the geometry: " << CPLGetLastErrorMsg());
192  }
193  geometry.release(); // success => commit the transaction (after any exception thrown)
194 }
195 
197 {
198  OGRGeometry * g = m_Feature->StealGeometry();
199  return UniqueGeometryPtr(g);
200 }
201 
202 void otb::ogr::Feature::UncheckedSetGeometry(OGRGeometry const* geometry)
203 {
204  // OGR copies the input geometry => should have been const
205  const OGRErr res = m_Feature->SetGeometry(const_cast <OGRGeometry*>(geometry));
206  if (res != OGRERR_NONE)
207  {
208  itkGenericExceptionMacro(<<"Cannot set the geometry: " << CPLGetLastErrorMsg());
209  }
210 }
211 
212 OGRGeometry const* otb::ogr::Feature::UncheckedGetGeometry() const
213 {
214  return m_Feature->GetGeometryRef();
215 }

Generated at Sun Feb 3 2013 00:40:43 for Orfeo Toolbox with doxygen 1.8.1.1