-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimize_dem
More file actions
executable file
·145 lines (125 loc) · 4.21 KB
/
optimize_dem
File metadata and controls
executable file
·145 lines (125 loc) · 4.21 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
#!/bin/bash
## Bash settings
set -uo pipefail
## Script globals
script_name=$(basename "${BASH_SOURCE[0]}")
script_dir=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd)
script_dir_abs=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd -P)
script_file="${script_dir}/${script_name}"
if [ -L "${BASH_SOURCE[0]}" ]; then
script_file_abs=$(readlink "${BASH_SOURCE[0]}")
else
script_file_abs="${script_dir_abs}/${script_name}"
fi
export CURRENT_PARENT_BASH_SCRIPT_FILE="$script_file"
script_args=("$@")
## Arguments
demfile=''
outfile=''
num_threads=''
outfile_default_suffix='round-cog.tif'
dryrun=false
## Script usage
read -r -d '' script_usage << EOM
Usage:
${script_name} DEMFILE [OPTION]...
Use gdal_calc.py and gdal_translate to produce a COG copy of the
source demfile raster with DEM values rounded to 1/128 meters for
optimal LZW compression.
If the --outfile option is not provided, by default the output file
will be created next to the demfile with filename like
"DEMFILE_${outfile_default_suffix}".
Options:
-o,--outfile=<path>
Path to output rounded COG file.
-nt,--num-threads=<int or "ALL_CPUS">
Adds the GDAL GeoTIFF driver creation option 'NUM_THREADS'
with the provided value to the GDAL program calls.
More information on this creation option here:
https://gdal.org/drivers/raster/gtiff.html#open-options
-dr,--dryrun
Print 'ln' command used to create link, without executing.
EOM
if (( $# < 1 )); then
echo -e >/dev/stderr "$script_usage"
exit 1
fi
## Parse arguments
set +u
while (( $# > 0 )); do
arg="$1"
if ! [[ $arg == -* ]]; then
if [ -z "$demfile" ]; then
demfile="$arg"
else
echo >/dev/stderr "Unexpected argument: ${arg}"
exit 1
fi
else
if [ "$arg" = '-h' ] || [ "$arg" = '--help' ]; then
echo -e "$script_usage"
exit 0
elif [ "$arg" = '-dr' ] || [ "$arg" = '--dryrun' ]; then
dryrun=true
elif [ "$arg" = '-o' ] || [ "$arg" = '--outfile' ]; then
outfile="$2"; shift
elif [ "$arg" = '-nt' ] || [ "$arg" = '--num-threads' ]; then
num_threads="$2"; shift
else
echo >/dev/stderr "Unexpected argument: ${arg}"
exit 1
fi
fi
shift
done
set -u
if [ -z "$outfile" ]; then
outfile="${demfile%.*}_${outfile_default_suffix}"
fi
tmpfile="${outfile%.*}_tmp.tif"
## Validate arguments
if [ ! -f "$demfile" ]; then
echo >/dev/stderr "ERROR: Source demfile is not an existing file: ${demfile}"
exit 1
fi
if [ -f "$outfile" ]; then
echo >/dev/stderr "WARNING: Output file exists and will be overwritten: ${outfile}"
fi
## Do processing
echo "Checking source DEM NoData value"
cmd="gdalinfo ${demfile} | grep 'NoData Value' | cut -d= -f2"
echo -e "$cmd"
nodata_val=$(eval "$cmd")
if [ -n "$nodata_val" ]; then
nodata_arg="--NoDataValue=${nodata_val}"
else
nodata_arg=''
fi
echo "Creating intermediate rounded DEM: ${demfile} -> ${tmpfile}"
if [ -n "$num_threads" ]; then
num_threads_arg="--co NUM_THREADS=${num_threads}"
else
num_threads_arg=''
fi
cmd="gdal_calc.py --overwrite --format GTiff --co bigtiff=yes --co tiled=yes --co compress=lzw --co predictor=1 ${num_threads_arg} -A \"${demfile}\" --outfile=\"${tmpfile}\" --calc='round(A*128.0)/128.0' ${nodata_arg}"
echo -e "$cmd"
if [ "$dryrun" = false ]; then
eval "$cmd"
fi
echo "Converting rounded DEM to COG: ${tmpfile} -> ${outfile}"
if [ -n "$num_threads" ]; then
num_threads_arg="-co NUM_THREADS=${num_threads}"
else
num_threads_arg=''
fi
cmd="gdalwarp -ovr NONE -of COG -co bigtiff=yes -co overviews=ignore_existing -co resampling=bilinear -co compress=lzw -co predictor=3 ${num_threads_arg} \"${tmpfile}\" \"${outfile}\""
#cmd="gdal_translate -ovr NONE -of GTiff -co tiled=yes -co compress=lzw -co predictor=3 ${num_threads_arg} \"${tmpfile}\" \"${outfile}\"" # create GeoTIFF rather than COG
echo -e "$cmd"
if [ "$dryrun" = false ]; then
eval "$cmd"
fi
echo "Removing intermediate rounded DEM"
if [ "$dryrun" = false ]; then
rm "$tmpfile"
fi
echo "Done"