-
Notifications
You must be signed in to change notification settings - Fork 0
/
las2chm
executable file
·83 lines (74 loc) · 1.8 KB
/
las2chm
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Daniel Rode
# Dependencies:
# python 3.12.6
# pdal 2.6.3
# gdal
# Created: 17 Sep 2024
# Updated: 18 Sep 2024
import sys
import json
import subprocess as sp
from sys import exit
from pathlib import Path
from datetime import datetime
HELP_TEXT = "Usage: lidar-chm.py IN_LAS_PATH OUT_TIFF_PATH"
def pdal(in_path, out_path):
pipeline = [
str(in_path),
# Height normalize
{
"type":"filters.hag_delaunay",
},
{
"type":"filters.ferry",
"dimensions":"HeightAboveGround=>Z",
},
# Select first returns and points above 2 meters (assuming your CRS using
# meters as its unit)
# {
# "type": "filters.expression",
# "expression": "(Z > 2) && (NumberOfReturns > 1 && ReturnNumber == 1)"
# },
# Values are set to zero (instead of being filtered out) so GDAL does not
# error out on empty pixels)
{
"type": "filters.assign",
"value": [
"Z = 0 WHERE Z < 2",
"Z = 0 WHERE (NumberOfReturns > 1 && ReturnNumber != 1)",
],
},
# Generate CHM
{
"resolution": 0.5,
"binmode": True,
"dimension": "Z",
# Available options: min, max, mean, idw, count, stdev, and all
"output_type": ["max"],
"gdaldriver": "GTiff",
"filename": str(out_path),
}
]
cmd = [
"pdal",
"pipeline",
"--stdin",
"--nostream",
"--progress", "./pdal.log",
]
Path("./pdal.log").touch()
sp.run(cmd, text=True, check=True, input=json.dumps(pipeline))
# Parse command line arguments
args = sys.argv[1:]
try:
in_path = Path(args[0])
out_path = Path(args[1])
except IndexError:
print(HELP_TEXT)
exit(1)
# Run PDAL
print(f"Starting {datetime.now()}")
pdal(in_path, out_path)
print(f"Finished {datetime.now()}")