Skip to content

Commit

Permalink
clean up and simplify conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Koyaani committed Jun 9, 2022
1 parent cb98d8a commit 47a90ad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
28 changes: 14 additions & 14 deletions py3dtiles/points/task/las_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ def init(files, color_scale=None, srs_in=None, srs_out=None, fraction=100):
total_point_count += count

# read the first points red channel
if color_scale is not None:
if color_scale:
color_scale_by_file[filename] = color_scale
elif 'red' in f.header.point_format.dimension_names:
points = next(f.chunk_iterator(10000))['red']
points = next(f.chunk_iterator(10_000))['red']
if np.max(points) > 255:
color_scale_by_file[filename] = 1.0 / 255
else:
# the intensity is then used as color
color_scale_by_file[filename] = 1.0 / 255

_1M = min(count, 1000000)
_1M = min(count, 1_000_000)
steps = math.ceil(count / _1M)
portions = [(i * _1M, min(count, (i + 1) * _1M)) for i in range(steps)]
for p in portions:
pointcloud_file_portions += [(filename, p)]

if (srs_out is not None and srs_in is None):
# NOTE: decode is necessary because in python3.5, json cannot decode bytes. Remove this once 3.5 is EOL
output = subprocess.check_output(['pdal', 'info', '--summary', filename]).decode('utf-8')
if srs_out and not srs_in:
output = subprocess.check_output(['pdal', 'info', '--summary', filename])
summary = json.loads(output)['summary']
if 'srs' not in summary or 'proj4' not in summary['srs'] or not summary['srs']['proj4']:
raise SrsInMissingException('\'{}\' file doesn\'t contain srs information. Please use the --srs_in option to declare it.'.format(filename))
if 'srs' not in summary or not summary['srs'].get('proj4'):
raise SrsInMissingException(f"'{filename}' file doesn't contain srs information."
"Please use the --srs_in option to declare it.")
srs_in = summary['srs']['proj4']
except Exception as e:
print('Error opening {filename}. Skipping.'.format(**locals()))
print(f'Error opening {filename}. Skipping.')
print(e)
continue

Expand All @@ -74,17 +74,17 @@ def init(files, color_scale=None, srs_in=None, srs_out=None, fraction=100):


def run(_id, filename, offset_scale, portion, queue, transformer, verbose):
'''
"""
Reads points from a las file
'''
"""
try:
with laspy.open(filename) as f:

point_count = portion[1] - portion[0]

step = min(point_count, max((point_count) // 10, 100000))
step = min(point_count, max(point_count // 10, 100_000))

indices = [i for i in range(math.ceil((point_count) / step))]
indices = [i for i in range(math.ceil(point_count / step))]

color_scale = offset_scale[3]

Expand Down Expand Up @@ -125,7 +125,7 @@ def run(_id, filename, offset_scale, portion, queue, transformer, verbose):
green = points['intensity']
blue = points['intensity']

if color_scale is None:
if not color_scale:
red = red.astype(np.uint8)
green = green.astype(np.uint8)
blue = blue.astype(np.uint8)
Expand Down
7 changes: 4 additions & 3 deletions py3dtiles/points/task/node_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _forward_unassigned_points(node, queue, log_file):

def _flush(node_catalog, scale, node, queue, max_depth=1, force_forward=False, log_file=None, depth=0):
if depth >= max_depth:
threshold = 0 if force_forward else 10000
threshold = 0 if force_forward else 10_000
if node.get_pending_points_count() > threshold:
return _forward_unassigned_points(node, queue, log_file)
else:
Expand Down Expand Up @@ -128,14 +128,15 @@ def _process(nodes, octree_metadata, name, raw_datas, queue, begin, log_file):
print('save on disk {} [{}]'.format(name, time.time() - begin), file=log_file)

# save node state on disk
data = b''
if halt_at_depth > 0:
data = node_catalog.dump(name, halt_at_depth - 1)
else:
data = b''

if log_enabled:
print('saved on disk [{}]'.format(time.time() - begin), file=log_file)

return (total, data)
return total, data


def run(work, octree_metadata, queue, verbose):
Expand Down
10 changes: 5 additions & 5 deletions py3dtiles/points/task/pnts_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import pickle
import struct
from pathlib import Path

import lz4.frame as gzip
import numpy as np
Expand All @@ -9,7 +9,7 @@
from py3dtiles.points.utils import ResponseType, name_to_filename


class _DummyNode():
class _DummyNode:
def __init__(self, _bytes):
if 'children' in _bytes:
self.children = _bytes['children']
Expand Down Expand Up @@ -42,16 +42,16 @@ def points_to_pnts(name, points, out_folder, include_rgb):

filename = name_to_filename(out_folder, name, '.pnts')

assert not os.path.exists(filename), '{} already written'.format(filename)
if Path(filename).exists():
raise FileExistsError(f"{filename} already written")

tile.save_as(filename)

return count, filename


def node_to_pnts(name, node, out_folder, include_rgb):
from py3dtiles.points.node import Node
points = Node.get_points(node, include_rgb)
points = py3dtiles.points.node.Node.get_points(node, include_rgb)
return points_to_pnts(name, points, out_folder, include_rgb)


Expand Down
22 changes: 11 additions & 11 deletions py3dtiles/points/task/xyz_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ def init(files, color_scale=None, srs_in=None, srs_out=None, fraction=100):
aabb = None
total_point_count = 0
pointcloud_file_portions = []
avg_min = np.array([0.0, 0.0, 0.0])

for filename in files:
try:
f = open(filename, "r")
f = open(filename)
except Exception as e:
print("Error opening {filename}. Skipping.".format(**locals()))
print(f"Error opening {filename}. Skipping.")
print(e)
continue

count = 0
seek_values = []
while True:
batch = 10000
batch = 10_000
points = np.zeros((batch, 3))

offset = f.tell()
Expand All @@ -38,7 +37,7 @@ def init(files, color_scale=None, srs_in=None, srs_out=None, fraction=100):
if points.shape[0] == 0:
break

if not count % 1000000:
if not count % 1_000_000:
seek_values += [offset]

count += points.shape[0]
Expand All @@ -56,20 +55,21 @@ def init(files, color_scale=None, srs_in=None, srs_out=None, fraction=100):
# We need an exact point count
total_point_count += count * fraction / 100

_1M = min(count, 1000000)
_1M = min(count, 1_000_000)
steps = math.ceil(count / _1M)
assert steps == len(seek_values)
if steps != len(seek_values):
raise ValueError("the size of seek_values should be equal to steps,"
f"currently {steps = } and {len(seek_values) = }")
portions = [
(i * _1M, min(count, (i + 1) * _1M), seek_values[i]) for i in range(steps)
]
for p in portions:
pointcloud_file_portions += [(filename, p)]

if srs_out is not None and srs_in is None:
if srs_out and not srs_in:
raise Exception(
"'{}' file doesn't contain srs information. Please use the --srs_in option to declare it.".format(
filename
)
f"'{filename}' file doesn't contain srs information."
"Please use the --srs_in option to declare it."
)

return {
Expand Down

0 comments on commit 47a90ad

Please sign in to comment.