libLAS庫實現las文件的讀寫

       儘管libLAS庫已經沒有維護了。但是還是有些同學在以前的項目中需要用到libLAS庫。下面就簡單的用代碼展示如何使用libLAS庫來讀寫las點雲數據。

(1)讀las文件點雲數據(這個比較簡單,網上有大量的例子):

std::ifstream ifs;
ifs.open("C:\\test.las", std::ios::in | std::ios::binary);
if (!ifs)
{
	cout << "打開失敗!" << endl;
}
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
unsigned long int nbPoints = reader.GetHeader().GetPointRecordsCount();//獲取las數據點的個數
cout << nbPoints << endl;
double x = 0, y = 0, z = 0;
while (reader.ReadNextPoint())//循環讀取las文件中的點
{
	liblas::Point const& laspoint = reader.GetPoint();
	x = laspoint.GetX();
	y = laspoint.GetY();
	z = laspoint.GetZ();
}
ifs.close();

(2)寫las文件點雲數據(網上的一些例子不可用):

//建立存儲文件
ofstream outPt("C:\\Desktop\\mylas.las", ios::out | ios::binary);
if (!outPt.is_open())
{
	return 0;
}
double minPt[3] = { 9999999, 9999999, 9999999 };
double maxPt[3] = { 0, 0, 0 };
// 設置文件頭,點數、格式、縮放因子、偏移量
liblas::Header header;
header.SetVersionMajor(1);
header.SetVersionMinor(2);
header.SetMin(minPt[0], minPt[1], minPt[2]);
header.SetMax(maxPt[0], maxPt[1], maxPt[2]);
header.SetDataFormatId(liblas::PointFormatName::ePointFormat3);
//header.SetOffset(0, 0, 0);
header.SetScale(0.001, 0.001, 0.001);
header.SetPointRecordsCount(100);
liblas::Writer writer(outPt, header);
liblas::Point point(&header);
//寫入點雲
for (int j = 0; j < 100; ++j)
{
	double x = 100 * rand() / (RAND_MAX + 1.0f);
	double y = 100 * rand() / (RAND_MAX + 1.0f);
	double z = 100 * rand() / (RAND_MAX + 1.0f);
	point.SetCoordinates(x, y, z);
	writer.WritePoint(point);
}
writer.SetHeader(header);
outPt.flush();
outPt.close();

       需要注意的是header.SetPointRecordsCount(100);這個函數一定要在寫入點雲數據之前調用,網上的一些例子寫到了後面,導致寫出來的las文件爲空。如:https://www.cnblogs.com/xingzhensun/p/6305802.html等等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章