-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathraster_coverage
More file actions
273 lines (225 loc) · 8.13 KB
/
raster_coverage
File metadata and controls
273 lines (225 loc) · 8.13 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
#!/bin/bash
## Bash settings
set -uo pipefail
## Script globals
script_file=$(readlink -f "${BASH_SOURCE[0]}"); export CURRENT_PARENT_BASH_SCRIPT_FILE="$script_file"
script_dir=$(dirname "$script_file")
script_name=$(basename "$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
src_raster=''
out_raster=''
out_vector=''
out_vector_ext='shp'
out_suffix='_coverage'
dryrun=false
## Custom globals
unsupported_vector_tweak_ext_arr=( 'gpkg' )
## Script usage
read -r -d '' script_usage << EOM
Usage: ${script_name} [--dryrun] SRC_RASTER
EOM
if (( $# < 1 )); then
echo_e -e "$script_usage"
exit_script_with_status 1
fi
## Parse arguments
set +u
while (( $# > 0 )); do
arg="$1"
if [ "$(string_startswith "$arg" '-')" = false ]; then
if [ -z "$src_raster" ]; then
src_raster="$arg"
fi
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" = 'r' ] || [ "$arg_opt" = 'out-raster' ]; then
arg_opt_nargs=1
if [ "$arg_opt_nargs_do_shift" = true ]; then
out_raster=true
else
out_raster="$arg_val"
fi
elif [ "$arg_opt" = 'v' ] || [ "$arg_opt" = 'out-vector' ]; then
arg_opt_nargs=1
if [ "$arg_opt_nargs_do_shift" = true ]; then
out_vector=true
else
out_vector="$arg_val"
fi
elif [ "$arg_opt" = 'vf' ] || [ "$arg_opt" = 'out-vector-format' ]; then
arg_opt_nargs=1
out_vector_ext=$(echo "${arg_val##*.}" | tr '[:upper:]' '[:lower:]')
elif [ "$arg_opt" = 'os' ] || [ "$arg_opt" = 'out-suffix' ]; then
arg_opt_nargs=1
out_suffix="$arg_val"
elif [ "$arg_opt" = 'dr' ] || [ "$arg_opt" = 'dryrun' ]; then
arg_opt_nargs=0
dryrun=true
else
echo_e "Unexpected argument: ${arg}"
exit_script_with_status 1
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
if [ "$arg_opt" = 'r' ] || [ "$arg_opt" = 'out_raster' ]; then
arg_val="$2"
if [ -n "$arg_val" ] && [ "$(string_startswith "$arg_val" '-')" = true ]; then
out_raster="$arg_val"
shift
fi
elif [ "$arg_opt" = 'v' ] || [ "$arg_opt" = 'out_vector' ]; then
arg_val="$2"
if [ -n "$arg_val" ] && [ "$(string_startswith "$arg_val" '-')" = true ]; then
out_vector="$arg_val"
shift
fi
else
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
fi
done
fi
fi
shift
done
set -u
## Validate arguments
if [ -z "$src_raster" ]; then
echo_e "SRC_RASTER argument must be provided"
exit_script_with_status 1
fi
## Adjust arguments
if [ -z "$out_raster" ] && [ -z "$out_vector" ]; then
out_raster=true
out_vector=true
fi
if [ -n "$out_raster" ]; then
keep_out_raster=true
else
keep_out_raster=false
fi
if [ -n "$out_vector" ]; then
make_out_vector=true
else
make_out_vector=false
fi
if [ -z "$out_raster" ] || [ "$out_raster" = true ]; then
if [ -z "$out_suffix" ]; then
echo_e "OUT_SUFFIX cannot be empty string when using default OUT_RASTER filepath"
exit_script_with_status 1
fi
out_raster="${src_raster%.*}${out_suffix}.${src_raster##*.}"
fi
if [ -z "$out_vector" ] || [ "$out_vector" = true ]; then
out_vector="${src_raster%.*}${out_suffix}.${out_vector_ext}"
fi
out_vector_ext=$(echo "${out_vector##*.}" | tr '[:upper:]' '[:lower:]')
out_vector_tmp="${out_vector%.*}_tmp.${out_vector_ext}"
## Main program
echo "Creating output raster coverage file: ${out_raster}"
cmd="\
gdal_translate -b mask -of COG -ot Byte \
-co OVERVIEWS=IGNORE_EXISTING -co RESAMPLING=NEAREST \
-co PREDICTOR=NO -co COMPRESS=DEFLATE \
\"${src_raster}\" \"${out_raster}\""
echo "$cmd"
if [ "$dryrun" = false ]; then
eval "$cmd"
if [ -f "$out_raster" ]; then
echo "Generated output coverage raster: ${out_raster}"
else
echo "Failed to generate output coverage raster: ${out_raster}"
exit_script_with_status 1
fi
fi
echo "Tweaking raster coverage file"
cmd="gdal_edit.py -a_nodata 0 \"${out_raster}\""
echo "$cmd"
if [ "$dryrun" = false ]; then
eval "$cmd"
fi
if [ "$make_out_vector" = false ]; then
exit 0
fi
echo "Creating output vector coverage file: ${out_vector_tmp}"
if [ "$out_vector_ext" = 'shp' ]; then
layer_name_tmp=$(basename "${out_vector_tmp%.*}")
else
layer_name_tmp=$(basename "${out_vector%.*}")
fi
cmd="gdal_polygonize.py -overwrite \"${out_raster}\" \"${out_vector_tmp}\" \"${layer_name_tmp}\""
echo "$cmd"
if [ "$dryrun" = false ]; then
eval "$cmd"
if [ -f "$out_vector_tmp" ]; then
echo "Generated output coverage vector file: ${out_vector_tmp}"
else
echo "Failed to generate output coverage vector file: ${out_vector_tmp}"
exit_script_with_status 1
fi
fi
if [ "$keep_out_raster" = false ]; then
echo "Removing temporary coverage raster file: ${out_raster}"
if [ "$dryrun" = false ]; then
rm "$out_raster"
fi
fi
if [ "$dryrun" = true ]; then
exit 0
fi
echo "Tweaking vector coverage file"
layer_name_out=$(basename "${out_vector%.*}")
if [ "$out_vector_ext" = 'gpkg' ]; then
geom_field_name='geom'
else
geom_field_name='geometry'
fi
ogr2ogr "${out_vector}" "${out_vector_tmp}" -nln "$layer_name_out" -dialect sqlite -sql "SELECT ST_Union(${geom_field_name}) AS ${geom_field_name} FROM ${layer_name_tmp}"
if [ -f "$out_vector_tmp" ]; then
echo "Generated final coverage vector file: ${out_vector}"
else
echo "Failed to generate output coverage vector: ${out_vector}"
exit_script_with_status 1
fi
rm -v ${out_vector_tmp%.*}*
if [ "$(itemOneOf "$out_vector_ext" "${unsupported_vector_tweak_ext_arr[@]}")" = true ]; then
echo "Skipping addition of vector coverage filename fields due to unsupported output vector file type: ${out_vector_ext}"
exit 0
fi
ogrinfo "${out_vector}" -sql "ALTER TABLE ${layer_name_out} DROP COLUMN FID"
ogrinfo "${out_vector}" -sql "ALTER TABLE ${layer_name_out} ADD COLUMN srcfile character(254)"
ogrinfo "${out_vector}" -sql "ALTER TABLE ${layer_name_out} ADD COLUMN srcf_stem character(254)"
ogrinfo "${out_vector}" -sql "ALTER TABLE ${layer_name_out} ADD COLUMN outfile character(254)"
ogrinfo "${out_vector}" -sql "ALTER TABLE ${layer_name_out} ADD COLUMN outf_stem character(254)"
ogrinfo "${out_vector}" -dialect SQLite -sql "UPDATE ${layer_name_out} SET srcfile='$(basename ${src_raster})', srcf_stem='$(basename ${src_raster%.*})', outfile='$(basename ${out_vector})', outf_stem='$(basename ${out_vector%.*})'"