1+ import os
2+ import pytest
3+ import tempfile
4+ import numpy as np
5+ import laspy
6+ import sys
7+ from pdaltools .las_rename_dimension import rename_dimension , main
8+ from pyproj import CRS
9+
10+ def create_test_las_file ():
11+ """Create a temporary LAS file with test data."""
12+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
13+ # Create a LAS file with some test points
14+ header = laspy .LasHeader (point_format = 3 , version = "1.4" )
15+ header .add_extra_dim (laspy .ExtraBytesParams (name = "test_dim" , type = np .float32 ))
16+ header .add_extra_dim (laspy .ExtraBytesParams (name = "test_dim2" , type = np .int32 ))
17+
18+ las = laspy .LasData (header )
19+
20+ crs_pyproj = CRS .from_string ("epsg:4326" )
21+ las .header .add_crs (crs_pyproj )
22+
23+ # Add some test points
24+ las .x = np .array ([1.0 , 2.0 , 3.0 ])
25+ las .y = np .array ([4.0 , 5.0 , 6.0 ])
26+ las .z = np .array ([7.0 , 8.0 , 9.0 ])
27+ las .test_dim = np .array ([10.0 , 11.0 , 12.0 ])
28+ las .test_dim2 = np .array ([12 , 13 , 14 ])
29+
30+ las .write (tmp_file .name )
31+ return tmp_file .name
32+
33+ def test_rename_dimension ():
34+ """Test renaming a dimension in a LAS file."""
35+ # Create a temporary input LAS file
36+ input_file = create_test_las_file ()
37+
38+ # Create temporary output file
39+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
40+ output_file = tmp_file .name
41+
42+ try :
43+ # Rename dimension using direct function call
44+ rename_dimension (input_file , output_file , ["test_dim" , "test_dim2" ], ["new_test_dim" , "new_test_dim2" ])
45+
46+ # Verify the dimension was renamed
47+ with laspy .open (output_file ) as las_file :
48+ las = las_file .read ()
49+ assert "new_test_dim" in las .point_format .dimension_names
50+ assert "test_dim" not in las .point_format .dimension_names
51+ assert "new_test_dim2" in las .point_format .dimension_names
52+ assert "test_dim2" not in las .point_format .dimension_names
53+
54+ # Verify the data is preserved
55+ np .testing .assert_array_equal (las .x , [1.0 , 2.0 , 3.0 ])
56+ np .testing .assert_array_equal (las .y , [4.0 , 5.0 , 6.0 ])
57+ np .testing .assert_array_equal (las .z , [7.0 , 8.0 , 9.0 ])
58+ np .testing .assert_array_equal (las ["new_test_dim" ], [10.0 , 11.0 , 12.0 ])
59+ np .testing .assert_array_equal (las ["new_test_dim2" ], [12 , 13 , 14 ])
60+ finally :
61+ # Clean up temporary files
62+ try :
63+ os .unlink (input_file )
64+ os .unlink (output_file )
65+ except :
66+ pass
67+
68+ def test_rename_nonexistent_dimension ():
69+ """Test attempting to rename a dimension that doesn't exist."""
70+ input_file = create_test_las_file ()
71+
72+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
73+ output_file = tmp_file .name
74+
75+ try :
76+ with pytest .raises (RuntimeError ):
77+ rename_dimension (input_file , output_file , ["nonexistent_dim" ], ["new_dim" ])
78+ finally :
79+ os .unlink (input_file )
80+ os .unlink (output_file )
81+
82+ def test_rename_to_existing_dimension ():
83+ """Test attempting to rename to an existing dimension."""
84+ input_file = create_test_las_file ()
85+
86+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
87+ output_file = tmp_file .name
88+
89+ try :
90+ with pytest .raises (ValueError ):
91+ rename_dimension (input_file , output_file , ["test_dim" ], ["x" ])
92+ finally :
93+ os .unlink (input_file )
94+ os .unlink (output_file )
95+
96+ def test_rename_dimension_case_sensitive ():
97+ """Test that dimension renaming is case-sensitive."""
98+ input_file = create_test_las_file ()
99+
100+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
101+ output_file = tmp_file .name
102+
103+ try :
104+ with pytest .raises (RuntimeError ):
105+ rename_dimension (input_file , output_file , ["TEST_DIM" ], ["new_dim" ])
106+ finally :
107+ os .unlink (input_file )
108+ os .unlink (output_file )
109+
110+
111+ def test_rename_dimension_main ():
112+ """Test renaming dimensions using the main() function."""
113+ # Create a temporary input LAS file
114+ input_file = create_test_las_file ()
115+
116+ # Create temporary output file
117+ with tempfile .NamedTemporaryFile (suffix = '.las' , delete = False ) as tmp_file :
118+ output_file = tmp_file .name
119+
120+ try :
121+ # Save original sys.argv
122+ original_argv = sys .argv
123+
124+ # Mock command-line arguments
125+ sys .argv = [
126+ "las_rename_dimension.py" , # script name
127+ input_file ,
128+ output_file ,
129+ "--old-dims" , "test_dim" , "test_dim2" ,
130+ "--new-dims" , "new_test_dim" , "new_test_dim2"
131+ ]
132+
133+ # Call main() function
134+ main ()
135+
136+ # Restore original sys.argv
137+ sys .argv = original_argv
138+
139+ # Verify the dimension was renamed
140+ with laspy .open (output_file ) as las_file :
141+ las = las_file .read ()
142+ assert "new_test_dim" in las .point_format .dimension_names
143+ assert "test_dim" not in las .point_format .dimension_names
144+ assert "new_test_dim2" in las .point_format .dimension_names
145+ assert "test_dim2" not in las .point_format .dimension_names
146+
147+ # Verify the data is preserved
148+ np .testing .assert_array_equal (las .x , [1.0 , 2.0 , 3.0 ])
149+ np .testing .assert_array_equal (las .y , [4.0 , 5.0 , 6.0 ])
150+ np .testing .assert_array_equal (las .z , [7.0 , 8.0 , 9.0 ])
151+ np .testing .assert_array_equal (las ["new_test_dim" ], [10.0 , 11.0 , 12.0 ])
152+ np .testing .assert_array_equal (las ["new_test_dim2" ], [12 , 13 , 14 ])
153+ finally :
154+ # Clean up temporary files
155+ try :
156+ os .unlink (input_file )
157+ os .unlink (output_file )
158+ except :
159+ pass
0 commit comments