-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwktproj
executable file
·53 lines (48 loc) · 1.48 KB
/
wktproj
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
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
EPSG_IN=$1
EPSG_OUT=$2
WKT_STRING=${3:--}
function usage {
echo "Reproject WKT string using cs2cs from the Proj library"
echo ""
echo "usage: $PROGRAM_NAME <input_epsg_code> <output_epsg_code> <wkt_string|-(wkt_string on stdin)> "
echo " - <wkt_string>: single geometry WKT string"
echo " - <epsg_code>: [0-9]+"
exit 1
}
if test "$#" -ne 3; then
usage
fi
set -euo pipefail
function process_wkt_string() {
local wkt_string input_epsg_code output_epsg_code
input_epsg_code=$1
output_epsg_code=$2
wkt_string=$3
geom_type=$(echo "$wkt_string" | perl -pe 's/(.*?)\(\(.*?\)\)/$1/')
coord_string=$(echo "$wkt_string" | perl -pe 's/.*?\(\((.*?)\)\)/$1/')
IFS=","
result="${geom_type}(("
for coord in $coord_string; do
trans_coord=$(echo "$coord" | cs2cs -f "%.6f" "+init=epsg:$input_epsg_code" +to "+init=epsg:$output_epsg_code")
trans_xy=$(echo "$trans_coord" | perl -pe 's/(.*?)\s(.*?)\s.*/$1 $2/')
result="${result}${trans_xy},"
done
result="${result%?}"
result="${result}))"
echo "$result"
}
if [[ $WKT_STRING == "-" ]]; then
# WKT_STRING passed on stdin
if [ -t 0 ]; then
# if do not allow interactive tty
usage
fi
while read -r line; do
process_wkt_string "$EPSG_IN" "$EPSG_OUT" "$line"
done <"/dev/stdin"
else
# BBOX passed as argument
process_wkt_string "$EPSG_IN" "$EPSG_OUT" "$WKT_STRING"
fi