Orfeo Toolbox  4.0
otbVectorDataFileWriter.txx
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 #ifndef __otbVectorDataFileWriter_txx
20 #define __otbVectorDataFileWriter_txx
21 
22 #include "otbMacro.h"
24 #include "otbVectorDataIOFactory.h"
25 #include "otbVectorDataAdapter.h"
26 #include "otbVectorData.h"
27 
28 namespace otb
29 {
33 template <class TInputVectorData>
36  m_FileName(""),
37  m_VectorDataIO(0),
38  m_UserSpecifiedVectorDataIO(false)
39 {
40 }
44 template <class TInputVectorData>
47 {
48 }
49 
50 //---------------------------------------------------------
51 template <class TInputVectorData>
52 void
55 {
56  // ProcessObject is not const_correct so this cast is required here.
57  this->ProcessObject::SetNthInput(0,
58  const_cast<TInputVectorData *>(input));
59 }
60 
61 //---------------------------------------------------------
62 template <class TInputVectorData>
66 {
67  if (this->GetNumberOfInputs() < 1)
68  {
69  return 0;
70  }
71 
72  return static_cast<TInputVectorData*>
73  (this->ProcessObject::GetInput(0));
74 }
75 
76 //---------------------------------------------------------
77 template <class TInputVectorData>
80 ::GetInput(unsigned int idx)
81 {
82  return static_cast<TInputVectorData*> (this->ProcessObject::GetInput(idx));
83 }
84 
85 //---------------------------------------------------------
86 template <class TInputVectorData>
87 void
90 {
91  const InputVectorDataType * input = this->GetInput();
92 
93  itkDebugMacro(<< "Writing a vector data file");
94 
95  // Make sure input is available
96  if (input == 0)
97  {
98  itkExceptionMacro(<< "No input to writer!");
99  }
100 
101  // Make sure that we can write the file given the name
102  //
103  if (m_FileName == "")
104  {
105  itkExceptionMacro(<< "No filename was specified");
106  }
107 
108  if (m_VectorDataIO.IsNull()) //try creating via factory
109  {
110  itkDebugMacro(<< "Attempting factory creation of VectorDataIO for file: "
111  << m_FileName);
113  m_FileName.c_str(),
115  m_FactorySpecifiedVectorDataIO = true;
116  }
117  else
118  {
119  if (m_FactorySpecifiedVectorDataIO && !m_VectorDataIO->CanWriteFile(m_FileName.c_str()))
120  {
121  itkDebugMacro(<< "VectorDataIO exists but doesn't know how to write file:"
122  << m_FileName);
123  itkDebugMacro(<< "Attempting creation of VectorDataIO with a factory for file:"
124  << m_FileName);
126  m_FileName.c_str(),
128  m_FactorySpecifiedVectorDataIO = true;
129  }
130  }
131 
132  if (m_VectorDataIO.IsNull())
133  {
134  VectorDataFileWriterException e(__FILE__, __LINE__);
135  std::ostringstream msg;
136  msg << " Could not create IO object for file "
137  << m_FileName.c_str() << std::endl;
138  msg << " Tried to create one of the following:" << std::endl;
139  std::list<itk::LightObject::Pointer> allobjects =
140  itk::ObjectFactoryBase::CreateAllInstance("otbVectorDataIOBase");
141  for (std::list<LightObject::Pointer>::iterator i = allobjects.begin();
142  i != allobjects.end(); ++i)
143  {
144  VectorDataIOBase* io = dynamic_cast<VectorDataIOBase*>(i->GetPointer());
145  msg << " " << io->GetNameOfClass() << std::endl;
146  }
147  msg << " You probably failed to set a file suffix, or" << std::endl;
148  msg << " set the suffix to an unsupported type." << std::endl;
149  e.SetDescription(msg.str().c_str());
150  e.SetLocation(ITK_LOCATION);
151  throw e;
152  }
153 
154  // NOTE: this const_cast<> is due to the lack of const-correctness
155  // of the ProcessObject.
156  InputVectorDataType * nonConstVectorData = const_cast<InputVectorDataType *>(input);
157 
158  // Make sure the data is up-to-date.
159  if (nonConstVectorData->GetSource())
160  {
161  nonConstVectorData->GetSource()->Update();
162  }
163 
164  // Notify start event observers
165  this->InvokeEvent(itk::StartEvent());
166 
167  // Actually do something
168  this->GenerateData();
169 
170  // Notify end event observers
171  this->InvokeEvent(itk::EndEvent());
172 
173  // Release upstream data if requested
174  if (input->ShouldIReleaseData())
175  {
176  nonConstVectorData->ReleaseData();
177  }
178 }
179 
180 //---------------------------------------------------------
181 template <class TInputVectorData>
182 void
185 {
186  const InputVectorDataType * input = this->GetInput();
187 
188  itkDebugMacro(<< "Writing file: " << m_FileName);
189 
190  // Setup the vector data IO for writing.
191  //
192  m_VectorDataIO->SetFileName(m_FileName.c_str());
193 
195  typename AdapterType::Pointer adapter = AdapterType::New();
196  adapter->SetInput(input);
197  adapter->Update();
198  m_VectorDataIO->Write(adapter->GetOutput());
199 
200 }
201 
202 template <class TInputVectorData>
203 void
205 ::PrintSelf(std::ostream& os, itk::Indent indent) const
206 {
207  Superclass::PrintSelf(os, indent);
208  os << indent << "VectorDataFileWriter" << std::endl;
209 }
210 
211 } //namespace otb
212 
213 #endif

Generated at Sat Mar 8 2014 16:24:28 for Orfeo Toolbox with doxygen 1.8.3.1