使用LASlib(LASTool)库对LAS文件进行分割

       在实际的项目,有很多时候,我们会遇到一个巨大的las文件,比如7,8个G的大小。这时候,我们需要对这个大的las文件进行分块保存为多个小的las文件。可是按照点的个数进行保存,也可以按照内存的大小保存。实际上也就是对点云的读写过程。具体的代码如下所示:

void lasFileDivision()
{
	LASreadOpener lasreadopener;
	LASwriteOpener laswriteopener;
	//laslib只允许'\\'格式的文件路径。
	std::string lasFile("C:\\test\\test.las");

	int num = 1;
	//打开las文件
	lasreadopener.set_file_name(lasFile.c_str());
	LASreader* lasreader = lasreadopener.open();
	size_t count = lasreader->header.number_of_point_records;
	printf("%d", count);
	//LASpoint *one_point = new LASpoint();
	int loop_time = 0;
	LASwriter* laswriter = nullptr;
	while (lasreader->read_point())  //只让输出前 10 行坐标
	{
        //按照每889000个点一个las文件进行分割
		if (loop_time % 889000 == 0)
		{
                        //每打开一次文件都需要close();否则文件会一直占用。
                        if(laswriter != NULL)
                        {
                            laswriter->close();
                        }
			stringstream ss;
			ss << "C:\\test\\out" << num << ".las";
			string file_name = ss.str();
			laswriteopener.set_file_name(file_name.c_str());

			LASheader writeHeader;
			writeHeader.number_of_point_records = 889000;
            
                        writeHeader.min_x = lasreader->header.min_x;
			writeHeader.max_x = lasreader->header.max_x;
			writeHeader.min_y = lasreader->header.min_y;
			writeHeader.max_y = lasreader->header.max_y;
			writeHeader.min_z = lasreader->header.min_z;
			writeHeader.max_z = lasreader->header.max_z;
			writeHeader.x_scale_factor = lasreader->header.x_scale_factor;
			writeHeader.y_scale_factor = lasreader->header.y_scale_factor;;
			writeHeader.z_scale_factor = lasreader->header.z_scale_factor;;
			writeHeader.x_offset = lasreader->header.x_offset;
			writeHeader.y_offset = lasreader->header.y_offset;
			writeHeader.z_offset = lasreader->header.z_offset;
			writeHeader.point_data_format = 1;
			writeHeader.point_data_record_length = 28;

			laswriter = laswriteopener.open(&writeHeader);
			if (laswriter == 0)
			{
				fprintf(stderr, "ERROR: could not open laswriter\n");
			}
			num++;
		}
		laswriter->write_point(&lasreader->point);
		laswriter->update_inventory(&lasreader->point);
		loop_time++;
	}
	laswriter->update_header(&lasreader->header, TRUE);

	I64 total_bytes = laswriter->close();
	delete laswriter;
	laswriter = nullptr;

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

 

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