Orfeo Toolbox  4.0
otbGeometriesToGeometriesFilter.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 /*===========================================================================*/
23 #include <cassert>
24 #include "otbGeometriesSet.h"
25 #include "itkMacro.h"
26 #include "itkTimeProbe.h"
27 #include "otbMacro.h"
28 
29 /*===========================================================================*/
30 /*============================[ ProcessVisitor ]=============================*/
31 /*===========================================================================*/
41 struct otb::internal::ProcessVisitor : boost::static_visitor<>
42 {
44  : m_filter(filter) {}
54  void operator()(otb::ogr::Layer const& source, otb::ogr::Layer & destination) const
55  {
56  m_filter.DoProcessLayer(source, destination);
57  }
58 
76  {
77  assert(source && "can't filter a nil datasource");
78  assert(destination && "can't filter to a nil datasource");
79  for (otb::ogr::DataSource::const_iterator b = source->begin(), e = source->end()
80 ; b != e
81 ; ++b
82  )
83  {
84  otb::ogr::Layer const& sourceLayer = *b;
85  assert(sourceLayer && "unexpected nil source layer");
86  otb::ogr::Layer destLayer = destination->CreateLayer(
87  sourceLayer.GetName(),
91  );
92  m_filter.DoDefineNewLayerFields(sourceLayer, destLayer);
93  m_filter.DoProcessLayer(sourceLayer, destLayer);
94  }
95  }
96 
105  void operator()(otb::ogr::Layer & inout) const
106  {
107  m_filter.DoProcessLayer(inout, inout);
108  }
109 
119  {
120  assert(inout && "can't filter a nil datasource");
121  for (otb::ogr::DataSource::iterator b = inout->begin(), e = inout->end(); b != e; ++b)
122  {
123  otb::ogr::Layer layer = *b;
124  assert(layer && "unexpected nil source layer");
125  m_filter.DoProcessLayer(layer, layer);
126  }
127  }
128 
136  template <typename GT1, typename GT2> void operator()(GT1 const&, GT2 &) const
137  {
138  assert(!"You shall not mix DataSources and Layers in GeometriesToGeometriesFilter");
139  itkGenericExceptionMacro(<<"You shall not mix DataSources and Layers in GeometriesToGeometriesFilter");
140  }
141 private:
143 };
144 
145 /*===========================================================================*/
146 /*=====================[ GeometriesToGeometriesFilter ]======================*/
147 /*===========================================================================*/
148 
150 {
151 }
152 
153 /*virtual*/
155 {
156 }
157 
159  const InputGeometriesType * input)
160 {
161  // Process object is not const-correct so the const_cast is required here
163  0,
164  const_cast<InputGeometriesType *>(input));
165 }
166 
169 {
170  return static_cast <InputGeometriesType*>(Superclass::GetInput(0));
171 }
172 
174  InputGeometriesType const& source, OutputGeometriesType & destination)
175 {
176  // si layer, appelle virt process layer
177  // si DS, loop et appelle virt process layer
178  source.apply(internal::ProcessVisitor(*this), destination);
179 }
180 
182  OutputGeometriesType & inout)
183 {
184  // si layer, appelle virt process layer
185  // si DS, loop et appelle virt process layer
186  inout.apply(internal::ProcessVisitor(*this));
187 }
188 
189 /*virtual*/
191 {
192  this->DoAllocateOutputs();
193  this->DoFinalizeInitialisation();
194 
195  InputGeometriesType::ConstPointer input = this->GetInput();
196  // assert(input && "Cann't filter to a nil geometries set");
197  OutputGeometriesType::Pointer output = this->GetOutput();
198  assert(output && "Cann't filter a nil geometries set");
199 
200  // Start recursive processing
201  itk::TimeProbe chrono;
202  chrono.Start();
203  if (input)
204  {
205  this->Process(*input, *output);
206  }
207  else
208  {
209  this->Process(*output);
210  }
211 
212  chrono.Stop();
213  otbMsgDevMacro(<< "GeometriesToGeometriesFilter: geometries processed in " << chrono.GetMean() << " seconds.");
214 }
215 
216 /*virtual*/
218  ogr::Layer const& source) const
219 {
220  return 0;
221 }
222 
223 /*virtual*/
225  ogr::Layer const& source) const
226 {
227  return source.GetGeomType();
228 }
229 
230 /*virtual*/
232  ogr::Layer const& source) const
233 {
234  return std::vector<std::string>();
235 }
236 
237 /*===========================================================================*/
238 /*========================[ FieldCopyTransformation ]========================*/
239 /*===========================================================================*/
241  otb::ogr::Layer const& source, otb::ogr::Layer & dest) const
242 {
243  OGRFeatureDefn & inDefinition = source.GetLayerDefn();
244  OGRFeatureDefn & outDefinition = dest.GetLayerDefn();
245  for (int i=0,N=inDefinition.GetFieldCount(); i!=N; ++i)
246  {
247  dest.CreateField(*inDefinition.GetFieldDefn(i));
248  // assume the definition is updated automatically
249  m_SourceToDestFieldIndicesMap[i] = outDefinition.GetFieldCount()-1;
250  }
251 }
252 
253 
255  otb::ogr::Feature const& inFeature, otb::ogr::Feature & outFeature) const
256 {
257  // default => copy all fields for copy transformation
258 
259  // The following can't be assumed because of Drivers like KML that always add
260  // two fields: "Description" and "Name"
261  //assert(inFeature.GetSize() == outFeature.GetSize());
262 
263  for (size_t i=0,N=inFeature.GetSize(); i!=N; ++i)
264  {
265  int const indexNewField = m_SourceToDestFieldIndicesMap[i];
266  outFeature[indexNewField].Assign(inFeature[i]);
267  }
268 }

Generated at Sat Mar 8 2014 15:57:36 for Orfeo Toolbox with doxygen 1.8.3.1