-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessing.py
More file actions
427 lines (332 loc) · 17.4 KB
/
processing.py
File metadata and controls
427 lines (332 loc) · 17.4 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import os
import time
import json
import glob
import pdal
import laspy
import numpy as np
import subprocess
from datetime import timedelta
from tqdm import tqdm
from scipy.spatial import KDTree
import rasterio
import signal
import functools
import shutil
from matplotlib import pyplot as plt
from rasterio.warp import reproject, Resampling
import laspy
from shapely.geometry import box
from multiprocessing import get_context
import multiprocessing
from shapely.wkt import loads as wkt_loads, dumps as wkt_dumps
from core.processing_windowed import create_chunks_from_wkt, process_chunk_to_dsm, process_chunk_to_dem ,merge_chunks
def check_resolution(las_file, resolution, method="sampling", num_samples=10000):
"""
Checks if the DSM resolution is appropriate based on point cloud density.
Parameters:
las_file (str): Path to the LAS/LAZ file.
resolution (float): Desired DSM resolution.
method (str): "sampling" (nearest neighbor) or "density" (Poisson estimate).
num_samples (int): Number of random samples for nearest neighbor method.
Returns:
float: Estimated average point spacing.
bool: Whether the resolution is appropriate.
"""
with laspy.open(las_file) as file:
point_cloud = file.read()
points = np.vstack((point_cloud.x, point_cloud.y, point_cloud.z)).T
if len(points) == 0:
raise ValueError(f"Point cloud {las_file} is empty.")
if method == "sampling":
num_samples = min(num_samples, len(points))
sampled_points = points[np.random.choice(len(points), num_samples, replace=False)]
tree = KDTree(points)
distances, _ = tree.query(sampled_points, k=2)
avg_distance = np.mean(distances[:, 1]) # Ignore self-distance
elif method == "density":
extent_xy = (points[:, :2].max(axis=0) - points[:, :2].min(axis=0))
area = float(extent_xy[0] * extent_xy[1])
density = len(points) / area if area > 0 else float('inf') # pts / m²
avg_distance = (1.0 / density) ** 0.5 # m
else:
raise ValueError("Invalid method. Choose 'sampling' or 'density'.")
#if avg_distance > resolution:
#print(f"Warning: DSM resolution ({resolution}m) is finer than average point spacing ({avg_distance:.3f}m). "
#f"This may cause interpolation gaps.")
return avg_distance, avg_distance <= resolution
def get_las_footprint_wkt(las_file):
"""Extracts the WKT footprint (bounding box) from a LAS file."""
with laspy.open(las_file) as las:
header = las.header
min_x, min_y, max_x, max_y = header.min[0], header.min[1], header.max[0], header.max[1]
# Create a bounding box polygon
footprint = box(min_x, min_y, max_x, max_y)
return footprint.wkt # Convert to WKT format
def process_dsm_chunk_wrapper(args):
try:
las_file, large_chunk, small_chunk, output_dir, resolution = args
return process_chunk_to_dsm(las_file, large_chunk, small_chunk, output_dir, resolution)
except Exception as e:
return print(f"Error processing chunk for {las_file}: {e}")
def generate_dsm(input_folder, output_folder, run_name, method, resolution, chunk_size, chunk_overlap, num_workers, fill_gaps=True):
final_output_folder = os.path.join(output_folder, run_name, 'DSM')
os.makedirs(final_output_folder, exist_ok=True)
temp_folder = os.path.join(final_output_folder, "temp")
os.makedirs(temp_folder, exist_ok=True)
start_time = time.time()
las_files = glob.glob(os.path.join(input_folder, run_name, "*.las")) + \
glob.glob(os.path.join(input_folder, run_name, "*.laz"))
if not las_files:
print("No LAS/LAZ files found. Exiting DSM generation.")
return
for las_file in tqdm(las_files, desc="Processing LAS files", unit="file"):
target_wkt = get_las_footprint_wkt(las_file)
#avg_spacing, is_resolution_ok = check_resolution(las_file, resolution, method)
#if not is_resolution_ok:
# print(f"Warning: DSM resolution ({resolution}m) is finer than avg spacing ({avg_spacing:.3f}m).")
chunk_tasks = []
base_name = os.path.splitext(os.path.basename(las_file))[0]
temp_dsm_dir = os.path.join(temp_folder, base_name)
final_dsm_path = os.path.join(final_output_folder, f"{base_name}_DSM.tif")
if not os.path.exists(final_dsm_path):
print(f'saving temp files to {temp_dsm_dir}')
base_name = os.path.splitext(os.path.basename(las_file))[0]
temp_dsm_dir = os.path.join(temp_folder, base_name)
os.makedirs(temp_dsm_dir, exist_ok=True)
large_chunks, small_chunks = create_chunks_from_wkt(target_wkt, chunk_size, chunk_overlap)
for large_chunk, small_chunk in zip(large_chunks, small_chunks):
chunk_tasks.append((las_file, large_chunk, small_chunk, temp_dsm_dir, resolution))
with multiprocessing.Pool(processes=num_workers) as pool:
list(tqdm(
pool.imap_unordered(process_dsm_chunk_wrapper, chunk_tasks),
total=len(chunk_tasks),
desc="Processing DSM Chunks"))
chunk_files = sorted(glob.glob(os.path.join(temp_dsm_dir, "*.tif")))
if not chunk_files:
print(f"No DSM chunks found for {base_name}. Skipping.")
continue
merged_dsm = merge_chunks(chunk_files, final_dsm_path)
if fill_gaps and merged_dsm:
filled_dsm_path = os.path.join(temp_dsm_dir, f"{base_name}_filled.tif")
subprocess.run(["gdal_fillnodata.py", "-md", "10", "-si", "2", merged_dsm, filled_dsm_path],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
os.replace(filled_dsm_path, final_dsm_path)
#read output file for plotting
with rasterio.open(final_dsm_path) as src:
dsm_data = src.read(1)
dsm_nodata = src.nodata if src.nodata is not None else np.nan
dsm_data = np.where(dsm_data == dsm_nodata, np.nan, dsm_data)
# Plot the merged DSM and save as PNG
plt.figure(figsize=(10, 10))
plt.imshow(dsm_data, cmap='terrain', vmin=np.nanpercentile(dsm_data, 2), vmax=np.nanpercentile(dsm_data, 98))
plt.colorbar(label='Elevation (m)')
plt.title(f'DSM: {base_name}')
plt.axis('off')
plt.savefig(os.path.join(final_output_folder, f"{base_name}_DSM.png"), bbox_inches='tight', pad_inches=0.1, dpi=300)
plt.close() # Ensure we close the plot to free memory
shutil.rmtree(temp_dsm_dir, ignore_errors=True)
else:
print(f"Skipping {base_name}: File already exists.")
elapsed_time = timedelta(seconds=int(time.time() - start_time))
print(f"\nDSM generation completed in {elapsed_time}.")
def process_dtm_chunk_wrapper(args):
(las_file, large_chunk, small_chunk, output_dir, threshold,
scalar, slope, window, rigidness, iterations, resolution,
time_step, cloth_resolution, fill_gaps, filter_smrf, filter_csf) = args
return process_chunk_to_dem(input_file=las_file, large_chunk_bbox=large_chunk, small_chunk_bbox=small_chunk, temp_dir=output_dir, scalar=scalar, threshold=threshold, slope=slope, window=window, rigidness=rigidness, iterations=iterations, resolution=resolution, time_step=time_step, cloth_resolution=cloth_resolution, fill_gaps=fill_gaps, filter_smrf=filter_smrf, filter_csf=filter_csf)
def generate_dtm(input_folder, output_folder, run_name, resolution, chunk_size, fill_gaps, num_workers, method, chunk_overlap, threshold, scalar, slope, window, rigidness, iterations, time_step, cloth_resolution, filter_smrf, filter_csf):
final_output_folder = os.path.join(output_folder, run_name, 'DTM')
os.makedirs(final_output_folder, exist_ok=True)
temp_folder = os.path.join(final_output_folder, "temp")
os.makedirs(temp_folder, exist_ok=True)
start_time = time.time()
las_files = glob.glob(os.path.join(input_folder, run_name, "*.las")) + \
glob.glob(os.path.join(input_folder, run_name, "*.laz"))
if not las_files:
print("No LAS/LAZ files found. Exiting DTM generation.")
return
for las_file in tqdm(las_files, desc="Processing LAS files", unit="file"):
base_name = os.path.splitext(os.path.basename(las_file))[0]
temp_dtm_dir = os.path.join(temp_folder, base_name)
final_dtm_path = os.path.join(final_output_folder, f"{base_name}_DTM.tif")
if not os.path.exists(final_dtm_path):
target_wkt = get_las_footprint_wkt(las_file)
avg_spacing, is_resolution_ok = check_resolution(las_file, resolution, method)
if not is_resolution_ok:
print(f"Warning: DTM resolution ({resolution}m) is finer than avg spacing ({avg_spacing:.3f}m).")
base_name = os.path.splitext(os.path.basename(las_file))[0]
temp_dtm_dir = os.path.join(temp_folder, base_name)
os.makedirs(temp_dtm_dir, exist_ok=True)
large_chunks, small_chunks = create_chunks_from_wkt(
target_wkt,
chunk_size=chunk_size,
overlap=chunk_overlap
)
chunk_tasks = []
for large_chunk, small_chunk in zip(large_chunks, small_chunks):
chunk_tasks.append((
las_file, large_chunk, small_chunk, temp_dtm_dir,
threshold, # correct position
scalar, # correct position
slope, window, rigidness, iterations,
resolution, time_step, cloth_resolution,
fill_gaps, filter_smrf, filter_csf
))
with multiprocessing.Pool(processes=num_workers) as pool:
list(tqdm(
pool.imap_unordered(process_dtm_chunk_wrapper, chunk_tasks),
total=len(chunk_tasks),
desc="Processing DTM Chunks"))
chunk_files = sorted(glob.glob(os.path.join(temp_dtm_dir, "*.tif")))
if not chunk_files:
print(f"No DTM chunks found for {base_name}. Skipping.")
continue
merged_dtm = merge_chunks(chunk_files, final_dtm_path)
if fill_gaps and merged_dtm:
filled_dtm_path = os.path.join(temp_dtm_dir, f"{base_name}_filled.tif")
subprocess.run(
["gdal_fillnodata.py", "-md", "10", "-si", "2", merged_dtm, filled_dtm_path],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
os.replace(filled_dtm_path, final_dtm_path)
with rasterio.open(final_dtm_path) as src:
dtm_data = src.read(1)
dtm_nodata = src.nodata if src.nodata is not None else np.nan
dtm_data = np.where(dtm_data == dtm_nodata, np.nan, dtm_data)
# Plot the merged DSM and save as PNG
plt.figure(figsize=(10, 10))
plt.imshow(dtm_data, cmap='terrain', vmin=np.nanpercentile(dtm_data, 2), vmax=np.nanpercentile(dtm_data, 98))
plt.colorbar(label='Elevation (m)')
plt.title(f'DTM: {base_name}')
plt.axis('off')
plt.savefig(os.path.join(final_output_folder, f"{base_name}_DTM.png"), bbox_inches='tight', pad_inches=0.1)
plt.close() # Ensure we close the plot to free memory
shutil.rmtree(temp_dtm_dir, ignore_errors=True)
else:
print(f"Skipping {base_name}: DTM already exists.")
elapsed_time = timedelta(seconds=int(time.time() - start_time))
print(f"\nDTM generation completed in {elapsed_time}.")
def generate_chm(input_folder, output_folder, run_name):
dsm_folder = os.path.join(input_folder, run_name, "DSM")
dtm_folder = os.path.join(input_folder, run_name, "DTM")
chm_folder = os.path.join(output_folder, run_name, "CHM")
os.makedirs(chm_folder, exist_ok=True)
dsm_files = glob.glob(os.path.join(dsm_folder, "*.tif"))
print("\nStarting CHM generation")
start_time = time.time()
if not dsm_files:
print(f"No DSM files found in {dsm_folder}. Exiting CHM generation.")
return
for dsm_path in tqdm(dsm_files, desc="Processing CHMs", unit="file"):
try:
base_name = os.path.splitext(os.path.basename(dsm_path))[0].replace("_DSM", "")
dtm_path = os.path.join(dtm_folder, f"{base_name}_DTM.tif")
chm_output_path = os.path.join(chm_folder, f"{base_name}_CHM.tif")
#if os.path.exists(chm_output_path):
# print(f"Skipping {base_name}: CHM already exists.")
# continue
if not os.path.exists(dtm_path):
print(f"Skipping {base_name}: Corresponding DTM not found.")
continue
with rasterio.open(dsm_path) as dsm_src, rasterio.open(dtm_path) as dtm_src:
# Read DSM
dsm = dsm_src.read(1)
dsm_mask = dsm_src.read_masks(1)
dsm_meta = dsm_src.meta.copy()
dsm_nodata = dsm_src.nodata if dsm_src.nodata is not None else np.nan
# Prepare DTM to be aligned to DSM grid
dtm_aligned = np.full((dsm_src.height, dsm_src.width), dsm_nodata, dtype=np.float32)
reproject(
source=rasterio.band(dtm_src, 1),
destination=dtm_aligned,
src_transform=dtm_src.transform,
src_crs=dtm_src.crs,
dst_transform=dsm_src.transform,
dst_crs=dsm_src.crs,
resampling=Resampling.bilinear,
src_nodata=dtm_src.nodata,
dst_nodata=dsm_nodata
)
#Mask DTM where dsm_mask is 0
dtm_aligned[dsm_mask == 0] = dsm_nodata
# Compute CHM
chm = dsm - dtm_aligned
chm[(dsm == dsm_nodata) | (dtm_aligned == dsm_nodata)] = dsm_nodata
# Update metadata
dsm_meta.update({
"dtype": "float32",
"nodata": dsm_nodata,
"compress": "lzw"
})
with rasterio.open(chm_output_path, "w", **dsm_meta) as chm_dst:
chm_dst.write(chm.astype(np.float32), 1)
# plot merged_dsm and save as png
# set dsm_nodata to np.nan for plotting
chm = np.where(chm == dsm_nodata, np.nan, chm)
plt.figure(figsize=(10, 10))
plt.imshow(chm, cmap='viridis', vmin=np.nanpercentile(chm, 2), vmax=np.nanpercentile(chm, 98))
plt.colorbar(label='Elevation (m)')
plt.title(f'CHM: {base_name}')
plt.axis('off')
plt.savefig(os.path.join(chm_folder, f"{base_name}_CHM.png"), bbox_inches='tight', pad_inches=0.1)
plt.close()
except Exception as e:
print(f"[ERROR] Failed to process {base_name}: {e}")
elapsed_time = timedelta(seconds=int(time.time() - start_time))
print(f"\nCHM generation completed in {elapsed_time}.")
def process_all(config):
"""
Runs DSM generation using cleaned LAS files.
Reads from: `config.preprocessed_dir`
Saves to: `config.results_dir / run_name / .../`
"""
print('Starting Processing ...')
start_time = time.time()
if config.create_DSM:
print("\n========== Starting DSM Generation ==========")
generate_dsm(
input_folder=config.preprocessed_dir,
output_folder=config.results_dir,
run_name=config.run_name,
resolution=config.resolution,
chunk_size=config.chunk_size,
fill_gaps=config.fill_gaps,
num_workers=config.num_workers,
method=config.point_density_method,
chunk_overlap=config.chunk_overlap
)
if config.create_DEM:
print("\n========== Starting DEM Generation ==========")
generate_dtm(
input_folder=config.preprocessed_dir,
output_folder=config.results_dir,
run_name=config.run_name,
resolution=config.resolution,
chunk_size=config.chunk_size,
fill_gaps=config.fill_gaps,
method=config.point_density_method,
scalar=config.smrf_scalar,
slope=config.smrf_slope,
window=config.smrf_window_size,
rigidness = config.csf_rigidness,
time_step=config.csf_time_step,
cloth_resolution=config.csf_cloth_resolution,
iterations = config.csf_iterations,
num_workers= config.num_workers,
chunk_overlap=config.chunk_overlap,
filter_smrf=config.smrf_filter,
filter_csf=config.csf_filter,
threshold=config.threshold
)
if config.create_CHM:
print("\n========== Starting CHM Generation ==========")
generate_chm(
input_folder=config.results_dir,
output_folder=config.results_dir,
run_name=config.run_name
)
elapsed_time = timedelta(seconds=int(time.time() - start_time))
print(f"\n DEM generation completed in {elapsed_time}.\n")