Skip to content

Commit 6e2b228

Browse files
authored
Make value take offsets (#335)
* Added a simple test to compare x-coordinates between pyntcloud and laspy * Add Y and Z * Correction of error * Since the offset and scale of las data are double (float64), correct the default values. * Add test data(las). * Add test case for new data. * Adding offsets to coordinates. * Add test to check dtype of las data (there must be a better way...) * Add offsets for pylas. * Remove superfluous imports. * Lowercase properties are used to obtain the dimensions of the LAS data.
1 parent 2f7c921 commit 6e2b228

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

pyntcloud/io/las.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import numpy as np
2+
13
try:
24
import laspy
35
except ImportError:
@@ -50,9 +52,12 @@ def read_las_with_laspy(filename):
5052
with laspy.open(filename) as las_file:
5153
las = las_file.read()
5254
data["points"] = pd.DataFrame(las.points.array)
53-
data["points"].columns = (x.lower() for x in data["points"].columns)
54-
# because laspy do something strange with scale
55-
data["points"].loc[:, ["x", "y", "z"]] *= las.header.scales
55+
data["points"].columns = (name.lower() for name in data["points"].columns)
56+
57+
data["points"]["x"] = pd.Series(np.array(las.x))
58+
data["points"]["y"] = pd.Series(np.array(las.y))
59+
data["points"]["z"] = pd.Series(np.array(las.z))
60+
5661
data["las_header"] = las.header
5762
return data
5863

@@ -64,9 +69,12 @@ def read_las_with_pylas(filename):
6469
with pylas.open(filename) as las_file:
6570
las = las_file.read()
6671
data["points"] = pd.DataFrame(las.points)
67-
data["points"].columns = (x.lower() for x in data["points"].columns)
68-
# because pylas do something strange with scale
69-
data["points"].loc[:, ["x", "y", "z"]] *= las.header.scales
72+
data["points"].columns = (name.lower() for name in data["points"].columns)
73+
74+
data["points"]["x"] = pd.Series(np.array(las.x))
75+
data["points"]["y"] = pd.Series(np.array(las.y))
76+
data["points"]["z"] = pd.Series(np.array(las.z))
77+
7078
data["las_header"] = las.header
7179
return data
7280

tests/data/has_offsets.las

747 Bytes
Binary file not shown.

tests/integration/io/test_from_file.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44

55
from pyntcloud import PyntCloud
6+
import laspy
67

78

89
def assert_points_xyz(data):
@@ -51,7 +52,9 @@ def test_from_file(data_path, extension, color, mesh, comments):
5152
if extension == ".laz":
5253
pytest.xfail("TODO: Review laz decompression error")
5354
cloud = PyntCloud.from_file(str(data_path / "diamond{}".format(extension)))
55+
5456
assert_points_xyz(cloud)
57+
5558
if color:
5659
assert_points_color(cloud)
5760
if mesh:
@@ -76,6 +79,7 @@ def test_obj_issue_226(data_path):
7679

7780
assert "w" in cloud.points.columns
7881

82+
7983
def test_obj_issue_vn(data_path):
8084
"""
8185
Fix type issue in pyntcloud/io/obj.py.
@@ -99,3 +103,51 @@ def test_ply_with_bool(data_path):
99103
cloud = PyntCloud.from_file(filename=TEST_PLY, allow_bool=True)
100104
assert "is_green" in cloud.points.columns, "Failed to find expected Boolean column: 'is_green'"
101105
assert cloud.points.is_green.dtype == bool, "Boolean column no loaded as bool dtype"
106+
107+
108+
def test_simple_las_issue_333(data_path):
109+
""" Regression test https://github.com/daavoo/pyntcloud/issues/333
110+
"""
111+
las_file_name = (str(data_path / "simple.las"))
112+
cloud = PyntCloud.from_file(las_file_name)
113+
points = cloud.points
114+
115+
x_point_pyntcloud = points["x"][0]
116+
y_point_pyntcloud = points["y"][0]
117+
z_point_pyntcloud = points["z"][0]
118+
119+
with laspy.open(las_file_name) as las_file:
120+
las = las_file.read()
121+
header = las.header
122+
123+
x_point_laspy = (las.X[0] * header.x_scale) + header.x_offset
124+
y_point_laspy = (las.Y[0] * header.y_scale) + header.y_offset
125+
z_point_laspy = (las.Z[0] * header.z_scale) + header.z_offset
126+
127+
assert x_point_pyntcloud == x_point_laspy.astype("float32")
128+
assert y_point_pyntcloud == y_point_laspy.astype("float32")
129+
assert z_point_pyntcloud == z_point_laspy.astype("float32")
130+
131+
132+
def test_has_offsets_las_issue_333(data_path):
133+
""" Regression test https://github.com/daavoo/pyntcloud/issues/333
134+
"""
135+
las_file_name = (str(data_path / "has_offsets.las"))
136+
cloud = PyntCloud.from_file(las_file_name)
137+
points = cloud.points
138+
139+
x_point_pyntcloud = points["x"][0]
140+
y_point_pyntcloud = points["y"][0]
141+
z_point_pyntcloud = points["z"][0]
142+
143+
with laspy.open(las_file_name) as las_file:
144+
las = las_file.read()
145+
header = las.header
146+
147+
x_point_laspy = (las.X[0] * header.x_scale) + header.x_offset
148+
y_point_laspy = (las.Y[0] * header.y_scale) + header.y_offset
149+
z_point_laspy = (las.Z[0] * header.z_scale) + header.z_offset
150+
151+
assert x_point_pyntcloud == x_point_laspy.astype("float32")
152+
assert y_point_pyntcloud == y_point_laspy.astype("float32")
153+
assert z_point_pyntcloud == z_point_laspy.astype("float32")

0 commit comments

Comments
 (0)