PDAL:读写点云

PDAL读取与保存点云文件,笔者感觉与libLAS具有很大不同。

1 配置环境

1.1 属性——C/C++——常规——附加包含目录,添加头文件路径C:\OSGeo4W64\include

PDAL:读写点云_第1张图片

1.2 属性——链接器——输入——附加依赖项,添加lib文件C:\OSGeo4W64\lib\pdalcpp.libC:\OSGeo4W64\lib\pdal_util.lib

PDAL:读写点云_第2张图片

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

2.1 头文件

//PDAL
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

2.2 读取点云信息

void PDALtest::ReadPC()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	pdal::Option las_opt("filename", "E:/test.las");//"filename"(键)
	pdal::Options las_opts;
	las_opts.add(las_opt);

	pdal::PointTable table;
	pdal::LasReader las_reader;
	las_reader.setOptions(las_opts);
	las_reader.prepare(table);

	pdal::PointViewSet point_view_set = las_reader.execute(table);
	pdal::PointViewPtr point_view = *point_view_set.begin();
	pdal::Dimension::IdList dims = point_view->dims();
	pdal::LasHeader las_header = las_reader.header();

	//头文件信息
	unsigned int PointCount = las_header.pointCount();
	//char* projstr = table.spatialRef().getWKT(pdal::SpatialReference::eCompoundOK).c_str();
	double scale_x = las_header.scaleX();
	double scale_y = las_header.scaleY();
	double scale_z = las_header.scaleZ();
	double offset_x = las_header.offsetX();
	double offset_y = las_header.offsetY();
	double offset_z = las_header.offsetZ();
	double xmin = las_header.minX();
	double xmax = las_header.maxX();
	double ymin = las_header.minY();
	double ymax = las_header.maxY();
	double zmin = las_header.minZ();
	double zmax = las_header.maxZ();

	//读点
	unsigned int n_features = las_header.pointCount();
	for (pdal::PointId idx = 0; idx < point_view->size(); ++idx)
	{
		double x = point_view->getFieldAs(Id::X, idx);
		double y = point_view->getFieldAs(Id::Y, idx);
		double z = point_view->getFieldAs(Id::Z, idx);
		int Pintensity = point_view->getFieldAs(Id::Intensity, idx);
		int PointSourceID = point_view->getFieldAs(Id::PointSourceId, idx);
		double GPStime = point_view->getFieldAs(Id::GpsTime, idx);
		int red = point_view->getFieldAs(Id::Red, idx);
		int green = point_view->getFieldAs(Id::Green, idx) ;
		int blue = point_view->getFieldAs(Id::Blue, idx);
		int point_class = point_view->getFieldAs(Id::Classification, idx);
	}
}

2.3 保存点云为LAS文件

void PDALtest::WritePC()
{
	using namespace pdal;
	using namespace pdal::Dimension;

	Options xjOptions;
	xjOptions.add("filename", "E:/saved.las");
	
        //xjPoint是自定义点类型;MhPC为点的哈希表
        xjPoint p3d = MhPC.value(0);
	double xoffset, yoffset, zoffset;
	xoffset = floor(p3d.x);
	yoffset = floor(p3d.y);
	zoffset = floor(p3d.z);
	xjOptions.add("offset_x", xoffset);
	xjOptions.add("offset_y", yoffset);
	xjOptions.add("offset_z", zoffset);
	xjOptions.add("scale_x", 0.0001);
	xjOptions.add("scale_y", 0.0001);
	xjOptions.add("scale_z", 0.0001);

	PointTable table;
	table.layout()->registerDim(Dimension::Id::X);
	table.layout()->registerDim(Dimension::Id::Y);
	table.layout()->registerDim(Dimension::Id::Z);
	table.layout()->registerDim(Dimension::Id::Intensity);
	table.layout()->registerDim(Dimension::Id::GpsTime);
	table.layout()->registerDim(Dimension::Id::PointSourceId);
	table.layout()->registerDim(Dimension::Id::Red);
	table.layout()->registerDim(Dimension::Id::Green);
	table.layout()->registerDim(Dimension::Id::Blue);

	PointViewPtr view(new PointView(table));

	for (int i = 0; i < MhPC.size(); i++)
	{
		double x = MhPC.value(i).x;
		double y = MhPC.value(i).y;
		double z = MhPC.value(i).z;
		int intensity = MhPC.value(i).intensity;
		double GPStime = MhPC.value(i).GPStime;
		int PointSourceId = MhPC.value(i).PointSourceID;
		int red = MhPC.value(i).r;
		int green = MhPC.value(i).g;
		int blue = MhPC.value(i).b;

		view->setField(pdal::Dimension::Id::X, i, x);
		view->setField(pdal::Dimension::Id::Y, i, y);
		view->setField(pdal::Dimension::Id::Z, i, z);
		view->setField(pdal::Dimension::Id::Intensity, i, intensity);
		view->setField(pdal::Dimension::Id::GpsTime, i, GPStime);
		view->setField(pdal::Dimension::Id::PointSourceId, i, PointSourceId);
		view->setField(pdal::Dimension::Id::Red, i, static_cast(red));
		view->setField(pdal::Dimension::Id::Green, i, static_cast(green));
		view->setField(pdal::Dimension::Id::Blue, i, static_cast(blue));
	}

	BufferReader xjBufferReader;
	xjBufferReader.addView(view);

	StageFactory factory;
	Stage *writer = factory.createStage("writers.las");
	writer->setInput(xjBufferReader);
	writer->setOptions(xjOptions);
	writer->prepare(table);
	writer->execute(table);
}

3 参考

读:https://pdal.io/api/transition/index.html

写:https://pdal.io/development/writing.html

你可能感兴趣的:(PDAL,读,写,点云,PDAL)