Orfeo Toolbox  3.16
otbPointSetFileReader.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 #ifndef __otbPointSetFileReader_txx
19 #define __otbPointSetFileReader_txx
20 
21 #include "otbPointSetFileReader.h"
22 #include "otbMacro.h"
23 
24 #include <liblas/capi/liblas.h>
25 
26 #include <fstream> // std::ifstream
27 #include <iostream> // std::cout
28 #include <iomanip>
29 
30 namespace otb
31 {
32 template <class TOutputPointSet>
34 ::PointSetFileReader() : otb::PointSetSource<TOutputPointSet>()
35 {
36  m_NumberOfPoints = -1;
37  m_MinX = 0;
38  m_MaxX = 0;
39  m_MinY = 0;
40  m_MaxY = 0;
41 }
42 
43 template <class TOutputPointSet>
46 {
47 }
48 
49 template <class TOutputPointSet>
50 void
53 {
54  typename TOutputPointSet::Pointer output = this->GetOutput();
55 
56  otbDebugMacro(<< "Reading file for GenerateOutputInformation()" << m_FileName);
57 
58  // Check to see if we can read the file given the name or prefix
59  //
60  if (m_FileName == "")
61  {
62  throw itk::ImageFileReaderException(__FILE__, __LINE__, "FileName must be specified", ITK_LOCATION);
63  }
64 
65  this->TestFileExistanceAndReadability();
66 
67  LASReaderH reader = LASReader_Create(m_FileName.c_str());
68  LASHeaderH header = LASReader_GetHeader(reader);
69 
70  otbDebugMacro(<< "Signature: " << LASHeader_GetFileSignature(header));
71  otbDebugMacro(<< "Points count: " << LASHeader_GetPointRecordsCount(header));
72 
73  m_NumberOfPoints = LASHeader_GetPointRecordsCount(header);
74 
75  m_MinX = LASHeader_GetMinX(header);
76  m_MaxX = LASHeader_GetMaxX(header);
77  m_MinY = LASHeader_GetMinY(header);
78  m_MaxY = LASHeader_GetMaxY(header);
79 
80  LASReader_Destroy(reader);
81 }
82 
83 template <class TOutputPointSet>
84 void
87 {
88  // Test if the file exists.
89  if (!itksys::SystemTools::FileExists(m_FileName.c_str()))
90  {
91  itk::ImageFileReaderException e(__FILE__, __LINE__);
92  std::ostringstream msg;
93  msg << "The file doesn't exist. "
94  << std::endl << "Filename = " << m_FileName
95  << std::endl;
96  e.SetDescription(msg.str().c_str());
97  throw e;
98  return;
99  }
100 
101  // Test if the file can be open for reading access.
102  std::ifstream readTester;
103  readTester.open(m_FileName.c_str());
104  if (readTester.fail())
105  {
106  readTester.close();
107  std::ostringstream msg;
108  msg << "The file couldn't be opened for reading. "
109  << std::endl << "Filename: " << m_FileName
110  << std::endl;
111  itk::ImageFileReaderException e(__FILE__, __LINE__, msg.str().c_str(), ITK_LOCATION);
112  throw e;
113  return;
114 
115  }
116  readTester.close();
117 }
118 
119 template <class TOutputPointSet>
122 {
123  typename TOutputPointSet::Pointer output = this->GetOutput();
124 
125  LASReaderH reader = LASReader_Create(m_FileName.c_str());
126  LASHeaderH header = LASReader_GetHeader(reader);
127 
128  otbDebugMacro(<< "Signature: " << LASHeader_GetFileSignature(header));
129  otbDebugMacro(<< "Points count: " << LASHeader_GetPointRecordsCount(header));
130 
131  m_NumberOfPoints = LASHeader_GetPointRecordsCount(header);
132 
133  //If the output pointset is of dimension 2, altitude is stored as information
134  if (PointType::PointDimension == 2)
135  {
136  LASPointH pt = LASPoint_Create();
137  while ((pt = LASReader_GetNextPoint(reader)))
138  {
139 
140  PointType point;
141  point[0] = LASPoint_GetX(pt);
142  point[1] = LASPoint_GetY(pt);
143 
144  unsigned long i = output->GetNumberOfPoints();
145  output->SetPoint(i, point);
146 
147  PixelType V;
148  V = static_cast<PixelType>(LASPoint_GetZ(pt));
149  output->SetPointData(i, V);
150  }
151  }
152  //If the output pointset is of dimension 3, store the altitude as information
153  else if (PointType::PointDimension == 3)
154  {
155  LASPointH p = LASPoint_Create();
156  while ((p = LASReader_GetNextPoint(reader)))
157  {
158  //liblas::Point const& p = reader.GetPoint();
159 
160  PointType point;
161  point[0] = LASPoint_GetX(p);
162  point[1] = LASPoint_GetY(p);
163  point[2] = LASPoint_GetZ(p);
164 
165  unsigned long i = output->GetNumberOfPoints();
166  output->SetPoint(i, point);
167 
168  PixelType V;
169  V = static_cast<PixelType>(LASPoint_GetZ(p));
170  output->SetPointData(i, V);
171 
172  }
173  }
174  else
175  {
176  itkExceptionMacro(<< "Can't handle pointset dimension other than 2 and 3");
177  }
178 
179  LASReader_Destroy(reader);
180 }
181 
182 template <class TOutputPointSet>
184 ::PrintSelf(std::ostream& os, itk::Indent indent) const
185 {
186  Superclass::PrintSelf(os, indent);
187  os << indent << "Number of points: " << this->m_NumberOfPoints << std::endl;
188  os << indent << std::setprecision(15);
189  os << indent << "Min X: " << this->m_MinX << std::endl;
190  os << indent << "Max X: " << this->m_MaxX << std::endl;
191  os << indent << "Min Y: " << this->m_MinY << std::endl;
192  os << indent << "Max Y: " << this->m_MaxY << std::endl;
193  os << indent << "m_FileName: " << this->m_FileName << "\n";
194 }
195 
196 } //namespace otb
197 
198 #endif

Generated at Sun Feb 3 2013 00:42:03 for Orfeo Toolbox with doxygen 1.8.1.1