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