-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwkt2bbox
executable file
·43 lines (37 loc) · 1.2 KB
/
wkt2bbox
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
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
# shellcheck disable=SC2124
WKT_STRING="${@}"
WKT_STRING="${WKT_STRING:--}"
function usage {
echo "Converts POLYGON and BOX WKT objects to a bounding box string. WKT POLYGON object is required to be rectangular"
echo ""
echo "usage: $PROGRAM_NAME <wkt_string|-(wkt_string on stdin)>"
echo " arguments:"
echo " - <wkt_string>: POLYGON or BOX WKT string describing bounding box (arg or stdin)"
exit 1
}
if [ "$#" -lt 1 ]; then
usage
fi
set -euo pipefail
function process_wktstring() {
local input_string
input_string="$1"
if [[ "${input_string,,}" == *polygon* ]]; then
echo "$input_string" | perl -pe 's/POLYGON\s?\(\((-?\d+\.?\d*)\s(-?\d+\.?\d*),\s?-?\d+\.?\d*\s-?\d+\.?\d*,\s?(-?\d+\.?\d*)\s(-?\d+\.?\d*),.*$/$1,$2,$3,$4/i'
else
echo "$input_string" | perl -pe 's/BOX\((-?\d+\.?\d*)\s(-?\d+\.?\d*),\s?(-?\d+\.?\d*)\s(-?\d+\.?\d*)\).*$/$1,$2,$3,$4/i'
fi
}
if [[ $WKT_STRING == "-" ]]; then
if [ -t 0 ]; then
# if do not allow interactive tty
usage
fi
while read -r line; do
process_wktstring "$line"
done <"/dev/stdin"
else
process_wktstring "$WKT_STRING"
fi