| 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 = [ |
| { |
| "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 = [ |
| { |
| "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", |
| "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", |
| |
| |
| |
| "method": "statistical", |
| "mean_k": 6, |
| "multiplier": 2.0 |
| }, |
| { |
| "type": "writers.gdal", |
| "filename": str(output_tif), |
| "resolution": resolution, |
| "output_type": "max", |
| "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", |
| |
| |
| |
| "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", |
| "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) |
|
|