-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbboxcenter
executable file
·52 lines (44 loc) · 1.19 KB
/
bboxcenter
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
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
# shellcheck disable=SC2124
BBOX="${@}"
BBOX="${BBOX:--}"
BBOX="${BBOX//[[:space:]]/}"
function usage {
echo "Get center of bounding box. Requires AWK in PATH"
echo ""
echo "usage: $PROGRAM_NAME <bbox|-(bbox on stdin)> "
echo " - <bbox>: \$minx,\$miny,\$maxx,\$maxy"
exit 1
}
if test "$#" -lt 1; then
usage
fi
set -euo pipefail
function process_bbox(){
local bbox
bbox="$1"
xmin=$(echo "$bbox" | cut -d, -f1)
xmax=$(echo "$bbox" | cut -d, -f3)
ymin=$(echo "$bbox" | cut -d, -f2)
ymax=$(echo "$bbox" | cut -d, -f4)
x_range_half=$(awk "BEGIN {printf \"%.7f\n\", ($ymax - $ymin)/2}")
y_range_half=$(awk "BEGIN {printf \"%.7f\n\", ($xmax - $xmin)/2}")
center_x=$(awk "BEGIN {printf \"%.7f\n\", $x_range_half + $xmin}")
center_y=$(awk "BEGIN {printf \"%.7f\n\", $y_range_half + $ymin}")
echo "${center_x} ${center_y}"
}
if [[ $BBOX == "-" ]];then
# BBOX passed on stdin
if [ -t 0 ]; then
# if do not allow interactive tty
usage
fi
while read -r line
do
process_bbox "$line"
done < "/dev/stdin"
else
# BBOX passed as argument
process_bbox "$BBOX"
fi