1

I found an answer that uses lastools, but unfortunately, I use a MacOS laptop, so using lastools which has its binaries in .exe format cannot be executed without some other extensions.

I'd like to shift a las file by a certain lon and lat using laspy, I've tried to update xyz and XYZ, but it doesn't seem to shift it properly. (I know this because the shifted lasfile does not align with some other layer which has been correctly georeferenced. In fact, the alignment is way off, to the point the layers don't even intersect.

Here're my codes in python, could someone give some hints/ help out in this aspect?

import laspy


shift_lat = 103.83964917-103.83963837  # Shift in latitude
shift_lon = 1.35651542-1.35653912 # Shift in longitude



# Read the input LAS file
input_file = <input>
output_file = <output>

las = laspy.read(input_file)

# Apply the shift directly to the real-world coordinates
x_shifted = las.x.copy() + shift_lon
y_shifted = las.y.copy() + shift_lat

# Create a new LAS object with the same header
output = laspy.LasData(las.header)

# Update the LAS object with the shift
output.x = x_shifted
output.y = y_shifted
output.z = las.z.copy()

# Write the shifted LAS file
output.write(output_file)

When I view the file on QGIS also, I only can see the Z values.

I am a student and have no background in geographical systems.

I tried to use offsets but to no avail.

input_las = laspy.read(input_file)
print("old offsets: ")
print(input_las.header.offsets)
output = laspy.LasData(header=input_las.header)
output.X = input_las.X
output.Y = input_las.Y 
output.Z = input_las.Z 

output.header.offsets = (
  input_las.header.offsets[0] + shift_lat,
  input_las.header.offsets[1] + shift_lon, 
  input_las.header.offsets[2] 
)

output.write(output_file)

print("new offsets: ")
print(output.header.offsets)
4
  • 1
    You don't need to update all the records for a constant offset, LAS (and LAZ) files store their coordinates as (long) integers and use a scale/offset in the header, to convert to floating point X, Y and Z, just change the value in the header asprs.org/wp-content/uploads/2010/12/LAS_1_4_r13.pdf#page=5 this can be done easily in python with binary file I/O. Commented Jul 29 at 6:16
  • Hi @MichaelStimson, thank you for your reply, I've tried to do it by replacing the header offsets with the new offsets, however, I am still getting only Z values shown on QGIS, do you have any idea why? Commented Jul 30 at 4:20
  • I would skip laspy and write directly using standard binary file I/O, seek to the location, read to byte array, use struct.unpack docs.python.org/3/library/struct.html to unpickle the byte array to doubles, modify the origin values and seek back to the location and write the bytes pickled again using struct.pack. Double check your offset values though, it's not that common to have LAS in a geographical XY coordinate system. Commented Jul 30 at 4:31
  • Can you post a sample of what you are seeing? Before - After
    – Cary H
    Commented Jul 30 at 11:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.