pktools  2.6.4
Processing Kernel for geospatial data
FileReaderLas.cc
1 /**********************************************************************
2 FileReaderLas.cc: class to read LAS files using liblas API library
3 Copyright (C) 2008-2012 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #include <string>
21 #include <iostream>
22 #include <fstream>
23 #include "FileReaderLas.h"
24 //---------------------------------------------------------------------------
25 LastReturnFilter::LastReturnFilter( ) : liblas::FilterI(eInclusion) {}
26 
27 bool LastReturnFilter::filter(const liblas::Point& p)
28 {
29 
30  // If the GetReturnNumber equals the GetNumberOfReturns,
31  // we're a last return
32 
33  bool output = false;
34  if (p.GetReturnNumber() == p.GetNumberOfReturns())
35  {
36  output = true;
37  }
38 
39  // If the type is switched to eExclusion, we'll throw out all last returns.
40  if (GetType() == eExclusion && output == true)
41  {
42  output = false;
43  } else {
44  output = true;
45  }
46  return output;
47 }
48 
49 FileReaderLas::FileReaderLas(void)
50 {
51  m_reader=NULL;
52  m_ifstream=NULL;
53 }
54 
55 FileReaderLas::FileReaderLas(const std::string& filename)
56 {
57  open(filename);
58 }
59 
60 FileReaderLas::~FileReaderLas(void)
61 {
62  delete m_ifstream;
63  delete m_reader;
64 }
65 
66 //---------------------------------------------------------------------------
67 
68 void FileReaderLas::open(const std::string& filename)
69 {
70  m_filename = filename;
71  setCodec(filename);
72 }
73 
74 //---------------------------------------------------------------------------
75 void FileReaderLas::close(void)
76 {
77  m_ifstream->close();
78  m_ifstream=NULL;
79  m_reader=NULL;
80 }
81 
82 //---------------------------------------------------------------------------
83 void FileReaderLas::setCodec(const std::string& filename){
84  m_ifstream = new(std::ifstream);
85  m_ifstream->open(m_filename.c_str(),std::ios::in|std::ios::binary);
86  liblas::ReaderFactory f;
87  liblas::Reader reader = f.CreateWithStream(*m_ifstream);
88  m_reader=new liblas::Reader(reader);
89  // m_reader = new liblas::Reader(*m_ifstream);
90  //Note: It is possible to use the basic liblas::Reader constructor that takes in a std::istream, but it will not be able to account for the fact that the file might be compressed. Using the ReaderFactory will take care of all of this for you.
91  // m_reader=&rfactory.CreateWithStream(ifs);
92 }
93 
94 liblas::Header const& FileReaderLas::getHeader() const{
95  return(m_reader->GetHeader());
96 }
97 
98 bool FileReaderLas::isCompressed() const{
99  return getHeader().Compressed();
100 }
101 
102 unsigned long int FileReaderLas::getPointCount() const{
103  return getHeader().GetPointRecordsCount();
104 }
105 
106 void FileReaderLas::las2ascii(const std::string& filename, bool verbose) const{
107  std::ofstream fpoints(filename.c_str(),std::ios::out);
108  fpoints << "#";
109  fpoints << "X" << "," << "Y" << "," << "Z" << std::endl;
110  if(verbose)
111  std::cout << "reset reading" << std::endl;
112  m_reader->Reset();
113  if(verbose)
114  std::cout << "going through points" << std::endl;
115  while(m_reader->ReadNextPoint()){
116  liblas::Point const& thePoint=m_reader->GetPoint();
117  double x=thePoint.GetX();
118  double y=thePoint.GetY();
119  double z=thePoint.GetZ();
120  fpoints.precision(12);
121  fpoints << x << "," << y << "," << z << std::endl;
122  }
123  fpoints.close();
124 }
125 
126 void FileReaderLas::getExtent(double& ulx, double& uly, double& lrx, double& lry) const{
127  const liblas::Header& theHeader=getHeader();
128  ulx=theHeader.GetMinX();
129  uly=theHeader.GetMaxY();
130  lrx=theHeader.GetMaxX();
131  lry=theHeader.GetMinY();
132 }
133 
134 double FileReaderLas::getMinZ() const{
135  return(getHeader().GetMinZ());
136 }
137 
138 double FileReaderLas::getMaxZ() const{
139  return(getHeader().GetMaxZ());
140 }
141 
142 //todo: does not work ??
143 // void FileReaderLas::addBoundsFilter(double ulx, double uly, double lrx, double lry){
144 // liblas::Bounds<double> bounds = liblas::Bounds<double>(ulx,lry,lrx,uly);
145 // typedef liblas::BoundsFilter filter;
146 // filter* bounds_filter = new filter(bounds);
147 // bounds_filter->SetType(liblas::FilterI::eInclusion);
148 // m_filters.push_back(liblas::FilterPtr(bounds_filter));
149 // }
150 
151 void FileReaderLas::addReturnsFilter(std::vector<unsigned short> const& returns){
152  typedef liblas::ReturnFilter filter;
153  filter* return_filter;
154  std::vector<boost::uint16_t> returns_t;
155  if(returns[0]<0)
156  return_filter=new filter(returns_t,true);
157  else{
158  for(int index=0;index<returns.size();++index){
159  assert(returns[index]>0);
160  returns_t.push_back(returns[index]);
161  }
162  return_filter=new filter(returns_t,false);
163  }
164  m_filters.push_back(liblas::FilterPtr(return_filter));
165 }
166 
167 void FileReaderLas::addClassFilter(std::vector<unsigned short> const& classes){
168 
169  std::vector<liblas::FilterPtr> filters;
170  std::vector<liblas::Classification> theClasses;
171  for(int iclass=0;iclass<classes.size();++iclass){
172  liblas::Classification aClass(classes[iclass]);
173  theClasses.push_back(aClass);
174  }
175  liblas::FilterPtr class_filter = liblas::FilterPtr(new liblas::ClassificationFilter(theClasses));
176  // eInclusion means to keep the classes that match. eExclusion would
177  // throw out those that matched
178  class_filter->SetType(liblas::FilterI::eInclusion);
179  m_filters.push_back(class_filter);
180 }
181