OTB  9.0.0
Orfeo Toolbox
otbImageSeriesFileReaderBase.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 #ifndef otbImageSeriesFileReaderBase_hxx
24 #define otbImageSeriesFileReaderBase_hxx
26 
27 namespace otb
28 {
29 
30 template <class TImage, class TInternalImage>
32  :
33  m_FileName(""),
34  m_OutputList(OutputImageListType::New()),
35  m_ImageFileReaderList(ReaderListType::New())
36 {
37  m_ListOfFileNames.clear();
38  m_ListOfBandSelection.clear();
39  m_ListOfRegionSelection.clear();
40 }
41 
42 template <class TImage, class TInternalImage>
44 {
45  if (file == m_FileName)
46  return;
47 
48  this->m_FileName = file;
49  ReadMetaFile();
50  AllocateListOfComponents();
51 
52  this->Modified();
53 }
54 
55 template <class TImage, class TInternalImage>
57 {
58  if (this->GetNumberOfOutputs() < 1)
59  throw ImageSeriesFileReaderException(__FILE__, __LINE__, "No data to output", ITK_LOCATION);
60 
61  return static_cast<OutputImageListType*>(this->m_OutputList);
62 }
63 
64 template <class TImage, class TInternalImage>
67 {
68  if (this->GetNumberOfOutputs() < 1)
69  throw ImageSeriesFileReaderException(__FILE__, __LINE__, "No data to output", ITK_LOCATION);
70 
71  if (idx >= m_OutputList->Size())
72  throw ImageSeriesFileReaderException(__FILE__, __LINE__, "Index out of bounds", ITK_LOCATION);
73 
74  return static_cast<OutputImageType*>(this->m_OutputList->GetNthElement(idx));
75 }
76 
77 template <class TImage, class TInternalImage>
79 {
80  m_ListOfFileNames.clear();
81  m_ListOfBandSelection.clear();
82  m_ListOfRegionSelection.clear();
83 
84  this->TestFileExistenceAndReadability(m_FileName, kFileName);
85 
86  std::string aLine;
87  std::ifstream inputFile(m_FileName, std::ios_base::in);
88 
89  inputFile >> aLine;
90  if (aLine != "ENVI")
91  {
92  inputFile.close();
93  std::ostringstream msg;
94  msg << "The file " << m_FileName << " is not a \"ENVI META FILE\" format\n";
95  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
96  throw e;
97  return;
98  }
99 
100  inputFile >> aLine;
101  if (aLine != "META")
102  {
103  inputFile.close();
104  std::ostringstream msg;
105  msg << "The file " << m_FileName << " is not a \"ENVI META FILE\" format\n";
106  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
107  throw e;
108  return;
109  }
110 
111  inputFile >> aLine;
112  if (aLine != "FILE")
113  {
114  inputFile.close();
115  std::ostringstream msg;
116  msg << "The file " << m_FileName << " is not a \"ENVI META FILE\" format\n";
117  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
118  throw e;
119  return;
120  }
121 
122  while (1)
123  {
124  /*
125  * Reading the filenames
126  */
127  do
128  {
129  inputFile >> aLine;
130  // std::cerr << "-> '" << aLine << "'\n";
131  } while (aLine != "File" && inputFile.good());
132 
133  if (inputFile.good())
134  {
135  do
136  {
137  inputFile >> aLine;
138  // std::cerr << "--> '" << aLine << "'\n";
139  } while (aLine != ":" && inputFile.good());
140 
141  if (!inputFile.good())
142  {
143  inputFile.close();
144  std::ostringstream msg;
145  msg << "Unable to read image files in the \"ENVI META FILE\" file \n";
146  msg << "FileName: " << m_FileName << "\n";
147  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
148  throw e;
149  return;
150  }
151  }
152  else
153  return; // normal exit
154 
155  inputFile >> aLine;
156 
157  std::cerr << "-> " << aLine << "\n";
158 
159  // Get the Image fileName
160  // The test may modify image file name to add the path
161  // Or throw an exception when not found nor readable
162  TestFileExistenceAndReadability(aLine, kImageFileName);
163  m_ListOfFileNames.push_back(aLine);
164  std::string imageFileName = aLine;
165 
166  /*
167  * Reading the Band number
168  */
169  do
170  {
171  inputFile >> aLine;
172  // std::cerr << "-> '" << aLine << "'\n";
173  } while (aLine != "Bands:" && inputFile.good());
174 
175  if (!inputFile.good())
176  {
177  inputFile.close();
178  std::ostringstream msg;
179  msg << "Unable to read the number of bands in the images in the \"ENVI META FILE\" file \n";
180  msg << "FileName: " << m_FileName << "\n";
181  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
182  throw e;
183  return;
184  }
185 
186  std::vector<unsigned int> bands;
187  int aBand, oldBand = -1;
188  char sep;
189  while (1)
190  {
191  inputFile >> aBand;
192  if (oldBand != -1)
193  {
194  for (int i = oldBand; i <= aBand; ++i)
195  bands.push_back(i);
196  oldBand = -1;
197  }
198  else
199  bands.push_back(aBand);
200 
201  int posRef = inputFile.tellg();
202  inputFile >> sep;
203  if (sep == '-')
204  {
205  oldBand = aBand + 1;
206  }
207  else if (sep != ',')
208  {
209  inputFile.seekg(posRef, std::ios_base::beg);
210  break;
211  }
212  }
213 
214  // Storing band selection
215  try
216  {
217  TestBandSelection(bands);
218  }
220  {
221  std::ostringstream msg;
222  msg << e.GetDescription();
223  msg << "Image FileName : " << imageFileName << "\n";
224  e.SetDescription(msg.str());
225  throw e;
226  }
227  m_ListOfBandSelection.push_back(bands);
228 
229  /*
230  * Reading the Region selection
231  */
232  do
233  {
234  inputFile >> aLine;
235  // std::cerr << "-> '" << aLine << "'\n";
236  } while (aLine != "Dims" && inputFile.good());
237 
238  if (inputFile.good())
239  {
240  do
241  {
242  inputFile >> aLine;
243  // std::cerr << "--> '" << aLine << "'\n";
244  } while (aLine != ":" && inputFile.good());
245 
246  if (!inputFile.good())
247  {
248  inputFile.close();
249  std::ostringstream msg;
250  msg << "Unable to read image region in the \"ENVI META FILE\" file \n";
251  msg << "FileName : " << m_FileName << "\n";
252  msg << "ImageName: " << imageFileName << "\n";
253  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
254  throw e;
255  return;
256  }
257  }
258 
259  int beg_line, end_line, beg_col, end_col;
260  char sep1, sep2, sep3;
261  inputFile >> beg_col >> sep1 >> end_col >> sep2 >> beg_line >> sep3 >> end_line;
262 
263  if (!inputFile.good())
264  {
265  inputFile.close();
266  std::ostringstream msg;
267  msg << "Unable to read image region selection in the \"ENVI META FILE\" file \n";
268  msg << "FileName : " << m_FileName << "\n";
269  msg << "ImageName: " << imageFileName << "\n";
270  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
271  throw e;
272  return;
273  }
274 
275  IndexType index;
276  index[0] = beg_col - 1;
277  index[1] = beg_line - 1;
278 
279  SizeType size;
280  size[0] = end_col - beg_col + 1;
281  size[1] = end_line - beg_line + 1;
282 
283  RegionType region;
284  region.SetSize(size);
285  region.SetIndex(index);
286 
287  m_ListOfRegionSelection.push_back(region);
288  }
289 }
290 
291 template <class TImage, class TInternalImage>
293 {
294  for (unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
295  {
296  m_ImageFileReaderList->PushBack(ReaderType::New());
297  m_OutputList->PushBack(OutputImageType::New());
298  }
299 }
300 
304 template <class TImage, class TInternalImage>
306 {
307  std::ostringstream msg;
308  msg << "Something wrong... Check the template definition of this class in the program...\n";
309  msg << "\"ENVI META FILE\" FileName: " << m_FileName << "\n";
310  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
311  throw e;
312 }
314 
315 template <class TImage, class TInternalImage>
317 {
318  for (unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
319  GenerateData(i);
320 }
321 
325 template <class TImage, class TInternalImage>
327 {
328  this->Update();
329  return this->GetOutput();
330 }
332 
333 template <class TImage, class TInternalImage>
336 {
337  this->GenerateData(idx);
338  return this->GetOutput(idx);
339 }
340 
341 template <class TImage, class TInternalImage>
342 void ImageSeriesFileReaderBase<TImage, TInternalImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
343 {
344  Superclass::PrintSelf(os, indent);
345  os << indent << "File to be read : " << m_FileName << "\n";
346 
347  if (m_ListOfFileNames.size() > 0)
348  {
349  std::vector<std::vector<unsigned int>>::const_iterator bandSelection = m_ListOfBandSelection.begin();
350  os << indent << "Image File(s) to be read\n";
351  for (unsigned int i = 0; i < GetNumberOfOutputs(); ++i)
352  {
353  os << indent << " "
354  << "Filename : " << m_ListOfFileNames[i] << "\n";
355  os << indent << " "
356  << "RegionSelection: Index( " << m_ListOfRegionSelection[i].GetIndex()[0] << ", " << m_ListOfRegionSelection[i].GetIndex()[1] << ") Size( "
357  << m_ListOfRegionSelection[i].GetSize()[0] << ", " << m_ListOfRegionSelection[i].GetSize()[1] << ")\n";
358  os << indent << " "
359  << "BandSelection : ";
360 
361  for (std::vector<unsigned int>::const_iterator bd = (*bandSelection).begin(); bd != (*bandSelection).end(); ++bd)
362  {
363  os << *bd << " ";
364  }
365  os << "\n";
366 
367  ++bandSelection;
368  }
369  }
370 }
371 
372 template <class TImage, class TInternalImage>
374 {
375  // Test if the file exists.
376  if (!itksys::SystemTools::FileExists(file))
377  {
378  if (fileType != kImageFileName)
379  {
380  ImageSeriesFileReaderException e(__FILE__, __LINE__);
381  std::ostringstream msg;
382  msg << "The file doesn't exist. \n";
383  if (fileType == kFileName)
384  msg << "Filename = " << file << "\n";
385  else
386  msg << "File = " << file << "\n";
387  e.SetDescription(msg.str());
388  throw e;
389  return;
390  }
391  else
392  {
393  std::vector<std::string> fullPath;
394  fullPath.push_back(itksys::SystemTools::GetFilenamePath(m_FileName));
395  fullPath.push_back("/");
396  fullPath.push_back(file);
397 
398  std::string fullFileName = itksys::SystemTools::JoinPath(fullPath);
399 
400  if (!itksys::SystemTools::FileExists(fullFileName))
401  {
402  ImageSeriesFileReaderException e(__FILE__, __LINE__);
403  std::ostringstream msg;
404  msg << "The image file doesn't exist. \n";
405  msg << "ImageFileName = " << file << "\n";
406  msg << "tested path = " << itksys::SystemTools::GetFilenamePath(m_FileName) << "\n";
407  msg << "Other Tested File = " << fullFileName << "\n";
408  e.SetDescription(msg.str());
409  throw e;
410  return;
411  }
412  else
413  {
414  // At this step, image file name is modified to add its path
415  file = fullFileName;
416  }
417  }
418  }
419 
420  // Test if the file can be open for reading access.
421  std::ifstream readTester;
422  readTester.open(file);
423  if (readTester.fail())
424  {
425  readTester.close();
426  std::ostringstream msg;
427  msg << "The file couldn't be opened for reading. " << std::endl << "Filename: " << file << std::endl;
428  ImageSeriesFileReaderException e(__FILE__, __LINE__, msg.str(), ITK_LOCATION);
429  throw e;
430  return;
431  }
432  readTester.close();
433 }
434 
435 } // end of namespace otb
436 
437 #endif
otb::ImageSeriesFileReaderBase< TImage, TImage >::FileType
FileType
Definition: otbImageSeriesFileReaderBase.h:159
otb::bands
Definition: otbParserXPlugins.h:52
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImageSeriesFileReaderBase< TImage, TImage >::DataObjectPointerArraySizeType
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbImageSeriesFileReaderBase.h:109
otb::ImageSeriesFileReaderBase< TImage, TImage >::IndexType
OutputImageType::IndexType IndexType
Definition: otbImageSeriesFileReaderBase.h:87
otb::ImageListSource< TImage >::OutputImageType
TImage OutputImageType
Definition: otbImageListSource.h:59
otb::ImageSeriesFileReaderBase
Definition: otbImageSeriesFileReaderBase.h:68
otb::ImageSeriesFileReaderBase::ImageSeriesFileReaderBase
ImageSeriesFileReaderBase()
Definition: otbImageSeriesFileReaderBase.hxx:31
otb::ImageSeriesFileReaderBase< TImage, TImage >::RegionType
OutputImageType::RegionType RegionType
Definition: otbImageSeriesFileReaderBase.h:89
otb::ImageSeriesFileReaderException
Definition: otbImageSeriesFileReaderBase.h:39
otb::ImageSeriesFileReaderBase::OutputImageType
TImage OutputImageType
Definition: otbImageSeriesFileReaderBase.h:81
otb::ImageList
This class represent a list of images.
Definition: otbImageList.h:39
otb::ObjectList
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >.
Definition: otbObjectList.h:40
otb::ImageSeriesFileReaderBase< TImage, TImage >::SizeType
OutputImageType::SizeType SizeType
Definition: otbImageSeriesFileReaderBase.h:88
otbImageSeriesFileReaderBase.h