OTB  7.2.0
Orfeo Toolbox
otbVectorDataFileWriter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
22 #ifndef otbVectorDataFileWriter_hxx
23 #define otbVectorDataFileWriter_hxx
24 
25 #include "otbMacro.h"
27 #include "otbVectorDataIOFactory.h"
28 #include "otbVectorDataAdapter.h"
29 #include "otbVectorData.h"
30 
31 namespace otb
32 {
36 template <class TInputVectorData>
38  : m_FileName(""), m_VectorDataIO(nullptr), m_UserSpecifiedVectorDataIO(false), m_FactorySpecifiedVectorDataIO(false)
39 {
40 }
41 
45 template <class TInputVectorData>
47 {
48 }
49 
50 //---------------------------------------------------------
51 template <class TInputVectorData>
53 {
54  // ProcessObject is not const_correct so this cast is required here.
55  this->ProcessObject::SetNthInput(0, const_cast<TInputVectorData*>(input));
56 }
57 
58 //---------------------------------------------------------
59 template <class TInputVectorData>
61 {
62  if (this->GetNumberOfInputs() < 1)
63  {
64  return nullptr;
65  }
66 
67  return static_cast<TInputVectorData*>(this->ProcessObject::GetInput(0));
68 }
69 
70 //---------------------------------------------------------
71 template <class TInputVectorData>
73 {
74  return static_cast<TInputVectorData*>(this->ProcessObject::GetInput(idx));
75 }
76 
77 //---------------------------------------------------------
78 template <class TInputVectorData>
80 {
81  const InputVectorDataType* input = this->GetInput();
82 
83  itkDebugMacro(<< "Writing a vector data file");
84 
85  // Make sure input is available
86  if (input == nullptr)
87  {
88  itkExceptionMacro(<< "No input to writer!");
89  }
90 
91  // Make sure that we can write the file given the name
92  //
93  if (m_FileName == "")
94  {
95  itkExceptionMacro(<< "No filename was specified");
96  }
97 
98  if (m_VectorDataIO.IsNull()) // try creating via factory
99  {
100  itkDebugMacro(<< "Attempting factory creation of VectorDataIO for file: " << m_FileName);
101  m_VectorDataIO = VectorDataIOFactory::CreateVectorDataIO(m_FileName.c_str(), VectorDataIOFactory::WriteMode);
102  m_FactorySpecifiedVectorDataIO = true;
103  }
104  else
105  {
106  if (m_FactorySpecifiedVectorDataIO && !m_VectorDataIO->CanWriteFile(m_FileName.c_str()))
107  {
108  itkDebugMacro(<< "VectorDataIO exists but doesn't know how to write file:" << m_FileName);
109  itkDebugMacro(<< "Attempting creation of VectorDataIO with a factory for file:" << m_FileName);
110  m_VectorDataIO = VectorDataIOFactory::CreateVectorDataIO(m_FileName.c_str(), VectorDataIOFactory::WriteMode);
111  m_FactorySpecifiedVectorDataIO = true;
112  }
113  }
114 
115  if (m_VectorDataIO.IsNull())
116  {
117  VectorDataFileWriterException e(__FILE__, __LINE__);
118  std::ostringstream msg;
119  msg << " Could not create IO object for file " << m_FileName << std::endl;
120  msg << " Tried to create one of the following:" << std::endl;
121  std::list<itk::LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("otbVectorDataIOBase");
122  for (std::list<LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
123  {
124  VectorDataIOBase* io = dynamic_cast<VectorDataIOBase*>(i->GetPointer());
125  if (io)
126  msg << " " << io->GetNameOfClass() << std::endl;
127  }
128  msg << " You probably failed to set a file suffix, or" << std::endl;
129  msg << " set the suffix to an unsupported type." << std::endl;
130  e.SetDescription(msg.str());
131  e.SetLocation(ITK_LOCATION);
132  throw e;
133  }
134 
135  // NOTE: this const_cast<> is due to the lack of const-correctness
136  // of the ProcessObject.
137  InputVectorDataType* nonConstVectorData = const_cast<InputVectorDataType*>(input);
138 
139  // Make sure the data is up-to-date.
140  if (nonConstVectorData->GetSource())
141  {
142  nonConstVectorData->GetSource()->Update();
143  }
144 
145  // Notify start event observers
146  this->InvokeEvent(itk::StartEvent());
147 
148  // Actually do something
149  this->GenerateData();
150 
151  // Notify end event observers
152  this->InvokeEvent(itk::EndEvent());
153 
154  // Release upstream data if requested
155  if (input->ShouldIReleaseData())
156  {
157  nonConstVectorData->ReleaseData();
158  }
159 }
160 
161 //---------------------------------------------------------
162 template <class TInputVectorData>
164 {
165  const InputVectorDataType* input = this->GetInput();
166 
167  itkDebugMacro(<< "Writing file: " << m_FileName);
168 
169  // Setup the vector data IO for writing.
170  //
171  m_VectorDataIO->SetFileName(m_FileName);
172 
174  typename AdapterType::Pointer adapter = AdapterType::New();
175  adapter->SetInput(input);
176  adapter->Update();
177  m_VectorDataIO->Write(adapter->GetOutput());
178 }
179 
180 template <class TInputVectorData>
181 void VectorDataFileWriter<TInputVectorData>::PrintSelf(std::ostream& os, itk::Indent indent) const
182 {
183  Superclass::PrintSelf(os, indent);
184  os << indent << "VectorDataFileWriter" << std::endl;
185 }
186 
187 } // namespace otb
188 
189 #endif
Helper class to convert the vector data to generic type.
virtual const char * GetNameOfClass() const
Abstract superclass defines VectorData IO interface.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
OTBApplicationEngine_EXPORT void Write(const std::string &filename, Application::Pointer application)
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:58
This class writes vector data file format (shapefile and KML)