一、LAStools / LASlib

LAStools官网

LAStools的Github地址

二、读写LAS

C/C++——常规——附加包含目录——添加include目录

链接器——输入——附加依赖项——添加...\LASlib.lib

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

2.1 读取LAS信息

//LASlib
#include "LASlib/inc/lasreader.hpp"

/* read las */
void TestLASlib::ReadLas(std::vector<xjPoint> &vPoints, const std::string &sLasPath)
{
	LASreadOpener lasReadOpener;
	//lasReadOpener.set_file_name(qstringLasPath.toLocal8Bit().toStdString().c_str());
	lasReadOpener.set_file_name(sLasPath.c_str());

	LASreader* lasReader = lasReadOpener.open(false);

#pragma region Las information
	int majorVersion = lasReader->header.version_major;
	int minorVersion = lasReader->header.version_minor;
	int pointDataFormat = lasReader->header.point_data_format;
	int pointAmount = lasReader->header.number_of_point_records;
	double maxX = lasReader->header.max_x;
	double minX = lasReader->header.min_x;
	double maxY = lasReader->header.max_y;
	double minY = lasReader->header.min_y;
	double maxZ = lasReader->header.max_z;
	double minZ = lasReader->header.min_z;
	double deltaX = maxX - minX;
	double deltaY = maxY - minY;
	double deltaZ = maxZ - minZ;
	double xOffset = lasReader->header.x_offset;
	double yOffset = lasReader->header.y_offset;
	double zOffset = lasReader->header.z_offset;
	double xScale = lasReader->header.x_scale_factor;
	double yScale = lasReader->header.y_scale_factor;
	double zScale = lasReader->header.z_scale_factor;

	ui.listWidget->addItem("majorVersion = " + QString::number(majorVersion));
	ui.listWidget->addItem("minorVersion = " + QString::number(minorVersion));
	ui.listWidget->addItem("pointDataFormat = " + QString::number(pointDataFormat));
	ui.listWidget->addItem("number_of_point_records = " + QString::number(pointAmount));
	ui.listWidget->addItem("maxX = " + QString::number(maxX, 'f', 5) +
		", minX = " + QString::number(minX, 'f', 5) +
		", deltaX = " + QString::number(deltaX, 'f', 5));
	ui.listWidget->addItem("maxY = " + QString::number(maxY, 'f', 4) +
		", minY = " + QString::number(minY, 'f', 4) +
		", deltaY = " + QString::number(deltaY, 'f', 4));
	ui.listWidget->addItem("maxZ = " + QString::number(maxZ, 'f', 4) +
		", minZ = " + QString::number(minZ, 'f', 4) +
		", deltaZ = " + QString::number(deltaZ, 'f', 4));
	ui.listWidget->addItem("xOffset = " + QString::number(xOffset, 'f', 4));
	ui.listWidget->addItem("y_offset = " + QString::number(yOffset, 'f', 4));
	ui.listWidget->addItem("z_offset = " + QString::number(zOffset, 'f', 4));
	ui.listWidget->addItem("x_scale_factor = " + QString::number(xScale, 'f', 4));
	ui.listWidget->addItem("y_scale_factor = " + QString::number(yScale, 'f', 4));
	ui.listWidget->addItem("z_scale_factor = " + QString::number(zScale, 'f', 4));
#pragma endregion

	int pNumber = 0;
	while (lasReader->read_point())
	{
		LASpoint& pointReader = lasReader->point;

		xjPoint p;
		p.x = pointReader.get_x();
		p.y = pointReader.get_y();
		p.z = pointReader.get_z();
		p.intensity = pointReader.get_intensity();
		p.pointSourceID = pointReader.get_point_source_ID();
		p.GPStime = pointReader.get_gps_time();
		p.r = pointReader.get_R();
		p.g = pointReader.get_G();
		p.b = pointReader.get_B();
		p.classification = pointReader.get_classification();

		vPoints.push_back(p);
		pNumber++;

#pragma region point information
		if (pNumber < 5)
		{
			QString pointInfo = QString::number(p.x, 'f', 4) + ", " +
				QString::number(p.y, 'f', 4) + ", " + QString::number(p.z, 'f', 4) + ", ";
			pointInfo += QString::number(p.intensity) + ", " +
				QString::number(p.pointSourceID) + ", " + QString::number(p.GPStime, 'f', 4) + ", ";
			pointInfo += QString::number(p.r) + ", " +
				QString::number(p.g) + ", " + QString::number(p.b) + ", ";
			pointInfo += QString::number(p.classification);
			ui.listWidget->addItem(pointInfo);
		}
#pragma endregion
	}

	lasReader->close();
	delete lasReader;
	lasReader = nullptr;
}

2.2 保存LAS文件 

//LASlib
#include "LASlib/inc/laswriter.hpp"

/* write las */
bool TestLASlib::WriteLas(std::vector<xjPoint> &vPoints, const std::string & sResultPath)
{
	// init
	LASwriteOpener lasWriterOpener;
    //lasWriterOpener.set_file_name(qstringResPath.toLocal8Bit().toStdString().c_str());
    lasWriterOpener.set_file_name(sResultPath.data());

	// init header
	LASheader lasHeader;
	lasHeader.x_scale_factor = 0.0001;
	lasHeader.y_scale_factor = 0.0001;
	lasHeader.z_scale_factor = 0.0001;
	lasHeader.x_offset = (int)vPoints.at(0).x;
	lasHeader.y_offset = (int)vPoints.at(0).y;
	lasHeader.z_offset = (int)vPoints.at(0).z;
	lasHeader.point_data_format = 3;
	lasHeader.point_data_record_length = 34;
	//lasHeader.number_of_point_records = vPoints.size();

	// open laswriter
	LASwriter* lasWriter = lasWriterOpener.open(&lasHeader);

	// init point
	LASpoint lasPoint;
	lasPoint.init(&lasHeader, lasHeader.point_data_format, lasHeader.point_data_record_length, 0);

	// write points
	double minX = DBL_MAX, minY = DBL_MAX, minZ = DBL_MAX;
	double maxX = -DBL_MAX, maxY = -DBL_MAX, maxZ = -DBL_MAX;
	for (int i = 0; i < vPoints.size(); i++)
	{
		// populate the point
		lasPoint.set_x(vPoints.at(i).x);
		lasPoint.set_y(vPoints.at(i).y);
		lasPoint.set_z(vPoints.at(i).z);
		lasPoint.set_intensity(vPoints.at(i).intensity);
		lasPoint.set_point_source_ID(vPoints.at(i).pointSourceID);
		lasPoint.set_gps_time(vPoints.at(i).GPStime);
		lasPoint.set_R(vPoints.at(i).r);
		lasPoint.set_G(vPoints.at(i).g);
		lasPoint.set_B(vPoints.at(i).b);
		lasPoint.set_classification(vPoints.at(i).classification);

		// write the point
		lasWriter->write_point(&lasPoint);

		// add it to the inventory
		lasWriter->update_inventory(&lasPoint);

		//range
		double x = vPoints.at(i).x;
		double y = vPoints.at(i).y;
		double z = vPoints.at(i).z;
		if (x < minX) minX = x;
		if (x > maxX) maxX = x;
		if (y < minY) minY = y;
		if (y > maxY) maxY = y;
		if (z < minZ) minZ = z;
		if (z > maxZ) maxZ = z;
	}

	// update the boundary
	lasHeader.set_bounding_box(minX, minY, minZ, maxX, maxY, maxZ);

	// update the header
	lasWriter->update_header(&lasHeader, true);

	// close the writer
	lasWriter->close();
	delete lasWriter;
	lasWriter = nullptr;
}

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐