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

Commit

Permalink
Try to correctly read points on files shorter than expected
Browse files Browse the repository at this point in the history
Try to correctly read points if the file does not have enough bytes for the expected number of points by recalculating the actual number of points if point_size is a multiple of the bytes_len read
  • Loading branch information
tmontaigu committed Jul 4, 2018
1 parent 18ceb59 commit b197ea9
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pylas/point/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

from . import dims, packing
from ..compression import decompress_buffer
from .. import errors
import logging

logger = logging.getLogger(__name__)


class PointRecord(ABC):
Expand Down Expand Up @@ -211,7 +215,36 @@ def from_stream(cls, stream, point_format_id, count, extra_dims=None):
point_format_id, extra_dims=extra_dims
)
point_data_buffer = bytearray(stream.read(count * points_dtype.itemsize))
data = np.frombuffer(point_data_buffer, dtype=points_dtype, count=count)
expected_bytes_len = count * points_dtype.itemsize

try:
data = np.frombuffer(point_data_buffer, dtype=points_dtype, count=count)
except ValueError:
if len(point_data_buffer) % points_dtype.itemsize != 0:
missing_bytes_len = expected_bytes_len - len(point_data_buffer)
raise errors.PylasError(
"The file does not contain enough bytes to store the expected number of points\n"
"expected {} bytes, read {} bytes ({} bytes missing == {} points) and it cannot be corrected\n"
"{} (bytes) / {} (point_size) = {} (points)".format(
expected_bytes_len,
len(point_data_buffer),
missing_bytes_len,
missing_bytes_len / points_dtype.itemsize,
len(point_data_buffer),
points_dtype.itemsize,
len(point_data_buffer) / points_dtype.itemsize,
)
)
else:
actual_count = len(point_data_buffer) // points_dtype.itemsize
logger.warning(
"Expected {} points, there are {} ({} missing)".format(
count, actual_count, count - actual_count
)
)
data = np.frombuffer(
point_data_buffer, dtype=points_dtype, count=actual_count
)

return cls(data, point_format_id)

Expand Down

0 comments on commit b197ea9

Please sign in to comment.