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

Commit

Permalink
factorize code to raise format compatibility exception
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontaigu committed Jun 4, 2018
1 parent 9cba2f7 commit 84f07f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
17 changes: 5 additions & 12 deletions pylas/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ def create_las(*, point_format=0, file_version=None):
A new las data object
"""
if file_version is not None and not dims.is_point_fmt_compatible_with_version(point_format, file_version):
raise ValueError('Point format {} is not compatible with file version {}'.format(
point_format, file_version
))
if file_version is not None:
dims.raise_if_version_not_compatible_with_fmt(point_format, file_version)
else:
file_version = dims.min_file_version_for_point_format(point_format)

Expand Down Expand Up @@ -191,15 +189,10 @@ def convert(source_las, *, point_format_id=None, file_version=None):
point_format_id = source_las.points_data.point_format_id if point_format_id is None else point_format_id

if file_version is None:
file_version = dims.min_file_version_for_point_format(point_format_id)
# Don't downgrade the file version
if file_version < source_las.header.version:
file_version = source_las.header.version
elif dims.is_point_fmt_compatible_with_version(point_format_id, file_version):
file_version = str(file_version)
file_version = max(source_las.header.version, dims.min_file_version_for_point_format(point_format_id))
else:
raise ValueError('Point format {} is not compatible with file version {}'.format(
point_format_id, file_version))
file_version = str(file_version)
dims.raise_if_version_not_compatible_with_fmt(point_format_id, file_version)

header = headers.HeaderFactory.convert_header(source_las.header, file_version)
header.point_data_format_id = point_format_id
Expand Down
17 changes: 9 additions & 8 deletions pylas/point/dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,9 @@ def lost_dimensions(point_fmt_in, point_fmt_out):
""" Returns a list of the names of the dimensions that will be lost
when converting from point_fmt_in to point_fmt_out
"""
try:
unpacked_dims_in = UNPACKED_POINT_FORMATS_DTYPES[point_fmt_in]
except KeyError as e:
raise errors.PointFormatNotSupported(point_fmt_in) from e

try:
unpacked_dims_out = UNPACKED_POINT_FORMATS_DTYPES[point_fmt_out]
except KeyError as e:
raise errors.PointFormatNotSupported(point_fmt_out) from e
unpacked_dims_in = get_dtype_of_format_id(point_fmt_in, unpacked=True)
unpacked_dims_out = get_dtype_of_format_id(point_fmt_out, unpacked=True)

out_dims = unpacked_dims_out.fields
completely_lost = []
Expand All @@ -403,6 +397,12 @@ def is_point_fmt_compatible_with_version(point_format_id, file_version):
raise errors.FileVersionNotSupported(file_version)


def raise_if_version_not_compatible_with_fmt(point_format_id, file_version):
if not is_point_fmt_compatible_with_version(point_format_id, file_version):
raise ValueError('Point format {} is not compatible with file version {}'.format(
point_format_id, file_version))


def is_official_dimension(dimension_name, point_fmt):
official_names_for_fmt = set(get_dtype_of_format_id(point_fmt, unpacked=True).names)
return dimension_name in official_names_for_fmt
Expand All @@ -414,3 +414,4 @@ def get_extra_dimensions_spec(np_dtype, point_format_id):
if not is_official_dimension(name, point_format_id) and name not in COMPOSED_FIELDS[point_format_id]
]
return [(name, np_dtype[name]) for name in extra_dims_names]

0 comments on commit 84f07f4

Please sign in to comment.