Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

Commit

Permalink
Fix LasAppender
Browse files Browse the repository at this point in the history
By not rewritting the vlrs when the appender is closed,
as it might break things if the vlrs we would rewrite don't have the
exact same byter len (which can happen if we remove extra null bytes)
  • Loading branch information
tmontaigu committed Jan 8, 2021
1 parent 09e8c20 commit 99eca68
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
15 changes: 9 additions & 6 deletions pylas/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,12 @@ def read_from(cls, stream: BinaryIO, seekable=True) -> "LasHeader":

return header

def write_to(self, stream: BinaryIO) -> None:
def write_to(self, stream: BinaryIO, write_vlrs: bool = True) -> None:
little_endian = "little"
with io.BytesIO() as tmp:
self.vlrs.write_to(tmp)
vlr_bytes = tmp.getvalue()
if write_vlrs is True:
with io.BytesIO() as tmp:
self.vlrs.write_to(tmp)
vlr_bytes = tmp.getvalue()

stream.write(LAS_FILE_SIGNATURE)
stream.write(self.file_source_id.to_bytes(2, little_endian, signed=False))
Expand Down Expand Up @@ -538,7 +539,8 @@ def write_to(self, stream: BinaryIO) -> None:
)

header_size = LAS_HEADERS_SIZE[str(self.version)]
self.offset_to_point_data = header_size + len(vlr_bytes)
if write_vlrs is True:
self.offset_to_point_data = header_size + len(vlr_bytes)

stream.write(header_size.to_bytes(2, little_endian, signed=False))
stream.write(self.offset_to_point_data.to_bytes(4, little_endian, signed=False))
Expand Down Expand Up @@ -598,7 +600,8 @@ def write_to(self, stream: BinaryIO) -> None:
)
)

stream.write(vlr_bytes)
if write_vlrs is True:
stream.write(vlr_bytes)

def _sync_extra_bytes_vlr(self) -> None:
try:
Expand Down
6 changes: 5 additions & 1 deletion pylas/lasappender.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def _write_evlrs(self) -> None:
def _write_updated_header(self) -> None:
pos = self.dest.tell()
self.dest.seek(0, io.SEEK_SET)
self.header.write_to(self.dest)
# we don't want to rewrite the vlrs
# as written vlrs may not have to exact
# same number of bytes (e.g. if we remove
# extra spurious null bytes)
self.header.write_to(self.dest, write_vlrs=False)
self.dest.seek(pos, io.SEEK_SET)

def _create_laz_backend(
Expand Down
2 changes: 1 addition & 1 deletion pylas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def encode_to_len(string: str, wanted_len: int, codec="ascii") -> bytes:

def encode_to_null_terminated(string: str, codec: str = "utf-8") -> bytes:
b = string.encode(codec)
if b[-1] != b"\0":
if b[-1] != 0:
b += b"\0"
return b

0 comments on commit 99eca68

Please sign in to comment.