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

Commit

Permalink
Fix minor 'syntax' issues + formatting
Browse files Browse the repository at this point in the history
Remove some unnecessary else clause after for loops
remove one unused variable in LasMMAP
  • Loading branch information
tmontaigu committed Sep 4, 2020
1 parent b09d6cc commit a5ae92a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 65 deletions.
4 changes: 1 addition & 3 deletions pylas/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,4 @@ def find_laszip_executable():
)
if any(in_path):
return binary

else:
raise FileNotFoundError("Could not find laszip executable")
raise FileNotFoundError("Could not find laszip executable")
18 changes: 6 additions & 12 deletions pylas/headers/rawheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,28 +258,22 @@ def set_compressed(self, compressed: bool):

def update(self, points: PointRecord) -> None:
self.x_max = max(
self.x_max,
(points["X"].max() * self.x_scale) + self.x_offset,
self.x_max, (points["X"].max() * self.x_scale) + self.x_offset,
)
self.y_max = max(
self.y_max,
(points["Y"].max() * self.y_scale) + self.y_offset,
self.y_max, (points["Y"].max() * self.y_scale) + self.y_offset,
)
self.z_max = max(
self.z_max,
(points["Z"].max() * self.z_scale) + self.z_offset,
self.z_max, (points["Z"].max() * self.z_scale) + self.z_offset,
)
self.x_min = min(
self.x_min,
(points["X"].min() * self.x_scale) + self.x_offset,
self.x_min, (points["X"].min() * self.x_scale) + self.x_offset,
)
self.y_min = min(
self.y_min,
(points["Y"].min() * self.y_scale) + self.y_offset,
self.y_min, (points["Y"].min() * self.y_scale) + self.y_offset,
)
self.z_min = min(
self.z_min,
(points["Z"].min() * self.z_scale) + self.z_offset,
self.z_min, (points["Z"].min() * self.z_scale) + self.z_offset,
)

for i, count in zip(*np.unique(points.return_number, return_counts=True)):
Expand Down
55 changes: 34 additions & 21 deletions pylas/lasappender.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ def done(self):

class LasAppender:
def __init__(
self,
dest,
laz_backend: Union[LazBackend, Iterable[LazBackend]] = (
LazBackend.LazrsParallel,
LazBackend.Lazrs,
),
closefd: bool = True,
self,
dest,
laz_backend: Union[LazBackend, Iterable[LazBackend]] = (
LazBackend.LazrsParallel,
LazBackend.Lazrs,
),
closefd: bool = True,
) -> None:
if not dest.seekable():
raise TypeError("Expected the 'dest' to be a seekable file object")
Expand All @@ -110,14 +110,17 @@ def __init__(
if not header.are_points_compressed:
self.points_writer = UncompressedPointWriter(self.dest)
self.dest.seek(
(self.header.point_count * self.header.point_size) + self.header.offset_to_point_data,
io.SEEK_SET
(self.header.point_count * self.header.point_size)
+ self.header.offset_to_point_data,
io.SEEK_SET,
)
else:
self.points_writer = self._create_laz_backend(laz_backend)

if header.version >= "1.4" and header.number_of_evlr > 0:
assert self.dest.tell() <= self.header.start_of_first_evlr, "The position is past the start of evlrs"
assert (
self.dest.tell() <= self.header.start_of_first_evlr
), "The position is past the start of evlrs"
pos = self.dest.tell()
self.dest.seek(self.header.start_of_first_evlr, io.SEEK_SET)
self.evlrs = EVLRList.read_from(self.dest, self.header.number_of_evlr)
Expand Down Expand Up @@ -157,11 +160,11 @@ def _write_updated_header(self):
self.dest.seek(pos, io.SEEK_SET)

def _create_laz_backend(
self,
laz_backend: Union[LazBackend, Iterable[LazBackend]] = (
LazBackend.LazrsParallel,
LazBackend.Lazrs,
),
self,
laz_backend: Union[LazBackend, Iterable[LazBackend]] = (
LazBackend.LazrsParallel,
LazBackend.Lazrs,
),
) -> LazrsAppender:
try:
laz_backend = iter(laz_backend)
Expand All @@ -173,14 +176,24 @@ def _create_laz_backend(
if backend == LazBackend.Laszip:
raise PylasError("Laszip backend does not support appending")
elif backend == LazBackend.LazrsParallel:
return LazrsAppender(self.dest, self.header, self.vlrs, parallel=True)
try:
return LazrsAppender(
self.dest, self.header, self.vlrs, parallel=True
)
except Exception as e:
last_error = e
elif backend == LazBackend.Lazrs:
return LazrsAppender(self.dest, self.header, self.vlrs, parallel=False)
try:
return LazrsAppender(
self.dest, self.header, self.vlrs, parallel=False
)
except Exception as e:
last_error = e

if last_error is not None:
raise PylasError(f"Could not initialize a laz backend: {last_error}")
else:
if last_error is not None:
raise PylasError(f"Could not initialize a laz backend: {last_error}")
else:
raise PylasError(f"No valid laz backend selected")
raise PylasError(f"No valid laz backend selected")

def __enter__(self):
return self
Expand Down
1 change: 0 additions & 1 deletion pylas/lasmmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def __init__(self, filename):
except IndexError:
extra_dims = None

point_format = PointFormat(self.header.point_format_id, extra_dims=extra_dims)
self.points_data = record.PackedPointRecord.from_buffer(
self.mmap,
self.header.point_format_id,
Expand Down
21 changes: 13 additions & 8 deletions pylas/lasreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class LasReader:
"""The reader class handles LAS and LAZ via one of the supported backend"""

def __init__(
self,
source: BinaryIO,
closefd: bool = True,
laz_backends: Union[
LazBackend, Iterable[LazBackend]
] = LazBackend.detect_available(),
self,
source: BinaryIO,
closefd: bool = True,
laz_backends: Union[
LazBackend, Iterable[LazBackend]
] = LazBackend.detect_available(),
):
self.closefd = closefd
self.laz_backends = laz_backends
Expand Down Expand Up @@ -102,10 +102,15 @@ def read(self):
points = record.PackedPointRecord.empty(self.point_format)

if self.header.version >= "1.4":
if self.header.are_points_compressed and not self.point_source.source.seekable():
if (
self.header.are_points_compressed
and not self.point_source.source.seekable()
):
# We explicitly require seekable stream because we have to seek
# past the chunk table of LAZ file
raise errors.PylasError("source must be seekable, to read evlrs form LAZ file")
raise errors.PylasError(
"source must be seekable, to read evlrs form LAZ file"
)
evlrs = self._read_evlrs(self.point_source.source, seekable=True)
las_data = las14.LasData(
header=self.header, vlrs=self.vlrs, points=points, evlrs=evlrs
Expand Down
10 changes: 5 additions & 5 deletions pylas/laswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _create_laz_backend(
except TypeError:
laz_backends = (laz_backends,)

last_error = None
last_error: Optional[Exception] = None
for backend in laz_backends:
try:
if not backend.is_available():
Expand All @@ -188,11 +188,11 @@ def _create_laz_backend(
except Exception as e:
logger.error(e)
last_error = e

if last_error is not None:
raise PylasError("No LazBackend selected, cannot compress")
else:
if last_error is not None:
raise PylasError("No LazBackend selected, cannot compress")
else:
raise PylasError(f"No LazBackend could be initialized: {last_error}")
raise PylasError(f"No LazBackend could be initialized: {last_error}")

def __enter__(self):
return self
Expand Down
13 changes: 6 additions & 7 deletions pylas/point/dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ def np_dtype_to_point_format(dtype, unpacked=False):
for format_id, fmt_dtype in all_dtypes.items():
if fmt_dtype == dtype:
return format_id
else:
raise errors.IncompatibleDataFormat(
"Data type of array is not compatible with any point format (array dtype: {})".format(
dtype
)

raise errors.IncompatibleDataFormat(
"Data type of array is not compatible with any point format (array dtype: {})".format(
dtype
)
)


def size_of_point_format_id(point_format_id):
Expand All @@ -280,8 +280,7 @@ def min_file_version_for_point_format(point_format_id):
for version, point_formats in sorted(VERSION_TO_POINT_FMT.items()):
if point_format_id in point_formats:
return version
else:
raise errors.PointFormatNotSupported(point_format_id)
raise errors.PointFormatNotSupported(point_format_id)


def supported_versions():
Expand Down
6 changes: 3 additions & 3 deletions pylas/vlrs/known.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import abc
import ctypes
import struct
from abc import abstractmethod

from .rawvlr import NULL_BYTE, BaseVLR, VLR
from ..extradims import (
Expand All @@ -15,6 +14,8 @@
DimensionSignedness,
)

abstractmethod = abc.abstractmethod


class IKnownVLR(abc.ABC):
"""Interface that any KnownVLR must implement.
Expand Down Expand Up @@ -559,5 +560,4 @@ def vlr_factory(raw_vlr):
and raw_vlr.header.record_id in known_vlr.official_record_ids()
):
return known_vlr.from_raw(raw_vlr)
else:
return VLR.from_raw(raw_vlr)
return VLR.from_raw(raw_vlr)
3 changes: 1 addition & 2 deletions pylas/vlrs/vlrlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ def index(self, vlr_type):
for i, v in enumerate(self.vlrs):
if v.__class__.__name__ == vlr_type:
return i
else:
raise ValueError("{} is not in the VLR list".format(vlr_type))
raise ValueError("{} is not in the VLR list".format(vlr_type))

def __iter__(self):
yield from iter(self.vlrs)
Expand Down
5 changes: 2 additions & 3 deletions recursive_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np

import pylas
from pylas import LazBackend


def recursive_split(x_min, y_min, x_max, y_max, max_x_size, max_y_size):
Expand All @@ -29,7 +28,7 @@ def tuple_size(string):
try:
return tuple(map(float, string.split("x")))
except:
raise argparse.ArgumentError("Size must be in the form of 50.0x65.14")
raise ValueError("Size must be in the form of numberxnumber eg: 50.0x65.14")


def main():
Expand Down Expand Up @@ -61,7 +60,7 @@ def main():
if writers[i] is None:
writers[i] = pylas.open(f"{sys.argv[2]}/output_{i}.laz",
mode='w',
laz_backends=[LazBackend.LazrsParallel],
laz_backends=[pylas.LazBackend.LazrsParallel],
header=file.header)
sub_points = points[mask]
writers[i].write(sub_points)
Expand Down

0 comments on commit a5ae92a

Please sign in to comment.