解析las⽂件_点云las⽂件读取
由于⼤多数点云⽂件都是las格式的,所以有必要解析下此⽂件格式并提取出需要的点云坐标信息。
las格式是ASPRS下属的LIDAR委员会制定了LIDAR数据的标准交换格式,较好地顾及了LIDAR数据的特点,其结构合理,便于扩展。从本质上来说,LAS格式是⼀种⼆进制⽂件格式。⼀个符合LAS标准的LIDAR⽂件分为三个部分:公⽤⽂件头块(PUBLICHEADERBLOCK)、变量长度记(VARIABLELENGTHRECORDS)和点数据记录(POINTDATARECORD)。
las⽬前有好⼏种版本,但主要都包含上述三个部分,此程序主要参考1.2版本。las1.2版本介绍可从
1. 结构体定义:
1.1 PUBLICHEADERBLOCK:
公共头⽤来记录数据集的基本信息,包括LIDAR点总数、数据范围、LIDAR点格式、变长记录总数、LAS⽂件的⽣成⽅式等。
struct public_Header_Block
{
char file_signature[4];
unsigned short file_source_id;
unsigned short global_encoding;
unsigned long project_id_guid_data1;
unsigned short project_id_guid_data2;
unsigned short project_id_guid_data3;
unsigned char project_id_guid_data4[8];
unsigned char version_major;
unsigned char version_minor;
char systemID[32];
char generatingSoftware[32];
unsigned short fileCreationDay;
unsigned short fileCreationYear;
unsigned short headerSize;
unsigned long pointDataOffset;
unsigned long numberOfVariableRecords;
unsigned char pointDataFormatID;
unsigned short pointDataRecordLength;
unsigned long numberOfPointRecords;
unsigned long numberOfPointsByReturn[5];
double x_scale;
double y_scale;
double z_scale;
double x_offset;
double y_offset;
double z_offset;
double x_max;
double x_min;
double y_max;
double y_min;
double z_max;
double z_min;
};
1.2. VARIABLELENGTHRECORDS:
变长记录⽤来记录数据的投影信息、元数据信息以及⽤户⾃定义信息等,是LAS格式中最灵活的部分。每条变长记录包括固定的变长记录头和灵活的扩展域两部分。⼀条变长记录的长度=变长记录头长度+扩展域长度。
struct variable_Length_Record_Header
{
unsigned short reserved;
char user_ID[16];
unsigned short record_ID;
unsigned short record_Length_After_Header;
char description[32];
};
1.3. POINTDATARECORD:
点集记录部分保存了⼤量的L IDAR脚点信息,LAS⽀持的LIDAR点记录格式有100种,从Format0-Format99。在同⼀个LAS⽂件中,只有⼀种L IDAR点格式。⽽且要与公共头中的点格式⼀致。
struct point_Data_Record
{
long x;
long y;
long z;
unsigned short intensity;
unsigned char return_Number;//返回相关
unsigned char classification;//分类相关
unsigned char scanAngleRank;
unsigned char userData;
unsigned short pointSourceID;
double time; //GPS Time
unsigned short r;
unsigned short g;
unsigned short b;
};
2. 读取类函数定义:
使⽤c++对las⽂件读取,等于读取⼀个⼆进制⽂件,其中主要使⽤⼆进制⽂件读取的两个函数:infname.seekg()和ad(),前⼀个函数主要⽤于定位指针到指定位置,后者则按数据类型读取数据。
如下⾯⼏⾏代码为读取⽂件信息头⾥⾯第⼀个符号⽂件标志,如果成功应该为“LASF’’.
string str = filename + ".las";
ifstream infname(str, ios::binary | ios::in);
infname.seekg(0, ios::beg);
......
class ReadLas
{
public:
void read_Header();
void out_Header();
void read_var_Length_Record();
void out_var_Length_Record();
void read_Point_Data_Record();
void output_PointData(string outname);
public_Header_Block header;
vector variableRecord;
vector pointdataRecord;
vector laspoint;
string filename;
};
3. 源码:
3.1 Read Public Header Block:void ReadLas::read_Header()
{
string str = filename + ".las";
ifstream infname(str, ios::binary | ios::in);
infname.seekg(0, ios::beg);
string project_id_guid_data4 = (char*)header.project_id_guid_data4;
}
3.2 read in variable length records:void ReadLas::read_var_Length_Record()
{
string str = filename + ".las";
ifstream infname(str, ios::binary | ios::in);
infname.seekg(header.headerSize, ios::beg);
int num = header.numberOfVariableRecords;
for (int i = 0; i < num; i++)
{
variable_Length_Record_Header var_Len_Record;
variableRecord.push_back(var_Len_Record);
infname.seekg(var_d_Length_After_Header, ios::cur);
}
};
3.3 read point data record:void ReadLas::read_Point_Data_Record()
{
int c = header.pointDataOffset;
int n = header.numberOfPointRecords;
int formatlength;
switch (header.pointDataFormatID)
{
case 0:
formatlength = 20;
break;
case 1:
formatlength = 28;
break;
case 2:
formatlength = 26;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论