Monday, 17 August 2015

lidar - Georeferencing las files using liblas


I am trying to write georeferencing information to a '.las' file using liblas and c++.


Here is my code:


   ofstream ofs;
ofs.open("test.las", ios::out | ios::binary);
liblas::Header header;
liblas::Writer writer(ofs, header);

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


writer.WritePoint(point);
header.SetPointRecordsCount(1);

liblas::SpatialReference srs;
srs.SetFromUserInput("EPSG:4326");
header.SetSRS(srs);
writer.SetHeader(header);
writer.WriteHeader();


But, when I check the file using lasinfo tool I get:


 Spatial Reference:           None

If I check proj4, the information is stored, so I don't know why lasinfo doesn't print it:


Code  : std::cout << srs.GetProj4() << std::endl;
Output: +proj=longlat +datum=WGS84 +no_defs

However, when I use lasinfo tool with others las files not written by me I can see the WKT and geotiff output.


What am I doing wrong?



Answer




If you want to write a lasfile with SRS information, you must create your liblas::Writer using a liblas::Header that has a defined SRS. Change your code to this:


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


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

writer.WritePoint(point);
writer.WriteHeader();


libLAS's header writer implementation does not modify any VLRs when in append mode. libLAS also writes the header immediately upon creating a new writer, which ensures that all subsequent calls to Header::write() are considered to be in append mode. Therefore, you must set up your header with the appropriate spatial reference system (and other VLRs) before creating the writer.


IMO this could be a bug. If you feel the same way, open an issue on libLAS and see what they have to say.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...