[LASlib] Change coordinate and the offset

110 views
Skip to first unread message

Jérôme Chatillon

unread,
Aug 21, 2015, 10:14:17 AM8/21/15
to LAStools - efficient tools for LiDAR processing

Hi everyone,

I'm unsing the LASlib to read and write las files.

I would like to apply a coordinate transformation and also use a different xyz offset for the output las file. how to properly do it ?

The following code work as long as the offset is not changed:
LASreadOpener lasreadopener;
lasreadopener.set_file_name( "input.las" );
LASreader* lasreader = lasreadopener.open();
LASwriteOpener laswriteopener;
laswriteopener.set_file_name( "output.las" );
LASwriter* laswriter = laswriteopener.open(&lasreader->header);

unsigned int count = 0;
double oY,iZ,oZ;
while (lasreader->read_point())
{
    transform(lasreader->point.get_x(), lasreader->point.get_y(), lasreader->point.get_z(), &oX, &oY, &oZ);
    lasreader->point.set_x(oX);
    lasreader->point.set_y(oY);
    lasreader->point.set_z(oZ);
    laswriter->write_point(&lasreader->point);
    laswriter->update_inventory(&lasreader->point);
    count++;
}

laswriter->update_header(&lasreader->header, TRUE);
laswriter->close(true);
delete laswriter;
lasreader->close(true);
delete lasreader;

Thanks
How can I change the output file offset at the same time ?

Martin Isenburg

unread,
Aug 25, 2015, 12:08:20 AM8/25/15
to LAStools - efficient command line tools for LIDAR processing
Hello,

you can find an example in the open source code of las2las.cpp as this becomes necessary whenever reprojecting from one CRS to another. Below the distilled rundown that should work (minus whatever bugs this untested pseudo code has).

Regards,

Martin @rapidlasso

============

// open lasreader
LASreader* lasreader = lasreadopener.open();

// create new header for output
LASheader header = lasreader->header;

// zero the pointers of the old header so they don't get deallocated twice
lasreader->header.unlink();

// transform the center of bounding box to new position
F64 point[3];
point[0] = (header.min_x +  header.max_x)/2;
point[1] = (header.min_y +  header.max_y)/2;
point[2] = (header.min_z +  header.max_z)/2;;
transform(point, point);

// use position to create better suited offsets
header.x_offset = ((I64)((point[0]/header.x_scale_factor)/10000000))*10000000*header.x_scale_factor;
header.y_offset = ((I64)((point[1]/header.y_scale_factor)/10000000))*10000000*header.y_scale_factor;
header.z_offset = ((I64)((point[2]/header.z_scale_factor)/10000000))*10000000*header.z_scale_factor;

// open laswriter
LASwriter* laswriter = laswriteopener.open(&header);

// read and write points
while (lasreader->read_point())
{
   lasreader->point.compute_coordinates();
   transform(lasreader->point.coordinates, lasreader->point.coordinates);
   // create XYZ quantized with new quantizer
   lasreader->point.compute_XYZ(&header);
   laswriter->write_point(&lasreader->point);
   laswriter->update_inventory(&lasreader->point);
}

// update header and destroy writer
laswriter->update_header(&lasreader->header, TRUE);
laswriter->close();
delete laswriter;

// destroy reader
lasreader->close();
delete lasreader;


Jérôme Chatillon

unread,
Aug 26, 2015, 3:00:28 AM8/26/15
to LAStools - efficient tools for LiDAR processing

Hi Martin,

I did try to copy the header and modify it, but i didn't know about the unlink() function: brilliant :)
It's all working as I expected.

Merci :)

Jérôme Chatillon

Martin Isenburg

unread,
Aug 26, 2015, 3:10:37 AM8/26/15
to LAStools - efficient command line tools for LIDAR processing
I guess you saw the bug:

[...]
// update header and destroy writer
laswriter->update_header(&lasreader->header, TRUE);
[...]

should be

[...]
// update header and destroy writer
laswriter->update_header(&header, TRUE);
[...]

Martin

--
http://rapidlasso.com - fast code to transform your LiDARs

--
Reply all
Reply to author
Forward
0 new messages