LibLas学习笔记

LibLas学习笔记

什么是Las格式

LAS文件格式是数据用户之间交换三维点云数据的公共文件格式。
虽然这种格式主要用于交换激光雷达点云数据,但是它支持交换任何三维的x、y、z 数组。
这种二进制文件格式可以替代私有系统或许多公司使用的通用ASCII文件交换系统。私有系统的问题很明显,因为数据不能轻易地从一个系统转移到另一个系统。
ASCII文件交换有两个主要问题:

  1. 性能问题: 因为ASCII高程数据的读取和解释可能非常慢,即使对于少量数据,文件大小也可能非常大。
  2. 所有与激光雷达数据相关的信息都会丢失。

Las文件的优势

LAS文件格式是一种二进制文件格式,它维护特定于数据的激光雷达性质的信息,同时不会过于复杂。

Las格式介绍

参考链接

该格式包含由公共头块组成的二进制数据,任意数量的(可选)可变长度记录(vlrs),点数据记录,任意数量(可选)可扩展的可变长度记录。
所有数据是小顶端的格式

公共头文件包含的数据内容:
1. 一般的数据,点的个数和点的边界。

可变长度包含的数据类型:
1. 投影信息
2. 元数据
3. 波形包信息数据
4. 用户应用数据

可扩展的可变长度数据: 可以追加在LAS文件的末尾。

LAS文件数据格式定义
LAS文件数据格式定义

头文件格式的定义:
头文件的内容

使用liblas库

  1. 包含所需的头文件和C++标准库
#include <liblas/liblas.hpp>
#include <fstream>  // std::ifstream
#include <iostream> // std::cout
  1. 为las文件创建输入流,使用binary模式
  1. std::ifstream ifs; 
  2. ifs.open("file.las", std::ios::in | std::ios::binary); 
  1. 创建ReaderFactory和实例化一个liblas::Reader使用流
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
  1. 读取liblas头文件中的内容
liblas::Header const& header = reader.GetHeader();

std::cout << "Compressed: " << (header.Compressed() == true) ? "true":"false";
std::cout << "Signature: " << header.GetFileSignature() << '
';
std::cout << "Points count: " << header.GetPointRecordsCount() << '
';
  1. 遍历点的数据
while (reader.ReadNextPoint())
{
    liblas::Point const& p = reader.GetPoint();

    std::cout << p.GetX() << ", " << p.GetY() << ", " << p.GetZ() << "
";
}
  1. 随机访问数据
    注意:如果是压缩的las数据,会影响性能。如果是批量读取数据请使用Seek方法
reader.ReadPointAt(2);
liblas::Point const& p = reader.GetPoint();

使用las库写数据

Include required header files from libLAS and C++ Standard Library

#include <liblas/liblas.hpp>

#include <fstream>  // std::ofstream
#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);
Note It is also possible to open the stream in append mode to add data to an existing file. You should first instantiate a liblas::Reader and fetch the liblas::Header from the file, and then create the liblas::Writer with that header and the ofstream opened in append mode.

liblas::Header header = reader.GetHeader();

std::ios::openmode m = std::ios::out | std::ios::in | std::ios::binary | std::ios::ate;
ofs.open("file.las", m);
liblas::Writer writer(ofs, header);

Create instance of liblas::Header class to define Public Header Block and set some values

liblas::Header header;
header.SetDataFormatId(liblas::ePointFormat1); // Time only

// Set coordinate system using GDAL support
liblas::SpatialReference srs;
srs.SetFromUserInput("EPSG:4326");

header.SetSRS(srs);

// fill other header members
Note Simply setting header.SetCompressed(true) on the header will be sufficient to output a compressed file when the liblas::Writer is created if LASzip support is enabled in libLAS, but it is up to the user to specify the proper file name extension, .laz, when writing the file.
Note The default constructed header object can be used as perfectly valid header. It will define LAS file according to LAS 1.2 and Point Data Format 3.
Create LAS file writer object attached to the output stream and the based on the header object.

liblas::Writer writer(ofs, header);
// here the header has been serialized to disk into the file.las
Write some point records

liblas::Point point(&header);
point.SetCoordinates(10, 20, 30);
// fill other properties of point record

writer.WritePoint(point);

参考链接

原文地址:https://www.cnblogs.com/sweetdark/p/9335794.html