forked from lmotta/scripts-for-gis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcp_bb_2_gcp.sh
84 lines (84 loc) · 1.99 KB
/
gcp_bb_2_gcp.sh
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
#
#
#### Functions
#
msg_error(){
local name_script=$(basename $0)
echo "Total of arguments is $runargs" >&2
echo "Usage: $name_script <image_original> <gcp_bb>" >&2
echo "<image_original> is the file of original image to warp" >&2
echo "<gcp_bb> is the file of GCP" >&2
exit 1
}
#
calc_params_image(){
local tmpinfo=$(mktemp)
#
gdalinfo $1 > $tmpinfo
# Origin
local origin=$(cat $tmpinfo | grep "Upper Left" | cut -d'(' -f2 | cut -d')' -f1)
x0=$(echo $origin | cut -d',' -f1)
y0=$(echo $origin | cut -d',' -f2)
# Resolution
local res_xy=$(cat $tmpinfo | grep "Pixel Size" | cut -d'(' -f2 | cut -d')' -f1)
res_x=$(echo $res_xy | cut -d',' -f1)
res_y=$(echo $res_xy | cut -d',' -f2)
#
rm $tmpinfo
}
#
calc_georef_xy(){
local pixel_x=$1
local pixel_y=$2
georef_x=$(echo "$x0 + $res_x * $pixel_x" | bc )
georef_y=$(echo "$y0 + $res_y * $pixel_y" | bc )
}
#
runargs=$#
totalargs=2
#
if [ $runargs -ne $totalargs ] ; then
msg_error
exit 1
fi
#
r_img=$1
r_gcp=$2
#
if [ ! -f "$r_img" ] ; then
printf "Not found file '%s'\n" $r_img
msg_error
exit 1
fi
if [ ! -f "$r_gcp" ] ; then
printf "Not found file '%s'\n" $r_gcp
msg_error
exit 1
fi
#
w_gcp_tmp=$(mktemp)
w_gcp_georef=$(dirname $r_gcp)"/"$(basename ${r_gcp%.*}"_georef.gcp")
w_gcp_raw=$(dirname $r_gcp)"/"$(basename ${r_gcp%.*}"_raw.gcp")
#
printf "Creating GCP's..."
#
touch $w_gcp_georef
touch $w_gcp_raw
tail -n +2 $r_gcp | awk '{printf("%s;%s;%s;%s\n", $2,$3,$4,$5)}' > $w_gcp_tmp
#
calc_params_image $r_img
for item in $(cat $w_gcp_tmp)
do
pixel_x=$(echo $item | cut -d';' -f1)
pixel_y=$(echo $item | cut -d';' -f2)
ref_x=$(echo $item | cut -d';' -f3)
ref_y=$(echo $item | cut -d';' -f4)
calc_georef_xy $pixel_x $pixel_y
georef_cr=$(echo $item | cut -d';' -f34)
echo "-gcp $pixel_x $pixel_y $ref_x $ref_y" >> $w_gcp_raw
echo "-gcp $georef_x $georef_y $ref_x $ref_y" >> $w_gcp_georef
done
rm $w_gcp_tmp
#
printf "\r%-100s\n" "'$w_gcp_georef' and '$w_gcp_raw' created!"