-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbboxmerge
executable file
·60 lines (52 loc) · 1.7 KB
/
bboxmerge
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
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
function usage {
echo "Merge (union) multiple bounding boxes into a single bounding box"
echo ""
echo "usage: $PROGRAM_NAME <bbox>"
echo " - <bbox|-(bbox objects on stdin>: \$minx,\$miny,\$maxx,\$maxy"
exit 1
}
if test "$#" -lt 1; then
usage
fi
set -euo pipefail
# shellcheck disable=SC2124
BBOXS="${@:1}"
function get_min(){
local i="$1" # Save first argument in a variable
shift # Shift all arguments to the left (original $1 gets lost)
local vector=("$@") # Rebuild the array with rest of arguments
min=$(echo "${vector[0]}" | awk -F, "{print \$$i}")
for bbox in "${vector[@]}"; do
val=$(echo "$bbox" | awk -F, "{print \$$i}")
(( $(echo "$val < $min" | bc -l) )) && min=$val
done
echo "$min"
}
function get_max(){
local i="$1" # Save first argument in a variable
shift # Shift all arguments to the left (original $1 gets lost)
local vector=("$@") # Rebuild the array with rest of arguments
max=$(echo "${vector[0]}" | awk -F, "{print \$$i}")
for bbox in "${vector[@]}"; do
val=$(echo "$bbox" | awk -F, "{print \$$i}")
(( $(echo "$val > $max" | bc -l) )) && max=$val
done
echo "$max"
}
if [[ $BBOXS == "-" ]];then
# BBOX passed on stdin
if [ -t 0 ]; then
# if do not allow interactive tty
usage
fi
BBOXS=$(</dev/stdin)
BBOXS=$(echo "$BBOXS" | tr '\r\n' ' ')
fi
IFS=' ' read -r -a BBOXS_ARRAY <<< "$BBOXS"
min_x=$(get_min "1" "${BBOXS_ARRAY[@]}")
min_y=$(get_max "2" "${BBOXS_ARRAY[@]}")
max_x=$(get_max "3" "${BBOXS_ARRAY[@]}")
max_y=$(get_max "4" "${BBOXS_ARRAY[@]}")
echo "${min_x},${min_y},${max_x},${max_y}"