-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreview_rasters
More file actions
executable file
·394 lines (331 loc) · 13.3 KB
/
Copy pathpreview_rasters
File metadata and controls
executable file
·394 lines (331 loc) · 13.3 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
#!/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=("$@")
## Script imports
lib_dir="${script_dir}/../lib"
bash_functions_script="${lib_dir}/bash_script_func.sh"
## Source imports
source "$bash_functions_script"
## Arguments
target_srs=''
res_meters=10
tap=false
resampling_choices_common_arr=( 'near' 'bilinear' 'cubic' )
resampling='near'
outfile_single=''
do_thumb=false
do_hillshade=false
hillshade_single=''
no_resample=false
keep_downsampled=false
overwrite=false
srcfile_arr=()
## Custom globals
regexgrp_imgaux_minimum='<MDI key="STATISTICS_MINIMUM">(-?[0-9]+(\.[0-9]*)?)</MDI>'
regexgrp_imgaux_maximum='<MDI key="STATISTICS_MAXIMUM">(-?[0-9]+(\.[0-9]*)?)</MDI>'
## Script usage
read -r -d '' script_usage << EOM
Usage: ${script_name}
[-t_srs {target projection, same as gdalwarp option}]
[-tr {resolution (meters): <int>} (default=${res_meters})]
[-tap {same as gdalwarp option}]
[-r {resampling method: common options ($(string_join '|' "${resampling_choices_common_arr[@]}"))} (default=${resampling})]
[-o {downsampled raster file/dir path for single/multiple source file(s) (default is to add "_{resolution}m" suffix to source files)}
[--thumb]: scale source IMAGE min/max values to 0-255 and output downsampled raster in Byte format (incompatible with -t_srs and --hillshade options)
[--hillshade [output hillshade file/dir path for single/multiple source file(s)] (default is to add "_{resolution}m_shade" suffix to source files)]
[--no-resample]: make hillshade directly from source files, ignoring -tr -r -o arguments
[--keep-downsampled]: keep intermediate downsampled rasters if --hillshade is provided and -o is not provided
[--overwrite]: overwrite existing output downsampled and/or hillshade raster files
SRCFILE...
EOM
if (( $# < 1 )); then
echo_e "$script_usage"
exit_script_with_status 1
fi
## Parse arguments
set +u
while (( $# > 0 )); do
arg="$1"
if [ "$(string_startswith "$arg" '-')" = false ]; then
srcfile_arr+=( "$arg" )
else
arg_opt="$(string_lstrip "$arg" '-')"
arg_opt_nargs=''
if [ "$(string_contains "$arg_opt" '=')" = true ]; then
arg_val=$(printf '%s' "${arg_opt#*=}" | sed -r -e "s|^['\"]+||" -e "s|['\"]+$||")
arg_opt="${arg_opt%%=*}"
arg_opt_nargs_do_shift=false
else
arg_val="$2"
arg_opt_nargs_do_shift=true
fi
arg_val_can_start_with_dash=false
if [ "$arg_opt" = 'h' ] || [ "$arg_opt" = 'help' ]; then
arg_opt_nargs=0
echo "$script_usage"
exit 0
elif [ "$arg_opt" = 't_srs' ]; then
arg_opt_nargs=1
target_srs="$arg_val"
elif [ "$arg_opt" = 'tr' ]; then
arg_opt_nargs=1
res_meters="$arg_val"
elif [ "$arg_opt" = 'tap' ]; then
arg_opt_nargs=0
tap=true
elif [ "$arg_opt" = 'r' ]; then
arg_opt_nargs=1
resampling="$arg_val"
elif [ "$arg_opt" = 'o' ]; then
arg_opt_nargs=1
outfile_single="$arg_val"
keep_downsampled=true
elif [ "$arg_opt" = 'thumb' ]; then
arg_opt_nargs=0
do_thumb=true
elif [ "$arg_opt" = 'hillshade' ]; then
do_hillshade=true
if [ -n "$arg_val" ] && [ "$(string_startswith "$arg_val" '-')" = false ]; then
arg_opt_nargs=1
hillshade_single="$arg_val"
else
arg_opt_nargs=0
fi
elif [ "$arg_opt" = 'no-resample' ]; then
arg_opt_nargs=0
no_resample=true
elif [ "$arg_opt" = 'keep-downsampled' ]; then
arg_opt_nargs=0
keep_downsampled=true
elif [ "$arg_opt" = 'overwrite' ]; then
arg_opt_nargs=0
overwrite=true
else
arg_opt_nargs=0
if [ "$(re_test '^tr[0-9]+$' "$arg_opt")" = true ]; then
res_meters="${arg_opt/tr/}"
else
echo_e "Unexpected argument: ${arg}"
exit_script_with_status 1
fi
fi
if [ -z "$arg_opt_nargs" ]; then
echo_e "Developer error! "'$arg_opt_nargs'" was not set for argument: ${arg}"
exit_script_with_status 1
fi
if [ "$arg_opt_nargs_do_shift" = true ] && (( arg_opt_nargs >= 1 )); then
for arg_num in $(seq 1 $arg_opt_nargs); do
shift
arg_val="$1"
if [ -z "$arg_val" ]; then
echo_e "Missing expected value (#${arg_num}) for argument: ${arg}"
exit_script_with_status 1
elif [ "$arg_val_can_start_with_dash" = false ] && [ "$(string_startswith "$arg_val" '-')" = true ]; then
echo_e "Unexpected argument value: ${arg} ${arg_val}"
exit_script_with_status 1
fi
done
fi
fi
shift
done
set -u
## Validate arguments
if [ "$(string_is_posint "$res_meters")" = false ]; then
echo_e "Resolution argument must be a positive integer"
exit_script_with_status 1
fi
# Translate synonymous resampling options
if [ "$resampling" = 'nearest' ]; then
resampling='near'
elif [ "$resampling" = 'linear' ]; then
resampling='bilinear'
elif [ "$resampling" = 'bicubic' ]; then
resampling='cubic'
fi
if [ -n "$target_srs" ]; then
gdal_dsp_program='gdalwarp'
else
gdal_dsp_program='gdal_translate'
fi
if [ -n "$outfile_single" ]; then
if [ "$no_resample" = true ]; then
echo_e "-o and --no-resample arguments are incompatible"
exit_script_with_status 1
fi
if [ ! -d "$outfile_single" ] && (( ${#srcfile_arr[@]} > 1 )); then
echo_e "-o file path argument is incompatible with multiple source files"
exit_script_with_status 1
fi
fi
if [ "$do_thumb" = true ]; then
if [ -n "$target_srs" ] || [ "$tap" = true ]; then
echo_e "-t_srs/-tap and --thumb arguments are mutually exclusive"
exit_script_with_status 1
elif [ "$do_hillshade" = true ]; then
echo_e "--thumb and --hillshade arguments are mutually exclusive"
exit_script_with_status 1
fi
fi
if [ -n "$hillshade_single" ] && [ ! -d "$hillshade_single" ] && (( ${#srcfile_arr[@]} > 1 )); then
echo_e "--hillshade file path argument is incompatible with multiple source files"
exit_script_with_status 1
fi
if [ "$no_resample" = true ] && [ "$do_hillshade" = false ]; then
echo_e "--no-resample argument can only be provided with --hillshade argument"
exit_script_with_status 1
fi
outfile_provided_arr=( "$outfile_single" "$hillshade_single" )
if (( ${#srcfile_arr[@]} == 0 )); then
echo_e "At least one source file argument must be provided"
exit_script_with_status 1
fi
for srcfile in "${srcfile_arr[@]}"; do
if [ "$(itemOneOf "$srcfile" "${outfile_provided_arr[@]}")" = true ]; then
echo_e "Source file cannot also provided as an output file argument: ${srcfile}"
exit_script_with_status 1
fi
if [ ! -f "$srcfile" ]; then
echo_e "Source file does not exist: ${srcfile}"
exit_script_with_status 1
fi
done
## Do work
for srcfile in "${srcfile_arr[@]}"; do
srcfile_ext=${srcfile##*\.}
created_downsampled_raster=false
if [ "$do_hillshade" = true ]; then
# Set default output hillshade raster filename
if [ -n "$hillshade_single" ] && [ ! -d "$hillshade_single" ]; then
hillshade="$hillshade_single"
else
if [ "$no_resample" = true ]; then
hillshade=${srcfile/.${srcfile_ext}/_shade.${srcfile_ext}}
else
hillshade=${srcfile/.${srcfile_ext}/_${res_meters}m_shade.${srcfile_ext}}
fi
if [ -n "$hillshade_single" ] && [ -d "$hillshade_single" ]; then
hillshade="${hillshade_single}/$(basename "${hillshade}")"
fi
if (( ${#srcfile_arr[@]} == 1 )); then
echo "Output hillshade raster set automatically to: ${hillshade}"
fi
fi
# Handle existing output hillshade raster
if [ "$keep_downsampled" = false ] && [ -e "$hillshade" ] && [ "$overwrite" = false ]; then
echo "Skipping existing hillshade raster: ${hillshade}"
continue
fi
fi
if [ "$no_resample" = true ]; then
outfile="$srcfile"
else
# Set default output downsampled raster filename
if [ -n "$outfile_single" ] && [ ! -d "$outfile_single" ]; then
outfile="$outfile_single"
else
outfile="${srcfile/.${srcfile_ext}/}"
if [ "$(string_endswith "$outfile" "${res_meters}m")" = false ]; then
outfile="${outfile}_${res_meters}m"
fi
if [ "$do_thumb" = true ]; then
outfile="${outfile}_thumb"
fi
outfile="${outfile}.${srcfile_ext}"
if [ -n "$outfile_single" ] && [ -d "$outfile_single" ]; then
outfile="${outfile_single}/$(basename "${outfile}")"
fi
if (( ${#srcfile_arr[@]} == 1 )); then
echo "Output downsampled raster set automatically to: ${outfile}"
fi
fi
# Handle existing output downsampled raster
if [ -e "$outfile" ]; then
if [ "$overwrite" = true ]; then
echo "Existing downsampled raster will be overwritten"
elif [ "$do_hillshade" = false ]; then
echo "Skipping existing downsampled raster: ${outfile}"
continue
else
echo "Downsampled raster already exists: ${outfile}"
fi
fi
if [ ! -e "$outfile" ] || [ "$overwrite" = true ]; then
if [ "$do_thumb" = true ]; then
## Get image min/max stats
imgaux_file="${srcfile}.aux.xml"
echo "Getting image min/max stats from source raster: ${srcfile} -> ${imgaux_file}"
cmd="gdalinfo -stats ${srcfile}"
echo "$cmd"
eval "$cmd" 1>/dev/null
if [ ! -f "$imgaux_file" ]; then
echo_e "Expected image aux file does not exist: ${imgaux_file}"
exit_script_with_status 1
fi
imgaux_min=$(grep -Eoi "$regexgrp_imgaux_minimum" "$imgaux_file" | sed -r "s|${regexgrp_imgaux_minimum}|\1|")
imgaux_max=$(grep -Eoi "$regexgrp_imgaux_maximum" "$imgaux_file" | sed -r "s|${regexgrp_imgaux_maximum}|\1|")
if [ -z "$imgaux_min" ] || [ -z "$imgaux_max" ]; then
echo_e "Unable to parse image min/max stats from aux file: ${imgaux_file}"
exit_script_with_status 1
fi
echo "Removing image aux file: ${imgaux_file}"
rm "$imgaux_file"
fi
## Create downsampled raster
created_downsampled_raster=true
echo "Creating ${res_meters}m downsampled raster: ${srcfile} -> ${outfile}"
cmd="${gdal_dsp_program} ${srcfile} ${outfile} -tr ${res_meters} ${res_meters} -r ${resampling}"
if [ "$tap" = true ]; then
cmd="${cmd} -tap"
fi
if [ -n "$target_srs" ]; then
cmd="${cmd} -t_srs '${target_srs}'"
fi
if [ "$do_thumb" = true ]; then
cmd="${cmd} -ot Byte -scale ${imgaux_min} ${imgaux_max} 0 255"
fi
cmd="${cmd} -co TILED=YES -co BIGTIFF=IF_SAFER -co COMPRESS=LZW"
echo "$cmd"
eval "$cmd"
fi
fi
if [ "$do_hillshade" = true ]; then
# Handle existing output hillshade raster
if [ -e "$hillshade" ]; then
if [ "$overwrite" = true ]; then
echo "Existing hillshade raster will be overwritten"
else
echo "Skipping existing hillshade raster: ${hillshade}"
continue
fi
fi
if [ ! -e "$hillshade" ] || [ "$overwrite" = true ]; then
## Create hillshade raster
echo "Creating hillshade raster: ${outfile} -> ${hillshade}"
cmd="gdaldem hillshade \"${outfile}\" \"${hillshade}\" -z 3 -compute_edges -of GTiff -co TILED=YES -co BIGTIFF=IF_SAFER -co COMPRESS=LZW"
echo "$cmd"
eval "$cmd"
fi
## Remove downsampled raster if not needed
if [ "$outfile" = "$srcfile" ] || [ "$no_resample" = true ]; then
:
elif [ -n "$outfile_single" ] || [ "$keep_downsampled" = true ]; then
:
elif [ "$created_downsampled_raster" = true ]; then
echo "Removing temporary output downsampled raster: ${outfile}"
rm "$outfile"
fi
fi
done