3
############################################################################
6
# AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
8
# PURPOSE: cs2cs reprojection frontend for a list of coordinates.
9
# Replacement for m.proj2 from GRASS 5
10
# COPYRIGHT: (c) 2006 Hamish Bowman, and the GRASS Development Team
11
# This program is free software under the GNU General Public
12
# License (>=v2). Read the file COPYING that comes with GRASS
15
#############################################################################
18
# - cs2cs expects "x y" data so be sure to send it "lon lat" not "lat lon"
19
# - if you send cs2cs a third data column, beware it might be treated as "z"
22
#% description: Convert coordinates from one projection to another (cs2cs frontend).
23
#% keywords: miscellaneous, projection
28
#% gisprompt: old_file,file,file
29
#% description: Input coordinate file (omit to read from stdin)
31
#% key_desc : filename
32
#% guisection: Files & format
37
#% gisprompt: new_file,file,file
38
#% description: Output coordinate file (omit to send to stdout)
40
#% key_desc : filename
41
#% guisection: Files & format
46
#% description: Field separator
48
#% key_desc : character
50
#% guisection: Files & format
55
#% description: Input projection parameters (PROJ.4 style)
57
#% guisection: Projections
62
#% description: Output projection parameters (PROJ.4 style)
64
#% guisection: Projections
68
#% description: Use LL WGS84 as input and current location as output projection
69
#% guisection: Projections
73
#% description: Use current location as input and LL WGS84 as output projection
74
#% guisection: Projections
78
#% description: Output long/lat in decimal degrees, or other projections with many decimal places
79
#% guisection: Files & format
83
#% description: Script style output in CSV format respecting the field separator settings
84
#% guisection: Files & format
88
#% description: Verbose mode (print projection parameters and filenames to stderr)
93
if [ -z "$GISBASE" ] ; then
94
echo "You must be in GRASS GIS to run this program." 1>&2
98
if [ "$1" != "@ARGS_PARSED@" ] ; then
99
exec g.parser "$0" "$@"
102
# setting environment, so that awk works properly in all languages (needed?)
107
# turn verbose mode on if requested either way
108
if [ "$GIS_FLAG_V" -eq 1 ] ; then
109
g.message -w "The verbosity option is superseded. Use --verbose instead"
115
if [ ! -x "`which cs2cs`" ] ; then
116
g.message -e "cs2cs program not found, install PROJ.4 first"
117
g.message "http://proj.maptools.org"
121
#### check for overenthusiasm
122
if [ -n "$GIS_OPT_PROJ_IN" ] && [ $GIS_FLAG_I -eq 1 ] ; then
123
g.message -e "Choose only one input parameter method"
126
if [ -n "$GIS_OPT_PROJ_OUT" ] && [ $GIS_FLAG_O -eq 1 ] ; then
127
g.message -e "Choose only one output parameter method"
130
if [ $GIS_FLAG_I -eq 1 ] && [ $GIS_FLAG_O -eq 1 ] ; then
131
g.message -e "Choose only one auto-projection parameter method"
134
if [ -n "$GIS_OPT_OUTPUT" ] && [ -e "$GIS_OPT_OUTPUT" ] ; then
135
g.message -e "Output file already exists"
139
#### parse field separator
140
if [ "$GIS_OPT_FS" = "space" ] || [ "$GIS_OPT_FS" = "tab" ] ; then
143
fs="`echo "$GIS_OPT_FS" | cut -c1`"
146
#### setup projection params
147
PROJ_TYPE=`g.region -p | grep '^proj' | cut -f2 -d" "`
148
if [ $PROJ_TYPE -eq 0 ] && ( [ $GIS_FLAG_I -eq 1 ] || [ $GIS_FLAG_O -eq 1 ] ) ; then
149
g.message -e "Cannot project to or from a XY location."
154
if [ $GIS_FLAG_I -eq 1 ] ; then
155
IN_PROJ="+proj=longlat +datum=WGS84"
156
g.message -v "Assuming LL WGS84 as input, current projection as output."
158
if [ $GIS_FLAG_O -eq 1 ] ; then
159
IN_PROJ="`g.proj -jf`"
161
if [ -n "$GIS_OPT_PROJ_IN" ] ; then
162
IN_PROJ="$GIS_OPT_PROJ_IN"
164
if [ -z "$IN_PROJ" ] ; then
165
g.message -e "Missing input projection parameters."
170
if [ $GIS_FLAG_O -eq 1 ] ; then
171
OUT_PROJ="+proj=longlat +datum=WGS84"
172
g.message -v "Assuming current projection as input, LL WGS84 as output."
174
if [ $GIS_FLAG_I -eq 1 ] ; then
175
OUT_PROJ="`g.proj -jf`"
177
if [ -n "$GIS_OPT_PROJ_OUT" ] ; then
178
OUT_PROJ="$GIS_OPT_PROJ_OUT"
180
if [ -z "$OUT_PROJ" ] ; then
181
g.message -e "Missing output projection parameters."
185
g.message message="input parameters=[$IN_PROJ]"
186
g.message message="output parameters=[$OUT_PROJ]"
188
#### setup temporary file
189
TEMPFILE="`g.tempfile pid=$$`"
190
if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
191
g.message -e "Unable to create temporary file"
195
#### setup input file
196
if [ -z "$GIS_OPT_INPUT" ] || [ "$GIS_OPT_INPUT" = "-" ] ; then
197
# read from stdin to temp file
199
while [ $EXITCODE -eq 0 ] ; do
203
if [ -n "$REPLY" ] ; then
204
echo "$REPLY" >> "$TEMPFILE"
208
# make sure we have at least one line of data
209
if [ `cat "$TEMPFILE" | wc -l` -eq 0 ] ; then
210
g.message -e "ERROR reading data from stdin"
215
infile="$GIS_OPT_INPUT"
218
if [ ! -f "$infile" ] ; then
219
g.message -e "Couldn't read input data."
223
#### setup output file
224
outfile="$GIS_OPT_OUTPUT"
226
if [ -n "$infile" ] ; then
227
g.message -v message="input file=[$infile]"
229
if [ -n "$outfile" ] ; then
230
g.message -v message="output file=[$outfile]"
233
#### setup output style
234
if [ $GIS_FLAG_D -eq 0 ] ; then
240
#### do the conversion
241
# Convert cs2cs DMS format to GRASS DMS format:
242
# cs2cs | sed -e 's/d/:/g' -e "s/'/:/g" -e 's/"//g'
244
# reformat strange cs2cs output to pretty CSV format
245
if [ "$GIS_FLAG_G" -eq 1 ] ; then
246
if [ -z "$outfile" ] ; then
247
cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ | tr -s "\t" "$GIS_OPT_FS" | tr -s " " "$GIS_OPT_FS"
250
cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ | tr -s "\t" "$GIS_OPT_FS" | tr -s " " "$GIS_OPT_FS" > "$outfile"
253
#### check if transform REALLY worked (e.g. bogus output if grid file not found)
254
if [ -n "`head -n 1 "$outfile" | grep '^\*'`" ] ; then
259
# write cs2cs output as is
260
if [ -z "$outfile" ] ; then
261
cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ
264
cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ > "$outfile"
267
#### check if transform REALLY worked (e.g. bogus output if grid file not found)
268
if [ -n "`head -n 1 "$outfile" | grep '^\*'`" ] ; then
274
if [ $EXITCODE -ne 0 ] ; then
275
g.message -w "Projection transform probably failed, please investigate."
279
if [ -e "$TEMPFILE" ] ; then