libLAS:读写点云

libLAS:读写点云https liblas org https github com libLAS libLAS libLAS 是一个 C C 库 有 python 库 liblas 用于读写 LAS 格式的点云 ASPRS LAS 格式是一种顺序二进制文件格式

大家好,我是讯享网,很高兴认识大家。

https://liblas.org/

https://github.com/libLAS/libLAS

libLAS是一个C/C ++库(有python库 liblas),用于读写LAS格式的点云。ASPRS LAS格式是一种顺序二进制文件格式,用于存储来自LiDAR传感器以及LiDAR处理软件的数据以进行数据交换和存档。libLAS支持ASPRS LAS格式规范版本:1.0、1.1、1.2和1.3(基本支持)。

虽然libLAS已经被PDAL取代,但是不可否认,它是一个很nice的库。

libLAS库可以通过OSGeo4W下载,也可以自己编译。


讯享网

一、配置环境

分享给有需要的人,代码质量勿喷。

二、读取点云并存储

//libLAS #include "liblas\liblas.hpp" /* read las */ void libLAStest::ReadLas(std::vector<xjPoint> &vPoints, const std::string &xjLasPath) { std::ifstream ifs; ifs.open(xjLasPath, std::ios::in | std::ios::binary); liblas::ReaderFactory factory; liblas::Reader xjReader = factory.CreateWithStream(ifs); liblas::Header const& xjHeader = xjReader.GetHeader(); #pragma region QStringList xjInfo; //count int pCount = xjHeader.GetPointRecordsCount(); xjInfo.append("points count: " + QString::number(pCount)); //range double maxX = xjHeader.GetMaxX(); double minX = xjHeader.GetMinX(); double maxY = xjHeader.GetMaxY(); double minY = xjHeader.GetMinY(); double maxZ = xjHeader.GetMaxZ(); double minZ = xjHeader.GetMinZ(); double delatX = maxX - minX; double delatY = maxY - minY; double delatZ = maxZ - minZ; xjInfo.append("range X: " + QString::number(delatX)); xjInfo.append("range Y: " + QString::number(delatY)); xjInfo.append("range Z: " + QString::number(delatZ)); xjInfo.append("-----------"); //first point's information xjInfo.append("first point's information:"); if (xjReader.ReadPointAt(0)) { liblas::Point const & p = xjReader.GetPoint(); xjInfo.append("X = " + QString::number(p.GetX(), 'f', 4)); xjInfo.append("Y = " + QString::number(p.GetY(), 'f', 4)); xjInfo.append("Z = " + QString::number(p.GetZ(), 'f', 4)); xjInfo.append("Red = " + QString::number(p.GetColor().GetRed() >> 8)); xjInfo.append("Green = " + QString::number(p.GetColor().GetGreen() >> 8)); xjInfo.append("Blue = " + QString::number(p.GetColor().GetBlue() >> 8)); xjInfo.append("Intensity = " + QString::number(p.GetIntensity())); xjInfo.append("PointSourceID = " + QString::number(p.GetPointSourceID())); xjInfo.append("GPStime = " + QString::number(p.GetTime())); } ui.listWidget->addItems(xjInfo); #pragma endregion //while( xjReader.ReadNextPoint() ) //{ // liblas::Point const & p =xjReader.GetPoint(); // //... //} for (unsigned long i = 0; i < pCount; i++) { if (xjReader.ReadPointAt(i)) { liblas::Point const & p = xjReader.GetPoint(); xjPoint xjP; xjP.x = p.GetX(); xjP.y = p.GetY(); xjP.z = p.GetZ(); xjP.r = p.GetColor().GetRed(); xjP.g = p.GetColor().GetGreen(); xjP.b = p.GetColor().GetBlue(); xjP.intensity = p.GetIntensity(); xjP.pointSourceID = p.GetPointSourceID(); xjP.GPStime = p.GetTime(); vPoints.push_back(xjP); } } }

讯享网

三、写las

讯享网//libLAS #include <liblas/liblas.hpp> /* write las */ void libLAStest::WriteLas(const std::string &xjLasResultPath, std::vector<xjPoint> &vPoints) { std::ofstream ofs; if (!liblas::Create(ofs, xjLasResultPath)) return; int xoffset = int(vPoints.at(0).x); int yoffset = int(vPoints.at(0).y); int zoffset = int(vPoints.at(0).z); liblas::Header header; header.SetVersionMajor(1); header.SetVersionMinor(2); header.SetOffset(xoffset, yoffset, zoffset); header.SetDataFormatId(liblas::ePointFormat3); header.SetPointRecordsCount(vPoints.size()); header.SetScale(0.0001, 0.0001, 0.0001); liblas::Writer writer(ofs, header); double Max_x = vPoints.at(0).x, Min_x = vPoints.at(0).x; double Max_y = vPoints.at(0).y, Min_y = vPoints.at(0).y; double Max_z = vPoints.at(0).z, Min_z = vPoints.at(0).z; for (int i = 0; i < vPoints.size(); i++) { xjPoint p = vPoints.at(i); liblas::Point point; point.SetRawX((p.x - xoffset) * 10000); point.SetRawY((p.y - yoffset) * 10000); point.SetRawZ((p.z - zoffset) * 10000); liblas::Color color; color.SetRed(p.r); color.SetGreen(p.g); color.SetBlue(p.b); point.SetColor(color); point.SetIntensity(p.intensity); point.SetPointSourceID(p.pointSourceID); point.SetTime(p.GPStime); writer.WritePoint(point); if (p.x > Max_x)Max_x = p.x; if (p.y > Max_y)Max_y = p.y; if (p.z > Max_z)Max_z = p.z; if (p.x < Min_x)Min_x = p.x; if (p.y < Min_y)Min_y = p.y; if (p.z < Min_z)Min_z = p.z; } header.SetMax(Max_x, Max_y, Max_z); header.SetMin(Min_x, Min_y, Min_z); writer.SetHeader(header); writer.WriteHeader(); ofs.close(); }

四、button功能

void libLAStest::on_btnLas_clicked() { QString xjLasPath = QFileDialog::getOpenFileName(this, "select LAS", "D:/", "*.las"); ui.lineEdit->setText(xjLasPath); std::vector<xjPoint> vPoints; ReadLas(vPoints, xjLasPath.toStdString()); QString xjSaveLasPath = QCoreApplication::applicationDirPath() + "/save.las"; WriteLas(xjSaveLasPath.toStdString(), vPoints); ui.listWidget->addItem("-------------"); ui.listWidget->addItem("the new las file have save to"); ui.listWidget->addItem(xjSaveLasPath); }

小讯
上一篇 2025-04-02 23:56
下一篇 2025-04-05 19:08

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/95565.html