~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/m.proj/m.proj

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
 
3
 
############################################################################
4
 
#
5
 
# MODULE:       m.proj
6
 
# AUTHOR:       M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
7
 
#                 New Zealand
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
13
 
#               for details.
14
 
#
15
 
#############################################################################
16
 
 
17
 
# notes:
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"
20
 
 
21
 
#%Module
22
 
#%  description: Convert coordinates from one projection to another (cs2cs frontend).
23
 
#%  keywords: miscellaneous, projection
24
 
#%End
25
 
#%option
26
 
#% key: input
27
 
#% type: string
28
 
#% gisprompt: old_file,file,file
29
 
#% description: Input coordinate file (omit to read from stdin)
30
 
#% required : no
31
 
#% key_desc : filename
32
 
#% guisection: Files & format
33
 
#%end
34
 
#%option
35
 
#% key: output
36
 
#% type: string
37
 
#% gisprompt: new_file,file,file
38
 
#% description: Output coordinate file (omit to send to stdout)
39
 
#% required : no
40
 
#% key_desc : filename
41
 
#% guisection: Files & format
42
 
#%end
43
 
#%option
44
 
#% key: fs
45
 
#% type: string
46
 
#% description: Field separator
47
 
#% required : no
48
 
#% key_desc : character
49
 
#% answer : |
50
 
#% guisection: Files & format
51
 
#%end
52
 
#%option
53
 
#% key: proj_in
54
 
#% type: string
55
 
#% description: Input projection parameters (PROJ.4 style)
56
 
#% required : no
57
 
#% guisection: Projections
58
 
#%end
59
 
#%option
60
 
#% key: proj_out
61
 
#% type: string
62
 
#% description: Output projection parameters (PROJ.4 style)
63
 
#% required : no
64
 
#% guisection: Projections
65
 
#%end
66
 
#%flag
67
 
#% key: i
68
 
#% description: Use LL WGS84 as input and current location as output projection
69
 
#% guisection: Projections
70
 
#%end
71
 
#%flag
72
 
#% key: o
73
 
#% description: Use current location as input and LL WGS84 as output projection
74
 
#% guisection: Projections
75
 
#%end
76
 
#%flag
77
 
#% key: d
78
 
#% description: Output long/lat in decimal degrees, or other projections with many decimal places
79
 
#% guisection: Files & format
80
 
#%end
81
 
#%flag
82
 
#% key: g
83
 
#% description: Script style output in CSV format respecting the field separator settings
84
 
#% guisection: Files & format
85
 
#%end
86
 
#%flag
87
 
#% key: v
88
 
#% description: Verbose mode (print projection parameters and filenames to stderr)
89
 
#%end
90
 
 
91
 
 
92
 
 
93
 
if [ -z "$GISBASE" ] ; then
94
 
   echo "You must be in GRASS GIS to run this program." 1>&2
95
 
   exit 1
96
 
fi
97
 
 
98
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
99
 
   exec g.parser "$0" "$@"
100
 
fi
101
 
 
102
 
# setting environment, so that awk works properly in all languages (needed?)
103
 
unset LC_ALL
104
 
LC_NUMERIC=C
105
 
export LC_NUMERIC
106
 
 
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"
110
 
   GRASS_VERBOSE=3
111
 
   export GRASS_VERBOSE
112
 
fi
113
 
 
114
 
#### check for cs2cs
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"
118
 
   exit 1
119
 
fi
120
 
 
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"
124
 
   exit 1
125
 
fi
126
 
if [ -n "$GIS_OPT_PROJ_OUT" ] && [ $GIS_FLAG_O -eq 1 ] ; then
127
 
   g.message -e "Choose only one output parameter method" 
128
 
   exit 1
129
 
fi
130
 
if [ $GIS_FLAG_I -eq 1 ] && [ $GIS_FLAG_O -eq 1 ] ; then
131
 
   g.message -e "Choose only one auto-projection parameter method"
132
 
   exit 1
133
 
fi
134
 
if [ -n "$GIS_OPT_OUTPUT" ] && [ -e "$GIS_OPT_OUTPUT" ] ; then
135
 
   g.message -e "Output file already exists" 
136
 
   exit 1
137
 
fi
138
 
 
139
 
#### parse field separator
140
 
if [ "$GIS_OPT_FS" = "space" ] || [ "$GIS_OPT_FS" = "tab" ] ; then
141
 
   fs=" "
142
 
else
143
 
   fs="`echo "$GIS_OPT_FS" | cut -c1`"
144
 
fi
145
 
 
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." 
150
 
    exit 1
151
 
fi
152
 
 
153
 
unset IN_PROJ
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."
157
 
fi
158
 
if [ $GIS_FLAG_O -eq 1 ] ; then
159
 
   IN_PROJ="`g.proj -jf`"
160
 
fi
161
 
if [ -n "$GIS_OPT_PROJ_IN" ] ; then
162
 
   IN_PROJ="$GIS_OPT_PROJ_IN"
163
 
fi
164
 
if [ -z "$IN_PROJ" ] ; then
165
 
   g.message -e "Missing input projection parameters."
166
 
   exit 1
167
 
fi
168
 
 
169
 
unset OUT_PROJ
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."
173
 
fi
174
 
if [ $GIS_FLAG_I -eq 1 ] ; then
175
 
   OUT_PROJ="`g.proj -jf`"
176
 
fi
177
 
if [ -n "$GIS_OPT_PROJ_OUT" ] ; then
178
 
   OUT_PROJ="$GIS_OPT_PROJ_OUT"
179
 
fi
180
 
if [ -z "$OUT_PROJ" ] ; then
181
 
   g.message -e "Missing output projection parameters."
182
 
   exit 1
183
 
fi
184
 
 
185
 
g.message message="input parameters=[$IN_PROJ]"
186
 
g.message message="output parameters=[$OUT_PROJ]"
187
 
 
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" 
192
 
   exit 1
193
 
fi
194
 
 
195
 
#### setup input file
196
 
if [ -z "$GIS_OPT_INPUT" ] || [ "$GIS_OPT_INPUT" = "-" ] ; then
197
 
   # read from stdin to temp file
198
 
   EXITCODE=0
199
 
   while [ $EXITCODE -eq 0 ] ; do
200
 
      unset REPLY
201
 
      read REPLY
202
 
      EXITCODE=$?
203
 
      if [ -n "$REPLY" ] ; then
204
 
         echo "$REPLY" >> "$TEMPFILE"
205
 
      fi
206
 
   done
207
 
 
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"
211
 
      exit 1
212
 
   fi
213
 
   infile="$TEMPFILE"
214
 
else
215
 
   infile="$GIS_OPT_INPUT"
216
 
fi
217
 
 
218
 
if [ ! -f "$infile" ] ; then
219
 
   g.message -e "Couldn't read input data." 
220
 
   exit 1
221
 
fi
222
 
 
223
 
#### setup output file
224
 
outfile="$GIS_OPT_OUTPUT"
225
 
 
226
 
if [ -n "$infile" ] ; then
227
 
    g.message -v message="input file=[$infile]"
228
 
fi
229
 
if [ -n "$outfile" ] ; then
230
 
    g.message -v message="output file=[$outfile]" 
231
 
fi
232
 
 
233
 
#### setup output style
234
 
if [ $GIS_FLAG_D -eq 0 ] ; then
235
 
   OUTFMT=""
236
 
else
237
 
   OUTFMT="-f %.8f"
238
 
fi
239
 
 
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'
243
 
 
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"
248
 
      EXITCODE=$?
249
 
   else
250
 
      cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ | tr -s "\t" "$GIS_OPT_FS" | tr -s " " "$GIS_OPT_FS" > "$outfile"
251
 
      EXITCODE=$?
252
 
 
253
 
      #### check if transform REALLY worked (e.g. bogus output if grid file not found)
254
 
      if [ -n "`head -n 1 "$outfile" | grep '^\*'`" ] ; then
255
 
         EXITCODE=1
256
 
      fi
257
 
   fi
258
 
else
259
 
   # write cs2cs output as is
260
 
   if [ -z "$outfile" ] ; then
261
 
      cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ
262
 
      EXITCODE=$?
263
 
   else
264
 
      cat "$infile" | tr "$fs" ' ' | cs2cs $OUTFMT $IN_PROJ +to $OUT_PROJ > "$outfile"
265
 
      EXITCODE=$?
266
 
 
267
 
      #### check if transform REALLY worked (e.g. bogus output if grid file not found)
268
 
      if [ -n "`head -n 1 "$outfile" | grep '^\*'`" ] ; then
269
 
         EXITCODE=1
270
 
      fi
271
 
   fi
272
 
fi
273
 
 
274
 
if [ $EXITCODE -ne 0 ] ; then
275
 
   g.message -w "Projection transform probably failed, please investigate."
276
 
fi
277
 
 
278
 
#### cleanup
279
 
if [ -e "$TEMPFILE" ] ; then
280
 
   rm -f "$TEMPFILE"
281
 
fi
282
 
 
283
 
exit 0