pointtree 0.0.0__py3-none-any.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.
- pointtree/__init__.py +0 -0
- pointtree/config/__init__.py +5 -0
- pointtree/config/_config.py +19 -0
- pointtree/evaluation/__init__.py +7 -0
- pointtree/evaluation/_metrics.py +233 -0
- pointtree/evaluation/_time_tracker.py +45 -0
- pointtree/evaluation/_timer.py +29 -0
- pointtree/instance_segmentation/__init__.py +7 -0
- pointtree/instance_segmentation/_instance_segmentation_algorithm.py +232 -0
- pointtree/instance_segmentation/_multi_stage_algorithm.py +1218 -0
- pointtree/instance_segmentation/_priority_queue.py +107 -0
- pointtree/instance_segmentation/_utils.py +45 -0
- pointtree/io/__init__.py +19 -0
- pointtree/io/_base_point_cloud_reader.py +88 -0
- pointtree/io/_base_point_cloud_writer.py +90 -0
- pointtree/io/_csv_reader.py +83 -0
- pointtree/io/_csv_writer.py +76 -0
- pointtree/io/_las_reader.py +103 -0
- pointtree/io/_las_writer.py +145 -0
- pointtree/io/_point_cloud_io_data.py +19 -0
- pointtree/io/_point_cloud_reader.py +46 -0
- pointtree/io/_point_cloud_writer.py +45 -0
- pointtree/operations/__init__.py +9 -0
- pointtree/operations/_knn_search.py +277 -0
- pointtree/operations/_make_labels_consecutive.py +64 -0
- pointtree/operations/_pack_batch.py +61 -0
- pointtree/operations/_ravel_index.py +121 -0
- pointtree/operations/_voxel_downsampling.py +170 -0
- pointtree/visualization/__init__.py +7 -0
- pointtree/visualization/_color_palette.py +39 -0
- pointtree/visualization/_color_point_cloud.py +107 -0
- pointtree/visualization/_hex_to_rgb.py +29 -0
- pointtree/visualization/_save_tree_map.py +85 -0
- pointtree-0.0.0.dist-info/LICENSE +21 -0
- pointtree-0.0.0.dist-info/METADATA +114 -0
- pointtree-0.0.0.dist-info/RECORD +38 -0
- pointtree-0.0.0.dist-info/WHEEL +5 -0
- pointtree-0.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
""" Priority queue. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["PriorityQueue"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import heapq
|
|
7
|
+
from typing import Any, Hashable, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PriorityQueue:
|
|
11
|
+
"""
|
|
12
|
+
Priority queue that allows to update the priority of items in the queue.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
REMOVED = "_REMOVED_TASK"
|
|
16
|
+
|
|
17
|
+
def __init__(self):
|
|
18
|
+
self._pq = []
|
|
19
|
+
self._entry_map = {}
|
|
20
|
+
self._len = 0
|
|
21
|
+
|
|
22
|
+
def get(self, key: Hashable) -> Optional[Tuple[float, Any]]:
|
|
23
|
+
"""
|
|
24
|
+
Retrieves an item without removing it from the queue.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
key: The key of the item to retrieve.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Priority of the retrieved item and its data entry if the key exists, :code:`None` otherwise.
|
|
31
|
+
"""
|
|
32
|
+
if key in self._entry_map:
|
|
33
|
+
priority, key, entry = self._entry_map[key]
|
|
34
|
+
return priority, entry
|
|
35
|
+
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
def add(self, key: Hashable, entry: Any, priority: float) -> None:
|
|
39
|
+
"""
|
|
40
|
+
Adds a new item to the queue or updates the priority of an existing item.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
key: The key of the item to add or update.
|
|
44
|
+
entry: The data entry of the item.
|
|
45
|
+
priority: The priority of the item.
|
|
46
|
+
"""
|
|
47
|
+
if key in self._entry_map:
|
|
48
|
+
self.remove(key)
|
|
49
|
+
else:
|
|
50
|
+
self._len += 1
|
|
51
|
+
entry = [priority, key, entry]
|
|
52
|
+
self._entry_map[key] = entry
|
|
53
|
+
heapq.heappush(self._pq, entry)
|
|
54
|
+
|
|
55
|
+
def update(self, key: Hashable, new_entry: Any) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Updates the data entry of an item in the queue without changing its priority.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
key: The key of the item to update.
|
|
61
|
+
new_entry: New data entry for the item.
|
|
62
|
+
|
|
63
|
+
Raises:
|
|
64
|
+
KeyError: If the :code:`key` does not exist.
|
|
65
|
+
"""
|
|
66
|
+
if key not in self._entry_map:
|
|
67
|
+
raise KeyError(f"The key {key} does not exist.")
|
|
68
|
+
|
|
69
|
+
self._entry_map[key][-1] = new_entry
|
|
70
|
+
|
|
71
|
+
def remove(self, key: Hashable) -> None:
|
|
72
|
+
"""
|
|
73
|
+
Marks an existing item as REMOVED.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
key: The key of the item to be removed.
|
|
77
|
+
|
|
78
|
+
Raises:
|
|
79
|
+
KeyError: If the :code:`key` does not exist.
|
|
80
|
+
"""
|
|
81
|
+
entry = self._entry_map.pop(key)
|
|
82
|
+
entry[-1] = PriorityQueue.REMOVED
|
|
83
|
+
|
|
84
|
+
def pop(self) -> Tuple[float, Hashable, Any]:
|
|
85
|
+
"""
|
|
86
|
+
Removes and returns the lowest priority item.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
The priority, key, and data entry of the lowest priority item.
|
|
90
|
+
|
|
91
|
+
Raises:
|
|
92
|
+
KeyError: If the queue is empty.
|
|
93
|
+
"""
|
|
94
|
+
while self._pq:
|
|
95
|
+
priority, key, entry = heapq.heappop(self._pq)
|
|
96
|
+
if entry is not PriorityQueue.REMOVED:
|
|
97
|
+
self._len -= 1
|
|
98
|
+
del self._entry_map[key]
|
|
99
|
+
return priority, key, entry
|
|
100
|
+
raise KeyError("Cannot pop from an empty priority queue.")
|
|
101
|
+
|
|
102
|
+
def __len__(self) -> int:
|
|
103
|
+
"""
|
|
104
|
+
Returns:
|
|
105
|
+
Length of the priority queue.
|
|
106
|
+
"""
|
|
107
|
+
return self._len
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
""" Utilities for tree instance segmentation. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["remap_instance_ids"]
|
|
4
|
+
|
|
5
|
+
from typing import Tuple
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def remap_instance_ids(instance_ids: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
11
|
+
r"""
|
|
12
|
+
Remaps instance IDs that may not be continuous to a continuous range.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
instance_ids: Instance IDs to remap.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
Tuple of two arrays. The first contains the remapped instance IDs. The second contains the unique instance IDs
|
|
19
|
+
after remapping.
|
|
20
|
+
|
|
21
|
+
Shape:
|
|
22
|
+
- :code:`instance_ids`: :math:`(N)`
|
|
23
|
+
- Output: Tuple of two arrays. The first has shape :math:`(N)` and the second :math:`(I)`
|
|
24
|
+
|
|
25
|
+
| where
|
|
26
|
+
|
|
|
27
|
+
| :math:`N = \text{ number of points}`
|
|
28
|
+
| :math:`I = \text{ number of instances}`
|
|
29
|
+
"""
|
|
30
|
+
if len(instance_ids) == 0:
|
|
31
|
+
return instance_ids, np.empty(0, dtype=np.int64)
|
|
32
|
+
|
|
33
|
+
unique_instance_ids = np.unique(instance_ids)
|
|
34
|
+
unique_instance_ids = unique_instance_ids[unique_instance_ids != -1]
|
|
35
|
+
|
|
36
|
+
# map to continuous range of IDs
|
|
37
|
+
if len(unique_instance_ids) == 0 or unique_instance_ids.max() >= len(unique_instance_ids):
|
|
38
|
+
updated_instance_ids = np.copy(instance_ids)
|
|
39
|
+
for new_istance_id, instance_id in enumerate(unique_instance_ids):
|
|
40
|
+
updated_instance_ids[instance_ids == instance_id] = new_istance_id
|
|
41
|
+
unique_instance_ids = np.arange(len(unique_instance_ids), dtype=np.int64)
|
|
42
|
+
else:
|
|
43
|
+
updated_instance_ids = instance_ids
|
|
44
|
+
|
|
45
|
+
return updated_instance_ids, unique_instance_ids
|
pointtree/io/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
""" Tools for reading and writing point cloud files. """
|
|
2
|
+
|
|
3
|
+
__all__ = [
|
|
4
|
+
"CsvReader",
|
|
5
|
+
"CsvWriter",
|
|
6
|
+
"LasReader",
|
|
7
|
+
"LasWriter",
|
|
8
|
+
"PointCloudIoData",
|
|
9
|
+
"PointCloudReader",
|
|
10
|
+
"PointCloudWriter",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
from ._csv_reader import CsvReader
|
|
14
|
+
from ._csv_writer import CsvWriter
|
|
15
|
+
from ._las_reader import LasReader
|
|
16
|
+
from ._las_writer import LasWriter
|
|
17
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
18
|
+
from ._point_cloud_reader import PointCloudReader
|
|
19
|
+
from ._point_cloud_writer import PointCloudWriter
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
""" Abstract base class for implementing point cloud file readers. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["BasePointCloudReader"]
|
|
4
|
+
|
|
5
|
+
import abc
|
|
6
|
+
import os
|
|
7
|
+
from typing import List, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
import pandas
|
|
10
|
+
|
|
11
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BasePointCloudReader(abc.ABC):
|
|
15
|
+
"""Abstract base class for implementing point cloud file readers."""
|
|
16
|
+
|
|
17
|
+
@abc.abstractmethod
|
|
18
|
+
def supported_file_formats(self) -> List[str]:
|
|
19
|
+
"""
|
|
20
|
+
Returns:
|
|
21
|
+
File formats supported by the point cloud file reader.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def read(self, file_path: str, columns: Optional[List[str]] = None) -> PointCloudIoData:
|
|
25
|
+
"""
|
|
26
|
+
Reads a point cloud file.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
file_path: Path of the point cloud file to be read.
|
|
30
|
+
columns: Name of the point cloud columns to be read. The x, y, and z columns are always read.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
Point cloud object.
|
|
34
|
+
|
|
35
|
+
Raises:
|
|
36
|
+
ValueError: If the point cloud format is not supported by the reader.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
file_name = os.path.basename(file_path)
|
|
40
|
+
file_format = file_name.split(".")[-1]
|
|
41
|
+
file_id = file_name.rstrip(file_format)
|
|
42
|
+
if file_format not in self.supported_file_formats():
|
|
43
|
+
raise ValueError(f"The {file_format} format is not supported by the point cloud reader.")
|
|
44
|
+
|
|
45
|
+
if columns is not None:
|
|
46
|
+
columns = columns.copy()
|
|
47
|
+
|
|
48
|
+
# The x, y, z coordinates are always loaded.
|
|
49
|
+
for idx, coord in enumerate(["x", "y", "z"]):
|
|
50
|
+
if coord not in columns:
|
|
51
|
+
columns.insert(idx, coord)
|
|
52
|
+
|
|
53
|
+
point_cloud_df = self._read_points(file_path, columns=columns)
|
|
54
|
+
(x_max_resolution, y_max_resolution, z_max_resolution) = self._read_max_resolutions(file_path)
|
|
55
|
+
|
|
56
|
+
return PointCloudIoData(
|
|
57
|
+
point_cloud_df,
|
|
58
|
+
identifier=file_id,
|
|
59
|
+
x_max_resolution=x_max_resolution,
|
|
60
|
+
y_max_resolution=y_max_resolution,
|
|
61
|
+
z_max_resolution=z_max_resolution,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
@abc.abstractmethod
|
|
65
|
+
def _read_points(self, file_path: str, columns: Optional[List[str]] = None) -> pandas.DataFrame:
|
|
66
|
+
"""
|
|
67
|
+
Reads point data from a point cloud file. This method has to be overriden by child classes.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
file_path: Path of the point cloud file to be read.
|
|
71
|
+
columns: Name of the point cloud columns to be read.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Point cloud data.
|
|
75
|
+
"""
|
|
76
|
+
|
|
77
|
+
@staticmethod
|
|
78
|
+
@abc.abstractmethod
|
|
79
|
+
def _read_max_resolutions(file_path: str) -> Tuple[Optional[float], Optional[float], Optional[float]]:
|
|
80
|
+
"""
|
|
81
|
+
Reads the maximum resolution for each coordinate dimension from the point cloud file.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
file_path: Path of the point cloud file to be read.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Maximum resolution of the x-, y-, and z-coordinates of the point cloud.
|
|
88
|
+
"""
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
""" Abstract base class for implementing point cloud file writers. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["BasePointCloudWriter"]
|
|
4
|
+
|
|
5
|
+
import abc
|
|
6
|
+
from typing import List, Optional
|
|
7
|
+
|
|
8
|
+
import pandas
|
|
9
|
+
|
|
10
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BasePointCloudWriter(abc.ABC):
|
|
14
|
+
"""Abstract base class for implementing point cloud file writers."""
|
|
15
|
+
|
|
16
|
+
@abc.abstractmethod
|
|
17
|
+
def supported_file_formats(self) -> List[str]:
|
|
18
|
+
"""
|
|
19
|
+
Returns:
|
|
20
|
+
File formats supported by the point cloud file writer.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def write(self, point_cloud: PointCloudIoData, file_path: str, columns: Optional[List[str]] = None) -> None:
|
|
24
|
+
"""
|
|
25
|
+
Writes a point cloud to a file.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
point_cloud: Point cloud to be written.
|
|
29
|
+
file_path: Path of the output file.
|
|
30
|
+
columns: Point cloud columns to be written. The x, y, and z columns are always written.
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
ValueError: If the point cloud format is not supported by the writer or if `columns` contains a column name
|
|
34
|
+
that is not existing in the point cloud.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
file_format = file_path.split(".")[-1]
|
|
38
|
+
if file_format not in self.supported_file_formats():
|
|
39
|
+
raise ValueError(f"The {file_format} format is not supported by the point cloud writer.")
|
|
40
|
+
|
|
41
|
+
point_cloud_df = point_cloud.data
|
|
42
|
+
# pylint: disable=duplicate-code
|
|
43
|
+
if columns is not None:
|
|
44
|
+
columns = columns.copy()
|
|
45
|
+
|
|
46
|
+
# The x, y, z coordinates are always written.
|
|
47
|
+
for idx, coord in enumerate(["x", "y", "z"]):
|
|
48
|
+
if coord not in columns:
|
|
49
|
+
columns.insert(idx, coord)
|
|
50
|
+
|
|
51
|
+
missing_columns = set(columns).difference(set(point_cloud_df.columns))
|
|
52
|
+
if len(missing_columns) == 1:
|
|
53
|
+
raise ValueError(f"The point cloud does not contain a column named {list(missing_columns)[0]}.")
|
|
54
|
+
if len(missing_columns) > 1:
|
|
55
|
+
missing_columns_list = list(missing_columns)
|
|
56
|
+
missing_columns_str = f"{', '.join(missing_columns_list[:-1])}, and {missing_columns_list[-1]}"
|
|
57
|
+
raise ValueError(f"The point cloud does not contain columns named {missing_columns_str}.")
|
|
58
|
+
point_cloud_df = point_cloud_df[columns]
|
|
59
|
+
|
|
60
|
+
self._write_data(
|
|
61
|
+
point_cloud_df,
|
|
62
|
+
file_path,
|
|
63
|
+
x_max_resolution=point_cloud.x_max_resolution,
|
|
64
|
+
y_max_resolution=point_cloud.y_max_resolution,
|
|
65
|
+
z_max_resolution=point_cloud.z_max_resolution,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
@abc.abstractmethod
|
|
69
|
+
def _write_data(
|
|
70
|
+
self,
|
|
71
|
+
point_cloud: pandas.DataFrame,
|
|
72
|
+
file_path: str,
|
|
73
|
+
*,
|
|
74
|
+
x_max_resolution: Optional[float] = None,
|
|
75
|
+
y_max_resolution: Optional[float] = None,
|
|
76
|
+
z_max_resolution: Optional[float] = None,
|
|
77
|
+
) -> None:
|
|
78
|
+
"""
|
|
79
|
+
Writes a point cloud to a file. This method has to be overriden by child classes.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
point_cloud: Point cloud to be written.
|
|
83
|
+
file_path: Path of the output file.
|
|
84
|
+
x_max_resolution (float, optional): Maximum resolution of the point cloud's x-coordinates in meter. Defaults
|
|
85
|
+
to `None`.
|
|
86
|
+
y_max_resolution (float, optional): Maximum resolution of the point cloud's y-coordinates in meter. Defaults
|
|
87
|
+
to `None`.
|
|
88
|
+
z_max_resolution (float, optional): Maximum resolution of the point cloud's z-coordinates in meter. Defaults
|
|
89
|
+
to `None`.
|
|
90
|
+
"""
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
""" Point cloud file reader for csv and txt files. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["CsvReader"]
|
|
4
|
+
|
|
5
|
+
from typing import List, Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import pandas
|
|
8
|
+
|
|
9
|
+
from ._base_point_cloud_reader import BasePointCloudReader
|
|
10
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CsvReader(BasePointCloudReader):
|
|
14
|
+
"""Point cloud file reader for csv and txt files."""
|
|
15
|
+
|
|
16
|
+
def supported_file_formats(self) -> List[str]:
|
|
17
|
+
"""
|
|
18
|
+
Returns:
|
|
19
|
+
File formats supported by the point cloud file reader.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
return ["csv", "txt"]
|
|
23
|
+
|
|
24
|
+
def read(self, file_path: str, columns: Optional[List[str]] = None) -> PointCloudIoData:
|
|
25
|
+
"""
|
|
26
|
+
Reads a point cloud file.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
file_path: Path of the point cloud file to be read.
|
|
30
|
+
columns: Name of the point cloud columns to be read. The x, y, and z columns are always read.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
Point cloud object.
|
|
34
|
+
|
|
35
|
+
Raises:
|
|
36
|
+
ValueError: If the point cloud format is not supported by the reader.
|
|
37
|
+
"""
|
|
38
|
+
# The method from the base is called explicitly so that the read method appears in the documentation of this
|
|
39
|
+
# class.
|
|
40
|
+
return super().read(file_path, columns=columns)
|
|
41
|
+
|
|
42
|
+
def _read_points(self, file_path: str, columns: Optional[List[str]] = None) -> pandas.DataFrame:
|
|
43
|
+
"""
|
|
44
|
+
Reads point data from a point cloud file in csv or txt format.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
file_path: Path of the point cloud file to be read.
|
|
48
|
+
columns: Name of the point cloud columns to be read. The x, y, and z columns are always read.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
Point cloud data.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
file_format = file_path.split(".")[-1]
|
|
55
|
+
return pandas.read_csv(file_path, usecols=columns, sep="," if file_format == "csv" else " ")
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
def _read_max_resolutions(file_path: str) -> Tuple[float, float, float]:
|
|
59
|
+
"""
|
|
60
|
+
Reads the maximum resolution for each coordinate dimension from the point cloud file.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
file_path: Path of the point cloud file to be read.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
Maximum resolution of the x-, y-, and z-coordinates of the point cloud.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
file_format = file_path.split(".")[-1]
|
|
70
|
+
df = pandas.read_csv(file_path, usecols=["x", "y", "z"], sep="," if file_format == "csv" else " ", dtype=str)
|
|
71
|
+
|
|
72
|
+
# The precision of each coordinate is calculated by counting the digits after the decimal.
|
|
73
|
+
x_max_resolution = (
|
|
74
|
+
df["x"].str.split(".").apply(lambda x: round(0.1 ** len(x[1]), len(x)) if len(x) > 1 else 1).min()
|
|
75
|
+
)
|
|
76
|
+
y_max_resolution = (
|
|
77
|
+
df["y"].str.split(".").apply(lambda x: round(0.1 ** len(x[1]), len(x)) if len(x) > 1 else 1).min()
|
|
78
|
+
)
|
|
79
|
+
z_max_resolution = (
|
|
80
|
+
df["z"].str.split(".").apply(lambda x: round(0.1 ** len(x[1]), len(x)) if len(x) > 1 else 1).min()
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return x_max_resolution, y_max_resolution, z_max_resolution
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
""" Point cloud file writer for csv and txt files. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["CsvWriter"]
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from typing import List, Optional
|
|
7
|
+
|
|
8
|
+
import pandas
|
|
9
|
+
|
|
10
|
+
from ._base_point_cloud_writer import BasePointCloudWriter
|
|
11
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class CsvWriter(BasePointCloudWriter):
|
|
15
|
+
"""Point cloud file writer for csv and txt files."""
|
|
16
|
+
|
|
17
|
+
def supported_file_formats(self) -> List[str]:
|
|
18
|
+
"""
|
|
19
|
+
Returns:
|
|
20
|
+
File formats supported by the point cloud file writer.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
return ["csv", "txt"]
|
|
24
|
+
|
|
25
|
+
def write(self, point_cloud: PointCloudIoData, file_path: str, columns: Optional[List[str]] = None) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Writes a point cloud to a file.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
point_cloud: Point cloud to be written.
|
|
31
|
+
file_path: Path of the output file.
|
|
32
|
+
columns: Point cloud columns to be written. The x, y, and z columns are always written.
|
|
33
|
+
|
|
34
|
+
Raises:
|
|
35
|
+
ValueError: If the point cloud format is not supported by the writer or if `columns` contains a column name
|
|
36
|
+
that is not existing in the point cloud.
|
|
37
|
+
"""
|
|
38
|
+
# The method from the base is called explicitly so that the read method appears in the documentation of this
|
|
39
|
+
# class.
|
|
40
|
+
super().write(point_cloud, file_path, columns=columns)
|
|
41
|
+
|
|
42
|
+
def _write_data(
|
|
43
|
+
self,
|
|
44
|
+
point_cloud: pandas.DataFrame,
|
|
45
|
+
file_path: str,
|
|
46
|
+
*,
|
|
47
|
+
x_max_resolution: Optional[float] = None,
|
|
48
|
+
y_max_resolution: Optional[float] = None,
|
|
49
|
+
z_max_resolution: Optional[float] = None,
|
|
50
|
+
) -> None:
|
|
51
|
+
"""
|
|
52
|
+
Writes a point cloud to a file.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
point_cloud: Point cloud to be written.
|
|
56
|
+
file_path: Path of the output file.
|
|
57
|
+
x_max_resolution: Maximum resolution of the point cloud's x-coordinates in meter. Defaults to `None`.
|
|
58
|
+
y_max_resolution: Maximum resolution of the point cloud's y-coordinates in meter. Defaults to `None`.
|
|
59
|
+
z_max_resolution: Maximum resolution of the point cloud's z-coordinates in meter. Defaults to `None`.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
num_decimals = None
|
|
63
|
+
|
|
64
|
+
if x_max_resolution is not None:
|
|
65
|
+
num_decimals = math.ceil(-1 * min(0, math.log(x_max_resolution, 10)))
|
|
66
|
+
if y_max_resolution is not None:
|
|
67
|
+
num_decimals_y = math.ceil(-1 * min(0, math.log(y_max_resolution, 10)))
|
|
68
|
+
num_decimals = max(num_decimals, num_decimals_y) if num_decimals is not None else num_decimals_y
|
|
69
|
+
if z_max_resolution is not None:
|
|
70
|
+
num_decimals_z = math.ceil(-1 * min(0, math.log(z_max_resolution, 10)))
|
|
71
|
+
num_decimals = max(num_decimals, num_decimals_z) if num_decimals is not None else num_decimals_z
|
|
72
|
+
|
|
73
|
+
float_format = f"%.{num_decimals}f" if num_decimals is not None else None
|
|
74
|
+
|
|
75
|
+
file_format = file_path.split(".")[-1]
|
|
76
|
+
point_cloud.to_csv(file_path, sep="," if file_format == "csv" else " ", index=False, float_format=float_format)
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
""" Point cloud file reader for las and laz files. """
|
|
2
|
+
|
|
3
|
+
__all__ = ["LasReader"]
|
|
4
|
+
|
|
5
|
+
from typing import List, Optional, Tuple
|
|
6
|
+
|
|
7
|
+
import laspy
|
|
8
|
+
import numpy
|
|
9
|
+
import pandas
|
|
10
|
+
|
|
11
|
+
from ._base_point_cloud_reader import BasePointCloudReader
|
|
12
|
+
from ._point_cloud_io_data import PointCloudIoData
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class LasReader(BasePointCloudReader):
|
|
16
|
+
"""Point cloud file reader for las and laz files."""
|
|
17
|
+
|
|
18
|
+
_standard_field_defaults = {
|
|
19
|
+
"return_number": 1,
|
|
20
|
+
"number_of_returns": 1,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
def __init__(self, ignore_default_columns: bool = True) -> None:
|
|
24
|
+
"""
|
|
25
|
+
Args:
|
|
26
|
+
ignore_default_columns: Whether fields of a las or laz file that only contain default values should be
|
|
27
|
+
ignored. Defaults to `True`.
|
|
28
|
+
"""
|
|
29
|
+
super().__init__()
|
|
30
|
+
self._ignore_default_columns = ignore_default_columns
|
|
31
|
+
|
|
32
|
+
def supported_file_formats(self) -> List[str]:
|
|
33
|
+
"""
|
|
34
|
+
Returns:
|
|
35
|
+
File formats supported by the point cloud file reader.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
return ["las", "laz"]
|
|
39
|
+
|
|
40
|
+
def read(self, file_path: str, columns: Optional[List[str]] = None) -> PointCloudIoData:
|
|
41
|
+
"""
|
|
42
|
+
Reads a point cloud file.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
file_path: Path of the point cloud file to be read.
|
|
46
|
+
columns: Name of the point cloud columns to be read. The x, y, and z columns are always read.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Point cloud object.
|
|
50
|
+
|
|
51
|
+
Raises:
|
|
52
|
+
ValueError: If the point cloud format is not supported by the reader.
|
|
53
|
+
"""
|
|
54
|
+
# The method from the base is called explicitly so that the read method appears in the documentation of this
|
|
55
|
+
# class.
|
|
56
|
+
return super().read(file_path, columns=columns)
|
|
57
|
+
|
|
58
|
+
def _read_points(self, file_path: str, columns: Optional[List[str]] = None) -> pandas.DataFrame:
|
|
59
|
+
"""
|
|
60
|
+
Reads point data from a point cloud file in las or laz format.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
file_path: Path of the point cloud file to be read.
|
|
64
|
+
columns: Name of the point cloud columns to be read. The x, y, and z columns are always read.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Point cloud data.
|
|
68
|
+
"""
|
|
69
|
+
las_data = laspy.read(file_path)
|
|
70
|
+
data = numpy.array([las_data.x, las_data.y, las_data.z]).T
|
|
71
|
+
point_cloud_df = pandas.DataFrame(data, columns=["x", "y", "z"])
|
|
72
|
+
|
|
73
|
+
for column_name in las_data.header.point_format.standard_dimension_names:
|
|
74
|
+
if column_name.lower() in ["x", "y", "z"] or columns is not None and column_name not in columns:
|
|
75
|
+
continue
|
|
76
|
+
column_values = numpy.array(las_data[column_name])
|
|
77
|
+
default_value = LasReader._standard_field_defaults.get(column_name, 0)
|
|
78
|
+
if (column_values != default_value).any() or not self._ignore_default_columns:
|
|
79
|
+
point_cloud_df[column_name] = column_values
|
|
80
|
+
|
|
81
|
+
for column_name in las_data.header.point_format.extra_dimension_names:
|
|
82
|
+
if column_name.lower() in ["x", "y", "z"] or columns is not None and column_name not in columns:
|
|
83
|
+
continue
|
|
84
|
+
point_cloud_df[column_name] = las_data[column_name]
|
|
85
|
+
|
|
86
|
+
return point_cloud_df
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def _read_max_resolutions(file_path: str) -> Tuple[float, float, float]:
|
|
90
|
+
"""
|
|
91
|
+
Reads the maximum resolution for each coordinate dimension from the point cloud file.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
file_path: Path of the point cloud file to be read.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Maximum resolution of the x-, y-, and z-coordinates of the point cloud.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
with laspy.open(file_path, "r") as f:
|
|
101
|
+
scales = f.header.scales
|
|
102
|
+
|
|
103
|
+
return scales
|