py4dgeo 0.6.0__cp312-cp312-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
py4dgeo/epoch.py ADDED
@@ -0,0 +1,745 @@
1
+ from py4dgeo.logger import logger_context
2
+ from py4dgeo.registration import Transformation
3
+ from py4dgeo.util import (
4
+ Py4DGeoError,
5
+ append_file_extension,
6
+ as_double_precision,
7
+ find_file,
8
+ make_contiguous,
9
+ is_iterable,
10
+ )
11
+ from numpy.lib.recfunctions import append_fields
12
+
13
+ import dateparser
14
+ import datetime
15
+ import json
16
+ import laspy
17
+ import logging
18
+ import numpy as np
19
+ import os
20
+ import tempfile
21
+ import typing
22
+ import zipfile
23
+
24
+ import _py4dgeo
25
+
26
+ logger = logging.getLogger("py4dgeo")
27
+
28
+ # This integer controls the versioning of the epoch file format. Whenever the
29
+ # format is changed, this version should be increased, so that py4dgeo can warn
30
+ # about incompatibilities of py4dgeo with loaded data. This version is intentionally
31
+ # different from py4dgeo's version, because not all releases of py4dgeo necessarily
32
+ # change the epoch file format and we want to be as compatible as possible.
33
+ PY4DGEO_EPOCH_FILE_FORMAT_VERSION = 4
34
+
35
+
36
+ class NumpyArrayEncoder(json.JSONEncoder):
37
+ def default(self, obj):
38
+ if isinstance(obj, np.ndarray):
39
+ return obj.tolist()
40
+ return json.JSONEncoder.default(self, obj)
41
+
42
+
43
+ class Epoch(_py4dgeo.Epoch):
44
+ def __init__(
45
+ self,
46
+ cloud: np.ndarray,
47
+ normals: np.ndarray = None,
48
+ additional_dimensions: np.ndarray = None,
49
+ timestamp=None,
50
+ scanpos_info: dict = None,
51
+ ):
52
+ """
53
+
54
+ :param cloud:
55
+ The point cloud array of shape (n, 3).
56
+
57
+ :param normals:
58
+ The point cloud normals of shape (n, 3) where n is the
59
+ same as the number of points in the point cloud.
60
+
61
+ :param additional_dimensions:
62
+ A numpy array of additional, per-point data in the point cloud. The
63
+ numpy data type is expected to be a structured dtype, so that the data
64
+ columns are accessible by their name.
65
+
66
+ :param timestamp:
67
+ The point cloud timestamp, default is None.
68
+
69
+ :param scanpos_info:
70
+ The point scan positions information, default is None..
71
+ """
72
+ # Check the given array shapes
73
+ if len(cloud.shape) != 2 or cloud.shape[1] != 3:
74
+ raise Py4DGeoError("Clouds need to be an array of shape nx3")
75
+
76
+ # Make sure that cloud is double precision and contiguous in memory
77
+ cloud = as_double_precision(cloud)
78
+ cloud = make_contiguous(cloud)
79
+
80
+ # Set identity transformation
81
+ self._transformations = []
82
+
83
+ # Make sure that given normals are DP and contiguous as well
84
+ if normals is not None:
85
+ normals = make_contiguous(as_double_precision(normals))
86
+ self._normals = normals
87
+
88
+ # Set metadata properties
89
+ self.timestamp = timestamp
90
+ self.scanpos_info = scanpos_info
91
+
92
+ # Set the additional information (e.g. segment ids, normals, etc)
93
+ self.additional_dimensions = additional_dimensions
94
+
95
+ # Call base class constructor
96
+ super().__init__(cloud)
97
+
98
+ @property
99
+ def normals(self):
100
+ # Maybe calculate normals
101
+ if self._normals is None:
102
+ raise Py4DGeoError(
103
+ "Normals for this Epoch have not been calculated! Please use Epoch.calculate_normals or load externally calculated normals."
104
+ )
105
+
106
+ return self._normals
107
+
108
+ def calculate_normals(
109
+ self, radius=1.0, orientation_vector: np.ndarray = np.array([0, 0, 1])
110
+ ):
111
+ """Calculate point cloud normals
112
+
113
+ :param radius:
114
+ The radius used to determine the neighborhood of a point.
115
+
116
+ :param orientation_vector:
117
+ A vector to determine orientation of the normals. It should point "up".
118
+ """
119
+
120
+ # Ensure that the KDTree is built
121
+ if self.kdtree.leaf_parameter() == 0:
122
+ self.build_kdtree()
123
+
124
+ # Allocate memory for the normals
125
+ self._normals = np.empty(self.cloud.shape, dtype=np.float64)
126
+
127
+ # Reuse the multiscale code with a single radius in order to
128
+ # avoid code duplication.
129
+ with logger_context("Calculating point cloud normals:"):
130
+ _py4dgeo.compute_multiscale_directions(
131
+ self,
132
+ self.cloud,
133
+ [radius],
134
+ orientation_vector,
135
+ self._normals,
136
+ )
137
+
138
+ return self.normals
139
+
140
+ @property
141
+ def timestamp(self):
142
+ return self._timestamp
143
+
144
+ @timestamp.setter
145
+ def timestamp(self, timestamp):
146
+ self._timestamp = normalize_timestamp(timestamp)
147
+
148
+ @property
149
+ def scanpos_info(self):
150
+ return self._scanpos_info
151
+
152
+ @scanpos_info.setter
153
+ def scanpos_info(self, scanpos_info):
154
+ if isinstance(scanpos_info, list):
155
+ self._scanpos_info = scanpos_info
156
+ elif isinstance(scanpos_info, dict):
157
+ self._scanpos_info = scan_positions_info_from_dict(scanpos_info)
158
+ else:
159
+ self._scanpos_info = None
160
+
161
+ @property
162
+ def scanpos_id(self):
163
+ return (
164
+ self.additional_dimensions["scanpos_id"]
165
+ .reshape(self.cloud.shape[0])
166
+ .astype(np.int32)
167
+ )
168
+
169
+ @scanpos_id.setter
170
+ def scanpos_id(self, scanpos_id):
171
+ if self.additional_dimensions is None:
172
+ additional_columns = np.empty(
173
+ shape=(self.cloud.shape[0], 1),
174
+ dtype=np.dtype([("scanpos_id", "<i4")]),
175
+ )
176
+ additional_columns["scanpos_id"] = np.array(
177
+ scanpos_id, dtype=np.int32
178
+ ).reshape(-1, 1)
179
+ self.additional_dimensions = additional_columns
180
+ else:
181
+ scanpos_id = np.array(scanpos_id, dtype=np.int32)
182
+ new_additional_dimensions = append_fields(
183
+ self.additional_dimensions, "scanpos_id", scanpos_id, usemask=False
184
+ )
185
+
186
+ self.additional_dimensions = new_additional_dimensions
187
+
188
+ @property
189
+ def metadata(self):
190
+ """Provide the metadata of this epoch as a Python dictionary
191
+
192
+ The return value of this property only makes use of Python built-in
193
+ data structures such that it can e.g. be serialized using the JSON
194
+ module. Also, the returned values are understood by :ref:`Epoch.__init__`
195
+ such that you can do :code:`Epoch(cloud, **other.metadata)`.
196
+ """
197
+
198
+ return {
199
+ "timestamp": None if self.timestamp is None else str(self.timestamp),
200
+ "scanpos_info": None if self.scanpos_info is None else self.scanpos_info,
201
+ }
202
+
203
+ def build_kdtree(self, leaf_size=10, force_rebuild=False):
204
+ """Build the search tree index
205
+
206
+ :param leaf_size:
207
+ An internal optimization parameter of the search tree data structure.
208
+ The algorithm uses a bruteforce search on subtrees of size below the
209
+ given threshold. Increasing this value speeds up search tree build time,
210
+ but slows down query times.
211
+ :type leaf_size: int
212
+ :param force_rebuild:
213
+ Rebuild the search tree even if it was already built before.
214
+ :type force_rebuild: bool
215
+ """
216
+ if self.kdtree.leaf_parameter() == 0 or force_rebuild:
217
+ logger.info(f"Building KDTree structure with leaf parameter {leaf_size}")
218
+ self.kdtree.build_tree(leaf_size)
219
+
220
+ def transform(
221
+ self,
222
+ transformation: typing.Optional[Transformation] = None,
223
+ affine_transformation: typing.Optional[np.ndarray] = None,
224
+ rotation: typing.Optional[np.ndarray] = None,
225
+ translation: typing.Optional[np.ndarray] = None,
226
+ reduction_point: typing.Optional[np.ndarray] = None,
227
+ ):
228
+ """Transform the epoch with an affine transformation
229
+
230
+ :param transformation:
231
+ A Transformation object that describes the transformation to apply.
232
+ If this argument is given, the other arguments are ignored.
233
+ This parameter is typically used if the transformation was calculated
234
+ by py4dgeo itself.
235
+ :type transformation: Transformation
236
+ :param affine_transformation:
237
+ A 4x4 or 3x4 matrix representing the affine transformation. Given
238
+ as a numpy array. If this argument is given, the rotation and
239
+ translation arguments are ignored.
240
+ :type transformation: np.ndarray
241
+ :param rotation:
242
+ A 3x3 matrix specifying the rotation to apply
243
+ :type rotation: np.ndarray
244
+ :param translation:
245
+ A vector specifying the translation to apply
246
+ :type translation: np.ndarray
247
+ :param reduction_point:
248
+ A translation vector to apply before applying rotation and scaling.
249
+ This is used to increase the numerical accuracy of transformation.
250
+ If a transformation is given, this argument is ignored.
251
+ :type reduction_point: np.ndarray
252
+ """
253
+
254
+ # Extract the affine transformation and reduction point from the given transformation
255
+ if transformation is not None:
256
+ assert isinstance(transformation, Transformation)
257
+ affine_transformation = transformation.affine_transformation
258
+ reduction_point = transformation.reduction_point
259
+
260
+ # Build the transformation if it is not explicitly given
261
+ if affine_transformation is None:
262
+ trafo = np.identity(4, dtype=np.float64)
263
+ trafo[:3, :3] = rotation
264
+ trafo[:3, 3] = translation
265
+ else:
266
+ # If it was given, make a copy and potentially resize it
267
+ trafo = affine_transformation.copy()
268
+ if trafo.shape[0] == 3:
269
+ trafo.resize((4, 4), refcheck=False)
270
+ trafo[3, 3] = 1
271
+
272
+ if reduction_point is None:
273
+ reduction_point = np.array([0, 0, 0], dtype=np.float64)
274
+
275
+ # Ensure contiguous DP memory
276
+ trafo = as_double_precision(make_contiguous(trafo))
277
+
278
+ # Invalidate the KDTree
279
+ self.kdtree.invalidate()
280
+
281
+ # Apply the actual transformation as efficient C++
282
+ _py4dgeo.transform_pointcloud_inplace(self.cloud, trafo, reduction_point)
283
+
284
+ # Store the transformation
285
+ self._transformations.append(
286
+ Transformation(affine_transformation=trafo, reduction_point=reduction_point)
287
+ )
288
+
289
+ @property
290
+ def transformation(self):
291
+ """Access the affine transformations that were applied to this epoch
292
+
293
+ In order to set this property please use the transform method instead,
294
+ which will make sure to also apply the transformation.
295
+
296
+ :returns:
297
+ Returns a list of applied transformations. These are given
298
+ as a tuple of a 4x4 matrix defining the affine transformation
299
+ and the reduction point used when applying it.
300
+ """
301
+ return self._transformations
302
+
303
+ def save(self, filename):
304
+ """Save this epoch to a file
305
+
306
+ :param filename:
307
+ The filename to save the epoch in.
308
+ :type filename: str
309
+ """
310
+
311
+ # Ensure that we have a file extension
312
+ filename = append_file_extension(filename, "zip")
313
+ logger.info(f"Saving epoch to file '{filename}'")
314
+
315
+ # Use a temporary directory when creating files
316
+ with tempfile.TemporaryDirectory() as tmp_dir:
317
+ # Create the final archive
318
+ with zipfile.ZipFile(
319
+ filename, mode="w", compression=zipfile.ZIP_BZIP2
320
+ ) as zf:
321
+ # Write the epoch file format version number
322
+ zf.writestr("EPOCH_FILE_FORMAT", str(PY4DGEO_EPOCH_FILE_FORMAT_VERSION))
323
+
324
+ # Write the metadata dictionary into a json file
325
+ metadatafile = os.path.join(tmp_dir, "metadata.json")
326
+ with open(metadatafile, "w") as f:
327
+ json.dump(self.metadata, f)
328
+ zf.write(metadatafile, arcname="metadata.json")
329
+
330
+ # Write the transformation into a file
331
+ trafofile = os.path.join(tmp_dir, "trafo.json")
332
+ with open(trafofile, "w") as f:
333
+ json.dump(
334
+ [t.__dict__ for t in self._transformations],
335
+ f,
336
+ cls=NumpyArrayEncoder,
337
+ )
338
+ zf.write(trafofile, arcname="trafo.json")
339
+
340
+ # Write the actual point cloud array using laspy - LAZ compression
341
+ # is far better than any compression numpy + zipfile can do.
342
+ cloudfile = os.path.join(tmp_dir, "cloud.laz")
343
+ hdr = laspy.LasHeader(version="1.4", point_format=6)
344
+ hdr.x_scale = 0.00025
345
+ hdr.y_scale = 0.00025
346
+ hdr.z_scale = 0.00025
347
+ mean_extent = np.mean(self.cloud, axis=0)
348
+ hdr.x_offset = int(mean_extent[0])
349
+ hdr.y_offset = int(mean_extent[1])
350
+ hdr.z_offset = int(mean_extent[2])
351
+ lasfile = laspy.LasData(hdr)
352
+ lasfile.x = self.cloud[:, 0]
353
+ lasfile.y = self.cloud[:, 1]
354
+ lasfile.z = self.cloud[:, 2]
355
+
356
+ # define dimensions for normals below:
357
+ if self._normals is not None:
358
+ lasfile.add_extra_dim(
359
+ laspy.ExtraBytesParams(
360
+ name="NormalX", type="f8", description="X axis of normals"
361
+ )
362
+ )
363
+ lasfile.add_extra_dim(
364
+ laspy.ExtraBytesParams(
365
+ name="NormalY", type="f8", description="Y axis of normals"
366
+ )
367
+ )
368
+ lasfile.add_extra_dim(
369
+ laspy.ExtraBytesParams(
370
+ name="NormalZ", type="f8", description="Z axis of normals"
371
+ )
372
+ )
373
+ lasfile.NormalX = self.normals[:, 0]
374
+ lasfile.NormalY = self.normals[:, 1]
375
+ lasfile.NormalZ = self.normals[:, 2]
376
+ else:
377
+ logger.info("Saving a file without normals.")
378
+
379
+ lasfile.write(cloudfile)
380
+ zf.write(cloudfile, arcname="cloud.laz")
381
+
382
+ kdtreefile = os.path.join(tmp_dir, "kdtree")
383
+ with open(kdtreefile, "w") as f:
384
+ self.kdtree.save_index(kdtreefile)
385
+ zf.write(kdtreefile, arcname="kdtree")
386
+
387
+ @staticmethod
388
+ def load(filename):
389
+ """Construct an Epoch instance by loading it from a file
390
+
391
+ :param filename:
392
+ The filename to load the epoch from.
393
+ :type filename: str
394
+ """
395
+
396
+ # Ensure that we have a file extension
397
+ filename = append_file_extension(filename, "zip")
398
+ logger.info(f"Restoring epoch from file '{filename}'")
399
+
400
+ # Use temporary directory for extraction of files
401
+ with tempfile.TemporaryDirectory() as tmp_dir:
402
+ # Open the ZIP archive
403
+ with zipfile.ZipFile(filename, mode="r") as zf:
404
+ # Read the epoch file version number and compare to current
405
+ version = int(zf.read("EPOCH_FILE_FORMAT").decode())
406
+ if version > PY4DGEO_EPOCH_FILE_FORMAT_VERSION:
407
+ raise Py4DGeoError(
408
+ "Epoch file format not known - please update py4dgeo!"
409
+ )
410
+
411
+ # Read the metadata JSON file
412
+ metadatafile = zf.extract("metadata.json", path=tmp_dir)
413
+ with open(metadatafile, "r") as f:
414
+ metadata = json.load(f)
415
+
416
+ # Restore the point cloud itself
417
+ cloudfile = zf.extract("cloud.laz", path=tmp_dir)
418
+ lasfile = laspy.read(cloudfile)
419
+ cloud = np.vstack((lasfile.x, lasfile.y, lasfile.z)).transpose()
420
+ try:
421
+ normals = np.vstack(
422
+ (lasfile.NormalX, lasfile.NormalY, lasfile.NormalZ)
423
+ ).transpose()
424
+ except AttributeError:
425
+ normals = None
426
+ # Construct the epoch object
427
+ epoch = Epoch(cloud, normals=normals, **metadata)
428
+
429
+ # Restore the KDTree object
430
+ kdtreefile = zf.extract("kdtree", path=tmp_dir)
431
+ epoch.kdtree.load_index(kdtreefile)
432
+
433
+ # Read the transformation if it exists
434
+ if version >= 3:
435
+ trafofile = zf.extract("trafo.json", path=tmp_dir)
436
+ with open(trafofile, "r") as f:
437
+ trafo = json.load(f)
438
+ epoch._transformations = [Transformation(**t) for t in trafo]
439
+
440
+ return epoch
441
+
442
+ def __getstate__(self):
443
+ return (
444
+ PY4DGEO_EPOCH_FILE_FORMAT_VERSION,
445
+ self.metadata,
446
+ _py4dgeo.Epoch.__getstate__(self),
447
+ )
448
+
449
+ def __setstate__(self, state):
450
+ v, metadata, base = state
451
+
452
+ if v != PY4DGEO_EPOCH_FILE_FORMAT_VERSION:
453
+ raise Py4DGeoError("Epoch file format is out of date!")
454
+
455
+ # Restore metadata
456
+ for k, v in metadata.items():
457
+ setattr(self, k, v)
458
+
459
+ # Set the base class object
460
+ _py4dgeo.Epoch.__setstate__(self, base)
461
+
462
+
463
+ def save_epoch(epoch, filename):
464
+ """Save an epoch to a given filename
465
+
466
+ :param epoch:
467
+ The epoch that should be saved.
468
+ :type epoch: Epoch
469
+ :param filename:
470
+ The filename where to save the epoch
471
+ :type filename: str
472
+ """
473
+ return epoch.save(filename)
474
+
475
+
476
+ def load_epoch(filename):
477
+ """Load an epoch from a given filename
478
+
479
+ :param filename:
480
+ The filename to load the epoch from.
481
+ :type filename: str
482
+ """
483
+ return Epoch.load(filename)
484
+
485
+
486
+ def as_epoch(cloud):
487
+ """Create an epoch from a cloud
488
+
489
+ Idempotent operation to create an epoch from a cloud.
490
+ """
491
+
492
+ # If this is already an epoch, this is a no-op
493
+ if isinstance(cloud, Epoch):
494
+ return cloud
495
+
496
+ # Initialize an epoch from the given cloud
497
+ logger.info("Initializing Epoch object from given point cloud")
498
+ return Epoch(cloud)
499
+
500
+
501
+ def _as_tuple(x):
502
+ if isinstance(x, tuple):
503
+ return x
504
+ return (x,)
505
+
506
+
507
+ def read_from_xyz(
508
+ *filenames,
509
+ xyz_columns=[0, 1, 2],
510
+ normal_columns=[],
511
+ additional_dimensions={},
512
+ **parse_opts,
513
+ ):
514
+ """Create an epoch from an xyz file
515
+
516
+ :param filename:
517
+ The filename to read from. Each line in the input file is expected
518
+ to contain three space separated numbers.
519
+ :type filename: str
520
+ :param xyz_columns:
521
+ The column indices of X, Y and Z coordinates. Defaults to [0, 1, 2].
522
+ :type xyz_columns: list
523
+ :param normal_columns:
524
+ The column indices of the normal vector components. Leave empty, if
525
+ your data file does not contain normals, otherwise exactly three indices
526
+ for the x, y and z components need to be given.
527
+ :type normal_columns: list
528
+ :param parse_opts:
529
+ Additional options forwarded to numpy.genfromtxt. This can be used
530
+ to e.g. change the delimiter character, remove header_lines or manually
531
+ specify which columns of the input contain the XYZ coordinates.
532
+ :param additional_dimensions:
533
+ A dictionary, mapping column indices to names of additional data dimensions.
534
+ They will be read from the file and are accessible under their names from the
535
+ created Epoch objects.
536
+ Additional column indexes start with 3.
537
+ :type parse_opts: dict
538
+ """
539
+
540
+ # Resolve the given path
541
+ filename = find_file(filenames[0])
542
+
543
+ # Ensure that usecols is not passed by the user, we need to use this
544
+ if "usecols" in parse_opts:
545
+ raise Py4DGeoError(
546
+ "read_from_xyz cannot be customized by using usecols, please use xyz_columns, normal_columns or additional_dimensions instead!"
547
+ )
548
+
549
+ # Read the point cloud
550
+ logger.info(f"Reading point cloud from file '{filename}'")
551
+
552
+ try:
553
+ cloud = np.genfromtxt(
554
+ filename, dtype=np.float64, usecols=xyz_columns, **parse_opts
555
+ )
556
+ except ValueError:
557
+ raise Py4DGeoError("Malformed XYZ file")
558
+
559
+ # Potentially read normals
560
+ normals = None
561
+ if normal_columns:
562
+ if len(normal_columns) != 3:
563
+ raise Py4DGeoError("normal_columns need to be a list of three integers!")
564
+
565
+ try:
566
+ normals = np.genfromtxt(
567
+ filename, dtype=np.float64, usecols=normal_columns, **parse_opts
568
+ )
569
+ except ValueError:
570
+ raise Py4DGeoError("Malformed XYZ file")
571
+
572
+ # Potentially read additional_dimensions passed by the user
573
+ additional_columns = np.empty(
574
+ shape=(cloud.shape[0], 1),
575
+ dtype=np.dtype([(name, "<f8") for name in additional_dimensions.values()]),
576
+ )
577
+
578
+ add_cols = list(sorted(additional_dimensions.keys()))
579
+ try:
580
+ parsed_additionals = np.genfromtxt(
581
+ filename, dtype=np.float64, usecols=add_cols, **parse_opts
582
+ )
583
+ # Ensure that the parsed array is two-dimensional, even if only
584
+ # one additional dimension was given (avoids an edge case)
585
+ parsed_additionals = parsed_additionals.reshape(-1, 1)
586
+ except ValueError:
587
+ raise Py4DGeoError("Malformed XYZ file")
588
+
589
+ for i, col in enumerate(add_cols):
590
+ additional_columns[additional_dimensions[col]] = parsed_additionals[
591
+ :, i
592
+ ].reshape(-1, 1)
593
+
594
+ # Finalize the construction of the new epoch
595
+ new_epoch = Epoch(cloud, normals=normals, additional_dimensions=additional_columns)
596
+
597
+ if len(filenames) == 1:
598
+ # End recursion and return non-tuple to make the case that the user
599
+ # called this with only one filename more intuitive
600
+ return new_epoch
601
+ else:
602
+ # Go into recursion
603
+ return (new_epoch,) + _as_tuple(
604
+ read_from_xyz(
605
+ *filenames[1:],
606
+ xyz_columns=xyz_columns,
607
+ normal_columns=normal_columns,
608
+ additional_dimensions=additional_dimensions,
609
+ **parse_opts,
610
+ )
611
+ )
612
+
613
+
614
+ def read_from_las(*filenames, normal_columns=[], additional_dimensions={}):
615
+ """Create an epoch from a LAS/LAZ file
616
+
617
+ :param filename:
618
+ The filename to read from. It is expected to be in LAS/LAZ format
619
+ and will be processed using laspy.
620
+ :type filename: str
621
+ :param normal_columns:
622
+ The column names of the normal vector components, e.g. "NormalX", "nx", "normal_x" etc., keep in mind that there
623
+ must be exactly 3 columns. Leave empty, if your data file does not contain normals.
624
+ :type normal_columns: list
625
+ :param additional_dimensions:
626
+ A dictionary, mapping names of additional data dimensions in the input
627
+ dataset to additional data dimensions in our epoch data structure.
628
+ :type additional_dimensions: dict
629
+ """
630
+
631
+ # Resolve the given path
632
+ filename = find_file(filenames[0])
633
+
634
+ # Read the lasfile using laspy
635
+ logger.info(f"Reading point cloud from file '{filename}'")
636
+ lasfile = laspy.read(filename)
637
+
638
+ cloud = np.vstack(
639
+ (
640
+ lasfile.x,
641
+ lasfile.y,
642
+ lasfile.z,
643
+ )
644
+ ).transpose()
645
+
646
+ normals = None
647
+ if normal_columns:
648
+ if len(normal_columns) != 3:
649
+ raise Py4DGeoError("normal_columns need to be a list of three strings!")
650
+
651
+ normals = np.vstack(
652
+ [
653
+ lasfile.points[normal_columns[0]],
654
+ lasfile.points[normal_columns[1]],
655
+ lasfile.points[normal_columns[2]],
656
+ ]
657
+ ).transpose()
658
+
659
+ # set scan positions
660
+ # build additional_dimensions dtype structure
661
+ additional_columns = np.empty(
662
+ shape=(cloud.shape[0], 1),
663
+ dtype=np.dtype([(name, "<f8") for name in additional_dimensions.values()]),
664
+ )
665
+ for column_id, column_name in additional_dimensions.items():
666
+ additional_columns[column_name] = np.array(
667
+ lasfile.points[column_id], dtype=np.int32
668
+ ).reshape(-1, 1)
669
+
670
+ # Construct Epoch and go into recursion
671
+ new_epoch = Epoch(
672
+ cloud,
673
+ normals=normals,
674
+ timestamp=lasfile.header.creation_date,
675
+ additional_dimensions=additional_columns,
676
+ )
677
+
678
+ if len(filenames) == 1:
679
+ # End recursion and return non-tuple to make the case that the user
680
+ # called this with only one filename more intuitive
681
+ return new_epoch
682
+ else:
683
+ # Go into recursion
684
+ return (new_epoch,) + _as_tuple(
685
+ read_from_las(
686
+ *filenames[1:],
687
+ normal_columns=normal_columns,
688
+ additional_dimensions=additional_dimensions,
689
+ )
690
+ )
691
+
692
+
693
+ def normalize_timestamp(timestamp):
694
+ """Bring a given timestamp into a standardized Python format"""
695
+
696
+ # This might be normalized already or non-existing
697
+ if isinstance(timestamp, datetime.datetime) or timestamp is None:
698
+ return timestamp
699
+
700
+ # This might be a date without time information e.g. from laspy
701
+ if isinstance(timestamp, datetime.date):
702
+ return datetime.datetime(timestamp.year, timestamp.month, timestamp.day)
703
+
704
+ # If this is a tuple of (year, day of year) as used in the LAS
705
+ # file header, we convert it.
706
+ if is_iterable(timestamp):
707
+ if len(timestamp) == 2:
708
+ return datetime.datetime(timestamp[0], 1, 1) + datetime.timedelta(
709
+ timestamp[1] - 1
710
+ )
711
+
712
+ # If this is a string we use the dateparser library that understands
713
+ # all sorts of human-readable timestamps
714
+ if isinstance(timestamp, str):
715
+ parsed = dateparser.parse(timestamp)
716
+
717
+ # dateparser returns None for anything it does not understand
718
+ if parsed is not None:
719
+ return parsed
720
+
721
+ raise Py4DGeoError(f"The timestamp '{timestamp}' was not understood by py4dgeo.")
722
+
723
+
724
+ def scan_positions_info_from_dict(info_dict: dict):
725
+ if info_dict is None:
726
+ return None
727
+ if not isinstance(info_dict, dict):
728
+ raise Py4DGeoError(f"The input scan position information should be dictionary.")
729
+ return None
730
+ # Compatible with both integer key and string key as index of the scan positions in json file
731
+ # load scan positions from dictionary, standardize loading via json format dumps to string key
732
+ scanpos_dict_load = json.loads(json.dumps(info_dict))
733
+ sps_list = []
734
+ for i in range(1, 1 + len(scanpos_dict_load)):
735
+ sps_list.append(scanpos_dict_load[str(i)])
736
+
737
+ for sp in sps_list:
738
+ sp_check = True
739
+ sp_check = False if len(sp["origin"]) != 3 else sp_check
740
+ sp_check = False if not isinstance(sp["sigma_range"], float) else sp_check
741
+ sp_check = False if not isinstance(sp["sigma_scan"], float) else sp_check
742
+ sp_check = False if not isinstance(sp["sigma_yaw"], float) else sp_check
743
+ if not sp_check:
744
+ raise Py4DGeoError("Scan positions load failed, please check format. ")
745
+ return sps_list