-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reading of .xyz file format
- Loading branch information
Showing
2 changed files
with
174 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import numpy as np | ||
import math | ||
import traceback | ||
import pyproj | ||
import struct | ||
from pickle import dumps as pdumps | ||
|
||
|
||
def init(args): | ||
aabb = None | ||
total_point_count = 0 | ||
pointcloud_file_portions = [] | ||
avg_min = np.array([0.0, 0.0, 0.0]) | ||
color_scale = args.color_scale if "color_scale" in args else None | ||
|
||
input_srs = args.srs_in | ||
|
||
for filename in args.files: | ||
try: | ||
f = open(filename, "r") | ||
except Exception as e: | ||
print("Error opening {filename}. Skipping.".format(**locals())) | ||
print(e) | ||
continue | ||
|
||
count = 0 | ||
seek_values = [] | ||
while True: | ||
batch = 10000 | ||
points = np.zeros((batch, 3)) | ||
|
||
offset = f.tell() | ||
for i in range(batch): | ||
line = f.readline() | ||
if not line: | ||
points = np.resize(points, (i, 3)) | ||
break | ||
points[i] = [float(s) for s in line.split(" ")] | ||
|
||
if points.shape[0] == 0: | ||
break | ||
|
||
if not count % 1000000: | ||
seek_values += [offset] | ||
|
||
count += points.shape[0] | ||
batch_aabb = np.array([np.min(points, axis=0), np.max(points, axis=0)]) | ||
|
||
# Update aabb | ||
if aabb is None: | ||
aabb = batch_aabb | ||
else: | ||
aabb[0] = np.minimum(aabb[0], batch_aabb[0]) | ||
aabb[1] = np.maximum(aabb[1], batch_aabb[1]) | ||
|
||
# We need an exact point count | ||
total_point_count += count * args.fraction / 100 | ||
|
||
_1M = min(count, 1000000) | ||
steps = math.ceil(count / _1M) | ||
assert steps == 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 args.srs_out is not None and input_srs is None: | ||
raise Exception( | ||
"'{}' file doesn't contain srs information. Please use the --srs_in option to declare it.".format( | ||
filename | ||
) | ||
) | ||
|
||
return { | ||
"portions": pointcloud_file_portions, | ||
"aabb": aabb, | ||
"color_scale": color_scale, | ||
"srs_in": input_srs, | ||
"point_count": total_point_count, | ||
"avg_min": aabb[0], | ||
} | ||
|
||
|
||
def run(_id, filename, offset_scale, portion, queue, projection, verbose): | ||
""" | ||
Reads points from a xyz file | ||
""" | ||
try: | ||
f = open(filename, "r") | ||
|
||
point_count = portion[1] - portion[0] | ||
|
||
step = min(point_count, max((point_count) // 10, 100000)) | ||
|
||
f.seek(portion[2]) | ||
|
||
for i in range(0, point_count, step): | ||
points = np.zeros((step, 3), dtype=np.float32) | ||
|
||
for j in range(0, step): | ||
line = f.readline() | ||
if not line: | ||
points = np.resize(points, (j, 3)) | ||
break | ||
points[j] = [float(s) for s in line.split(" ")] | ||
|
||
x, y, z = [points[:, c] for c in [0, 1, 2]] | ||
|
||
if projection: | ||
x, y, z = pyproj.transform(projection[0], projection[1], x, y, z) | ||
|
||
x = (x + offset_scale[0][0]) * offset_scale[1][0] | ||
y = (y + offset_scale[0][1]) * offset_scale[1][1] | ||
z = (z + offset_scale[0][2]) * offset_scale[1][2] | ||
|
||
coords = np.vstack((x, y, z)).transpose() | ||
|
||
if offset_scale[2] is not None: | ||
# Apply transformation matrix (because the tile's transform will contain | ||
# the inverse of this matrix) | ||
coords = np.dot(coords, offset_scale[2]) | ||
|
||
coords = np.ascontiguousarray(coords.astype(np.float32)) | ||
|
||
# Read colors | ||
colors = np.full((point_count, 3), 255, dtype=np.uint8) | ||
|
||
result = ( | ||
"".encode("ascii"), | ||
pdumps({"xyz": coords, "rgb": colors}), | ||
len(coords), | ||
) | ||
queue.send_multipart( | ||
[ | ||
"".encode("ascii"), | ||
pdumps({"xyz": coords, "rgb": colors}), | ||
struct.pack(">I", len(coords)), | ||
], | ||
copy=False, | ||
) | ||
|
||
queue.send_multipart([pdumps({"name": _id, "total": 0})]) | ||
# notify we're idle | ||
queue.send_multipart([b""]) | ||
|
||
f.close() | ||
except Exception as e: | ||
print("Exception while reading points from xyz file") | ||
print(e) | ||
traceback.print_exc() |