| pktools 2.6.7 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//---------------------------------------------------------------------------
25LastReturnFilter::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
49FileReaderLas::FileReaderLas(void)
50{
51 m_reader=NULL;
52 m_ifstream=NULL;
53}
54
55FileReaderLas::FileReaderLas(const std::string& filename)
56{
57 open(filename);
58}
59
60FileReaderLas::~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
94liblas::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 bool const& FileReaderLas::readNextPoint(liblas::Point& thePoint){
107 bool returnValue=m_reader->ReadNextPoint();
108 thePoint=m_reader->GetPoint();
109 return(returnValue);
110}
111
112 void FileReaderLas::las2ascii(const std::string& filename, bool verbose) const{
113 std::ofstream fpoints(filename.c_str(),std::ios::out);
114 fpoints << "#";
115 fpoints << "X" << "," << "Y" << "," << "Z" << std::endl;
116 if(verbose)
117 std::cout << "reset reading" << std::endl;
118 m_reader->Reset();
119 if(verbose)
120 std::cout << "going through points" << std::endl;
121 while(m_reader->ReadNextPoint()){
122 liblas::Point const& thePoint=m_reader->GetPoint();
123 double x=thePoint.GetX();
124 double y=thePoint.GetY();
125 double z=thePoint.GetZ();
126 fpoints.precision(12);
127 fpoints << x << "," << y << "," << z << std::endl;
128 }
129 fpoints.close();
130}
131
132 void FileReaderLas::getExtent(double& ulx, double& uly, double& lrx, double& lry) const{
133 const liblas::Header& theHeader=getHeader();
134 ulx=theHeader.GetMinX();
135 uly=theHeader.GetMaxY();
136 lrx=theHeader.GetMaxX();
137 lry=theHeader.GetMinY();
138}
139
140 double FileReaderLas::getMinZ() const{
141 return(getHeader().GetMinZ());
142}
143
144 double FileReaderLas::getMaxZ() const{
145 return(getHeader().GetMaxZ());
146}
147
148//todo: does not work ??
149// void FileReaderLas::addBoundsFilter(double ulx, double uly, double lrx, double lry){
150// liblas::Bounds<double> bounds = liblas::Bounds<double>(ulx,lry,lrx,uly);
151// typedef liblas::BoundsFilter filter;
152// filter* bounds_filter = new filter(bounds);
153// bounds_filter->SetType(liblas::FilterI::eInclusion);
154// m_filters.push_back(liblas::FilterPtr(bounds_filter));
155// }
156
157 void FileReaderLas::addReturnsFilter(std::vector<unsigned short> const& returns){
158 typedef liblas::ReturnFilter filter;
159 filter* return_filter;
160 std::vector<boost::uint16_t> returns_t;
161 if(returns[0]<0)
162 return_filter=new filter(returns_t,true);
163 else{
164 for(int index=0;index<returns.size();++index){
165 assert(returns[index]>0);
166 returns_t.push_back(returns[index]);
167 }
168 return_filter=new filter(returns_t,false);
169 }
170 m_filters.push_back(liblas::FilterPtr(return_filter));
171}
172
173 void FileReaderLas::addClassFilter(std::vector<unsigned short> const& classes){
174
175 std::vector<liblas::FilterPtr> filters;
176 std::vector<liblas::Classification> theClasses;
177 for(int iclass=0;iclass<classes.size();++iclass){
178 liblas::Classification aClass(classes[iclass]);
179 theClasses.push_back(aClass);
180 }
181 liblas::FilterPtr class_filter = liblas::FilterPtr(new liblas::ClassificationFilter(theClasses));
182 // eInclusion means to keep the classes that match. eExclusion would
183 // throw out those that matched
184 class_filter->SetType(liblas::FilterI::eInclusion);
185 m_filters.push_back(class_filter);
186}
187