1

This question already has an answer here:

I am just starting to use libLAS and I had a small issue.

I have next code to generate a las file with georeferenced points:

std::ofstream ofs;
liblas::Header header;
liblas::SpatialReference srs;
srs.SetFromUserInput("EPSG:4326");
header.SetSRS(srs);
header.SetPointRecordsCount(1);
ofs.open("H:\\temp\\test2.las", std::ios::out | std::ios::binary);
liblas::Writer writer(ofs, header);

liblas::Point point(&header);
point.SetCoordinates(1.0, 2.0, 3.0);
writer.WritePoint(point);

point.SetCoordinates(5, 5, 5);
writer.WritePoint(point);

point.SetCoordinates(-1, -1, -1);
writer.WritePoint(point);

header.SetPointRecordsCount(3);

writer.SetHeader(header);

writer.WriteHeader();
ofs.Close();

When I run lasinfo over this file I get: It says that I have 3 points, but my spatial reference is none.

enter image description here

Same code above, but if I comment this line:

//writer.SetHeader(header);

I can see the spatial reference, but the number of points is 1.

enter image description here

Am I doing something wrong?

marked as duplicate by Andre Silva, Midavalo, Fezter, Mapperz May 29 '16 at 23:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In your second scenario (the one without the rewriting of the header), do you keep header.SetPointRecordsCount(1); on line 6 of your code? What happens when you change that to header.SetPointRecordsCount(3); and remove the SetPointRecordsCount() from later (after you finish writing the points)? – Pete Gadomski Aug 20 '15 at 21:56
4

Once you open your writer, you don't need to modify your header at all. The header is written when you create your writer. Change your first call to header.SetPointRecordsCount() to the correct number of points (3) and remove your later calls to header.SetPointRecordsCount(), writer.SetHeader(), and writer.WriteHeader().

Here's your original code, modified to work as you'd expect (at least on my system):

std::ofstream ofs;
liblas::Header header;
liblas::SpatialReference srs;
srs.SetFromUserInput("EPSG:4326");
header.SetSRS(srs);
header.SetPointRecordsCount(3);
ofs.open("H:\\temp\\test2.las", std::ios::out | std::ios::binary);
liblas::Writer writer(ofs, header);

liblas::Point point(&header);
point.SetCoordinates(1.0, 2.0, 3.0);
writer.WritePoint(point);

point.SetCoordinates(5, 5, 5);
writer.WritePoint(point);

point.SetCoordinates(-1, -1, -1);
writer.WritePoint(point);

ofs.close();

Not the answer you're looking for? Browse other questions tagged or ask your own question.