libLAS Tutorial
This basic tutorial explains how to use libLAS to read and write LIDAR data encoded in LAS File Format. For more examples in C++, check source of unit tests package for libLAS.
NOTE: This tutorial needs to be extended with more detailed instructions and examples.
Reading
- Include required header files from libLAS and C++ Standard Library
#include <liblas/laspoint.hpp> #include <liblas/lasreader.hpp> #include <fstream> // std::ifstream #include <iostream> // std::cout
- Create input stream and associate it with .las file opened to read in binary mode
std::ifstream ifs; ifs.open("file.las", std::ios::in | std::ios::binary);
- Instantiate LAS file reader attached to the input stream object
liblas::LASReader reader(ifs);
- After the reader has been created, you can access members of the Public Header Block
liblas::LASHeader const& header = reader.GetHeader(); std::cout << "Signature: " << header.GetFileSignature() << '\n'; std::cout << "Points count: " << header.GetPointRecordsCount() << '\n';
It's correct to assume that after successful instantiation of reader, public header block is automatically accessible. In other words, there never be a reader that can not serve a header data.
- And, iterate through point records
while (reader.ReadNextPoint()) { liblas::LASPoint const& p = reader.GetPoint(); std::cout << p.GetX() << ", " << p.GetY() << ", " << p.GetZ() << "\n"; }
Writing
- Include required header files from libLAS and C++ Standard Library
#include <liblas/laspoint.hpp> #include <liblas/laswriter.hpp> #include <fstream> // std::ifstream #include <iostream> // std::cout
- Create output stream and associate it with .las file opened to write data in binary mode
std::ofstream ofs; ofs.open("file.las", ios::out | ios::binary);
- Create instance of LASHeader class to define Public Header Block and set some values
liblas::LASHeader header; header.SetDataFormatId(LASHeader::ePointFormat1); // fill other header members
Note, the default constructed header object can be used as perfectly valid header. It will define LAS file according to LAS 1.0 and Point Data Format 0.
- Create LAS file writer object attached to the output stream and the based on the header object.
liblas::LASWriter writer(ofs, header); // here the header has been serialized to disk into the *file.las*
- Now, you can write some point records
liblas::LASPoint point; point.SetCoordinates(10, 20, 30); // fill other properties of point record writer.WritePoint(point);
Here, one point is written to the file.las.
Copying .las file
Below, two simple examples present how to rewrite content from one LAS file to another, in two different ways.
Using interface of LASReader and LASWriter classes
#include <liblas/lasreader.hpp> #include <liblas/laswriter.hpp> #include <exception> #include <iostream> int main() { try { std::ifstream ifs("input.las", ios::in | ios::binary); std::ofstream ofs("output.las", ios::out | ios::binary); liblas::LASReader reader(ifs); liblas::LASWriter writer(ofs, reader.GetHeader()); while (reader.ReadNextPoint()) { writer.WritePoint(reader.GetPoint()); } } catch (std::exception const& e) { std::cerr << "Error: " << e.what() << std::endl; } }
Using iterator classes defined for reader and writer
The library provides two iterators compatible with iterator concept defined in C++ Standard Template Library:
- lasreader_iterator - (input iterator) used to read data from LAS file attached to LASReader object
- laswriter_iterator - (output iterator) used to write data into LAS file attached to LASWriter object.
#include <liblas/lasreader.hpp> #include <liblas/laswriter.hpp> #include <liblas/iterator.hpp> #include <algorithm> #include <exception> #include <iostream> using namespace liblas; int main() { try { std::ifstream ifs("input.las", ios::in | ios::binary); std::ofstream ofs("output.las", ios::out | ios::binary); LASReader reader(ifs); LASWriter writer(ofs, reader.GetHeader()); std::copy(lasreader_iterator(reader), lasreader_iterator(), laswriter_iterator(writer)); } catch (std::exception const& e) { std::cerr << "Error: " << e.what() << std::endl; } }
