File size: 5,067 Bytes
0a7933d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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)