Skip to content

Commit

Permalink
Adjusted script to new demands
Browse files Browse the repository at this point in the history
Changed output coordinate system
Added new attributes to the metadata
 - Accepted boolean flag
 - TVU
 - HVU
  • Loading branch information
tobiaobr committed Oct 6, 2025
1 parent f780c67 commit 21c1183
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
36 changes: 29 additions & 7 deletions lasConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import numpy as np
import laspy
import pandas as pd
from pyproj import Transformer

def CSV_2_LAS(surveys_folders, output_folder, output_name="merged_final.las", chunk_size_bytes="64MB", max_points_per_file=10_000_000):

def CSV_2_LAS(surveys_folders, output_folder, output_name="merged_final1.las", chunk_size_bytes="64MB", max_points_per_file=10_000_000):
surveys_path = Path(surveys_folders)
output_folder = Path(output_folder)
output_folder.mkdir(parents=True, exist_ok=True)
transformer = Transformer.from_crs("EPSG:25832", "EPSG:4978", always_xy=True)


csv_files = list(surveys_path.rglob("*.csv"))
if not csv_files:
Expand All @@ -22,10 +26,14 @@ def CSV_2_LAS(surveys_folders, output_folder, output_name="merged_final.las", ch
pd.DataFrame(list(dir_to_id.items()), columns=["top_dir","id"]).to_csv(output_folder / "dir_mapping.csv", index=False)

# Create LAS header template
header = laspy.LasHeader(point_format=3, version="1.2")
header.scales = (0.001, 0.001, 0.001)
header.offsets = (0.0, 0.0, 0.0)
header = laspy.LasHeader(point_format=6, version="1.4")
header.scales = (1, 1, 1)
header.offsets = (0, 0, 0)

# Add extra dimensions
header.add_extra_dim(laspy.ExtraBytesParams(name="accepted", type=np.uint8))
header.add_extra_dim(laspy.ExtraBytesParams(name="TVU", type=np.float32))
header.add_extra_dim(laspy.ExtraBytesParams(name="THU", type=np.float32))

temp_folder = output_folder / "tmp"
temp_folder.mkdir(exist_ok=True)
Expand All @@ -51,20 +59,34 @@ def open_new_chunk_file():
for f in csv_files:
top_dir_id = dir_to_id[f.relative_to(surveys_path).parts[0]]
# Read CSV in Dask partitions (out-of-core)
ddf = dd.read_csv(str(f), header=None, usecols=[0,1,2], blocksize=chunk_size_bytes)
ddf = dd.read_csv(str(f), header=None, usecols=[0,1,2,3,4,5], blocksize=chunk_size_bytes)

for partition in ddf.to_delayed():
df = partition.compute()
x, y, z = df.iloc[:,0].to_numpy(), df.iloc[:,1].to_numpy(), df.iloc[:,2].to_numpy()

# Transform coordinates into EPSG:4978
x, y, z = transformer.transform(x, y, z)
accepted = (
df.iloc[:, 3].astype(str)
.str.strip()
.str.lower()
.eq("accepted")
.astype(np.uint8)
.to_numpy()
)
tvu = df.iloc[:, 4].to_numpy(dtype=np.float32)
thu = df.iloc[:, 5].to_numpy(dtype=np.float32)
ids = np.full(len(df), top_dir_id, dtype=np.uint16)

points = laspy.ScaleAwarePointRecord.zeros(len(df), header=header)
points.X = x
points.Y = y
points.Z = z
points.point_source_id = ids
points.return_number[:] = 1
points.number_of_returns[:] = 1
points["accepted"] = accepted
points["TVU"] = tvu
points["THU"] = thu

# Split points into multiple LAS chunks if needed
start_idx = 0
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pandas
numpy
laspy
pathlib
dask[dataframe]
dask[dataframe]
pyproj

0 comments on commit 21c1183

Please sign in to comment.