OTB  9.0.0
Orfeo Toolbox
otbVectorDataFileReader.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 otbVectorDataFileReader_hxx
23 #define otbVectorDataFileReader_hxx
24 
25 #include <fstream>
26 
27 #include "otbMacro.h"
28 #include "otbSystem.h"
29 #include "otbVectorDataIOFactory.h"
31 #include "itksys/SystemTools.hxx"
32 #include "otbVectorDataAdapter.h"
33 #include "otbVectorData.h"
34 
35 namespace otb
36 {
40 template <class TOutputVectorData>
41 VectorDataFileReader<TOutputVectorData>::VectorDataFileReader() : m_VectorDataIO(nullptr), m_UserSpecifiedVectorDataIO(false), m_FileName("")
42 {
43 }
44 
48 template <class TOutputVectorData>
50 {
51 }
52 
58 template <class TOutputVectorData>
60 {
61  // Test if the file exists.
62  if (!itksys::SystemTools::FileExists(this->m_FileName))
63  {
64  VectorDataFileReaderException e(__FILE__, __LINE__);
65  std::ostringstream msg;
66  msg << "The file doesn't exist. " << std::endl << "Filename = " << this->m_FileName << std::endl;
67  e.SetDescription(msg.str());
68  throw e;
69  return;
70  }
72 
73  // Test if the file can be open for reading access.
74  // Only if m_FileName specify a filename (not a dirname)
75  // FIXME shapefile should be able to open a directory and load the
76  // individual shapefiles as layers
77  if (itksys::SystemTools::FileExists(this->m_FileName, true) == true)
78  {
79  std::ifstream readTester;
80  readTester.open(this->m_FileName);
81  if (readTester.fail())
82  {
83  readTester.close();
84  std::ostringstream msg;
85  msg << "The file couldn't be opened for reading. " << std::endl << "Filename: " << this->m_FileName << std::endl;
86  VectorDataFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
87  throw e;
88  return;
89  }
90  readTester.close();
91  }
92 }
93 
94 template <class TOutputVectorData>
96 {
97  itkDebugMacro("setting VectorDataIO to " << vectorDataIO);
98  if (this->m_VectorDataIO != vectorDataIO)
99  {
100  this->m_VectorDataIO = vectorDataIO;
101  this->Modified();
102  }
103  m_UserSpecifiedVectorDataIO = true;
104 }
105 
106 template <class TOutputVectorData>
108 {
109  typename TOutputVectorData::Pointer output = this->GetOutput();
110 
111  itkDebugMacro(<< "Reading file for GenerateOutputInformation()" << m_FileName);
112 
113  // Check to see if we can read the file given the name or prefix
114  //
115  if (m_FileName == "")
116  {
117  throw VectorDataFileReaderException(__FILE__, __LINE__, "FileName must be specified", ITK_LOCATION);
118  }
119 
120  // Test if the file exist and if it can be open.
121  // and exception will be thrown otherwise.
122  //
123  try
124  {
125  m_ExceptionMessage = "";
126  this->TestFileExistenceAndReadability();
127  }
128  catch (itk::ExceptionObject& err)
129  {
130  m_ExceptionMessage = err.GetDescription();
131  }
132 
133  if (m_UserSpecifiedVectorDataIO == false) // try creating via factory
134  {
135  m_VectorDataIO = VectorDataIOFactory::CreateVectorDataIO(m_FileName.c_str(), VectorDataIOFactory::ReadMode);
136  }
137 
138  if (m_VectorDataIO.IsNull())
139  {
140  std::ostringstream msg;
141  msg << " Could not create IO object for file " << m_FileName << std::endl;
142  if (m_ExceptionMessage.size())
143  {
144  msg << m_ExceptionMessage;
145  }
146  else
147  {
148  msg << " Tried to create one of the following:" << std::endl;
149  std::list<itk::LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("otbVectorDataIOBase");
150  for (std::list<itk::LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
151  {
152  VectorDataIOBase* io = dynamic_cast<VectorDataIOBase*>(i->GetPointer());
153  msg << " " << io->GetNameOfClass() << std::endl;
154  }
155  msg << " You probably failed to set a file suffix, or" << std::endl;
156  msg << " set the suffix to an unsupported type." << std::endl;
157  }
158  VectorDataFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
159  throw e;
160  return;
161  }
162 
163  m_VectorDataIO->SetFileName(m_FileName);
164 
165  // Copy MetaDataDictionary from instantiated reader to output VectorData.
166  output->SetMetaDataDictionary(m_VectorDataIO->GetMetaDataDictionary());
167  this->SetMetaDataDictionary(m_VectorDataIO->GetMetaDataDictionary());
168 }
169 
170 template <class TOutputVectorData>
172 {
173 
174  typename TOutputVectorData::Pointer output = this->GetOutput();
175 
176  itkDebugMacro(<< "VectorDataFileReader::GenerateData() \n");
177 
178  // Test if the file exist and if it can be open.
179  // and exception will be thrown otherwise.
180  try
181  {
182  m_ExceptionMessage = "";
183  this->TestFileExistenceAndReadability();
184  }
185  catch (itk::ExceptionObject& err)
186  {
187  m_ExceptionMessage = err.GetDescription();
188  }
189 
190  m_VectorDataIO->SetFileName(m_FileName);
191 
192  // Tell the VectorDataIO to read the file
193  //
194 
195  typedef VectorDataAdapter<VectorData<double, 2>, TOutputVectorData> AdapterType;
196  typename AdapterType::InputVectorDataType::Pointer input = AdapterType::InputVectorDataType::New();
197 
198  m_VectorDataIO->Read(input);
199 
200  typename AdapterType::Pointer adapter = AdapterType::New();
201  adapter->SetInput(input);
202  adapter->GraftOutput(output);
203  adapter->Update();
204  this->GraftOutput(adapter->GetOutput());
205 
206  return;
207 }
208 
209 template <class TOutputVectorData>
210 void VectorDataFileReader<TOutputVectorData>::PrintSelf(std::ostream& os, itk::Indent indent) const
211 {
212  Superclass::PrintSelf(os, indent);
213 
214  if (m_VectorDataIO)
215  {
216  os << indent << "VectorDataIO: \n";
217  m_VectorDataIO->Print(os, indent.GetNextIndent());
218  }
219  else
220  {
221  os << indent << "m_VectorDataIO: (null)"
222  << "\n";
223  }
224 
225  os << indent << "UserSpecifiedVectorDataIO flag: " << m_UserSpecifiedVectorDataIO << "\n";
226  os << indent << "m_FileName: " << m_FileName << "\n";
227 }
228 
229 } // namespace otb
230 
231 #endif
otb::VectorDataIOBase::GetNameOfClass
virtual const char * GetNameOfClass() const
otb::VectorDataFileReader::VectorDataFileReader
VectorDataFileReader()
Definition: otbVectorDataFileReader.hxx:41
otb::VectorDataFileReaderException
Base exception class for IO conflicts.
Definition: otbVectorDataFileReader.h:36
otbVectorDataAdapter.h
otb::VectorDataIOFactory::CreateVectorDataIO
static VectorDataIOBasePointerType CreateVectorDataIO(const char *path, FileModeType mode)
otb::VectorDataFileReader::GenerateOutputInformation
void GenerateOutputInformation(void) override
Definition: otbVectorDataFileReader.hxx:107
otb::VectorDataFileReader::TestFileExistenceAndReadability
void TestFileExistenceAndReadability()
Definition: otbVectorDataFileReader.hxx:59
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbMacro.h
otb::VectorDataIOFactory::ReadMode
@ ReadMode
Definition: otbVectorDataIOFactory.h:55
otb::VectorDataFileReader::SetVectorDataIO
void SetVectorDataIO(VectorDataIOBaseType *vectorDataIO)
Definition: otbVectorDataFileReader.hxx:95
otbSystem.h
otb::VectorDataFileReader::GenerateData
void GenerateData() override
Definition: otbVectorDataFileReader.hxx:171
otb::VectorDataFileReader::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbVectorDataFileReader.hxx:210
otbVectorData.h
otbVectorDataFileReader.h
otb::VectorDataAdapter
Helper class to convert the vector data to generic type.
Definition: otbVectorDataAdapter.h:37
otb::VectorDataFileReader::~VectorDataFileReader
~VectorDataFileReader() override
Definition: otbVectorDataFileReader.hxx:49
otbVectorDataIOFactory.h
otb::VectorDataIOBase
Abstract superclass defines VectorData IO interface.
Definition: otbVectorDataIOBase.h:60