matchgeodem / scripts /process_las.py
paeslemesa's picture
Updated statcs/ splis/ and scrips/
0a7933d verified
Raw
History Blame Contribute Delete
5.07 kB
from pathlib import Path
import zipfile
from tqdm import tqdm
import json
import pdal
import time
#======================================================
#%%
KEY_ID = "IRN_JJ"
dirlaz = Path("/home/sabrina/Documents/Datasets/IRN_JJ")
OUT_RESOLUTION = 1.5
#======================================================
#%%
dirdem = Path(dirlaz, "dem")
dirdem.mkdir(exist_ok=True, parents=True)
filelaz = list(dirlaz.glob("*.laz"))
print(f"Found {len(filelaz)} LAZ files.")
#======================================================
def laz_to_dem(key_id, input_laz: Path, output_tif: Path, resolution=1.0):
"""
Convert a single LAZ file to DEM using PDAL.
"""
if key_id == "KAZ-AC" :
pipeline = [ # PLEIADES DATA DO NOT USE SIMPLE MORPHOLOGICAL FILTER (SMRF)
{
"type": "readers.las",
"filename": str(input_laz),
"spatialreference": "EPSG:32643"
},
{
"type": "writers.gdal",
"filename": str(output_tif),
"resolution": resolution,
"output_type": "max",
"data_type": "float32",
"nodata": -9999,
"gdalopts": "COMPRESS=DEFLATE|TILED=YES"
}
]
elif key_id == 'BRA-SP':
pipeline = [ # AIRBORNE DATA USE SMRF
{
"type": "readers.las",
"filename": str(input_laz)
},
{
"type": "filters.smrf",
"scalar": 1.25,
"slope": 0.15,
"threshold": 0.5,
"window": 16.0
},
{
"type": "writers.gdal",
"filename": str(output_tif),
"resolution": resolution,
"output_type": "max", # highest surface elevation per pixel
"data_type": "float32",
"nodata": -9999
}
]
elif key_id == 'CHN-YG':
pipeline = [
{
"type": "readers.las",
"filename": str(input_laz),
},
{
"type": "filters.range",
"limits": "Classification![7:7]"
},
{
"type": "filters.outlier",
# Optional: SfM point clouds often contain isolated spurious points
# above/below the surface that are not flagged as Class 7.
# This applies a statistical filter (radius 1.0 m, 6 neighbours).
"method": "statistical",
"mean_k": 6,
"multiplier": 2.0
},
{
"type": "writers.gdal",
"filename": str(output_tif),
"resolution": resolution,
"output_type": "max", # DSM: highest point per cell
"data_type": "float32",
"nodata": -9999,
"gdalopts": "COMPRESS=DEFLATE|TILED=YES|BIGTIFF=YES",
"override_srs": "EPSG:32648"
}
]
elif key_id == 'IRN_JJ':
pipeline = [
{
"type": "readers.las",
"filename": str(input_laz),
},
{
"type": "filters.assign",
# The metadata shows Class 0 only (Created, never classified).
# No noise class exists, so we skip filters.range.
# This filter is a no-op placeholder for clarity.
"assignment": "Classification[:]=0"
},
{
"type": "filters.outlier",
"method": "statistical",
"mean_k": 6,
"multiplier": 2.0
},
{
"type": "writers.gdal",
"filename": str(output_tif),
"resolution": resolution,
"output_type": "max", # DSM: highest point per cell
"data_type": "float32",
"nodata": -9999,
"gdalopts": "COMPRESS=DEFLATE|TILED=YES|BIGTIFF=YES",
}
]
else:
print("Worng key id")
quit
p = pdal.Pipeline(json.dumps(pipeline))
p.execute()
#======================================================
def batch_laz_to_dem(input_dir, output_dir, key_id, resolution=1.0):
input_dir = Path(input_dir)
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
laz_files = list(input_dir.glob("*.laz")) + list(input_dir.glob("*.las"))
for laz in tqdm(laz_files):
out_tif = output_dir / f"{laz.stem}.tif"
if Path(out_tif).exists == True:
print("File exists")
continue
else:
print(f"Processing: {laz.name}")
try:
laz_to_dem(input_laz=laz, output_tif= out_tif, resolution=resolution, key_id= key_id)
except Exception as e:
print(f"Error processing {laz.name}: {e}")
#======================================================
batch_laz_to_dem(input_dir = dirlaz,
output_dir = dirdem,
key_id = KEY_ID,
resolution = OUT_RESOLUTION)