-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbox2wkt
executable file
·55 lines (51 loc) · 1.39 KB
/
bbox2wkt
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
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
# shellcheck disable=SC2124
BBOXES="${@}"
BBOXES="${BBOXES:--}" # split string based on regex https://stackoverflow.com/a/45025569
function usage {
echo "Convert bounding box to WKT object"
echo ""
echo "usage: $PROGRAM_NAME <bbox>"
echo " arguments:"
echo " - <bbox>: \$minx,\$miny,\$maxx,\$maxy in EPSG:4326 (arg or stdin)"
exit 1
}
set -euo pipefail
function process_bbox() {
local input_string
local array
local minx
local maxx
local miny
local maxy
input_string=$1
if [[ -z $input_string ]]; then
return
fi
input_string=${input_string// /}
IFS=',' read -r -a array <<<"$input_string"
minx=${array[0]}
miny=${array[1]}
maxx=${array[2]}
maxy=${array[3]}
echo "POLYGON(($minx $miny,$minx $maxy,$maxx $maxy,$maxx $miny,$minx $miny))"
}
if [[ $BBOXES == "-" ]]; 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
IFS=$'\n'
# shellcheck disable=2013
for BBOX in $(grep --perl-regexp --only-matching '((-?\d*\.?\d*)\s?,\s?(-?\d*\.?\d*)\s?,\s?(-?\d*\.?\d*)\s?,\s?(-?\d*\.?\d*))\s?' <<<"$BBOXES"); do
BBOX="${BBOX//[[:space:]]/}"
process_bbox "$BBOX"
done
fi