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

« back to all changes in this revision

Viewing changes to scripts/v.in.mapgen/v.in.mapgen

  • 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:       v.in.mapgen
6
 
#
7
 
# AUTHOR(S):    Andreas Lange, andreas.lange@rhein-main.de
8
 
#               Updated for GRASS 6 by Hamish Bowman
9
 
#
10
 
# PURPOSE:      Import data from Mapgen or Matlab into a GRASS vector map
11
 
#
12
 
# COPYRIGHT:    Original version (c) Andreas Lange
13
 
#               Updates by Hamish Bowman (c) the GRASS Development Team
14
 
#
15
 
#               This program is free software under the GNU General Public
16
 
#               License (>=v2). Read the file COPYING that comes with GRASS
17
 
#               for details.
18
 
#############################################################################
19
 
#
20
 
# REQUIREMENTS:  awk
21
 
#
22
 
# DATA AVAILABILITY: e.g., NOAA's Online Coastline Extractor
23
 
#                    http://www.ngdc.noaa.gov/mgg/shorelines/shorelines.html
24
 
#
25
 
 
26
 
#%Module
27
 
#%  description: Imports Mapgen or Matlab-ASCII vector maps into GRASS.
28
 
#%  keywords: vector, import
29
 
#%End
30
 
#%flag
31
 
#%  key: f
32
 
#%  description: Input map is in Matlab format
33
 
#%end
34
 
#%flag
35
 
#%  key: z
36
 
#%  description: Create a 3D vector points map from 3 column Matlab data 
37
 
#%end
38
 
#%option
39
 
#% key: input
40
 
#% type: string
41
 
#% gisprompt: old_file,file,input
42
 
#% description: Name of input file in Mapgen/Matlab format
43
 
#% required : yes
44
 
#%end
45
 
#%option
46
 
#% key: output
47
 
#% type: string
48
 
#% gisprompt: new,vector,vector
49
 
#% description: Name for output vector map (omit for display to stdout)
50
 
#% required : no
51
 
#%end
52
 
 
53
 
if [ -z "$GISBASE" ] ; then
54
 
   echo "You must be in GRASS GIS to run this program." >&2
55
 
   exit 1
56
 
fi
57
 
 
58
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
59
 
  exec g.parser "$0" "$@"
60
 
fi
61
 
 
62
 
PROG=`basename "$0"`
63
 
 
64
 
#### check if we have awk
65
 
if [ ! -x "`which awk`" ] ; then
66
 
    g.message -e "awk required, please install awk or gawk first" 
67
 
    exit 1
68
 
fi
69
 
 
70
 
# set environment so that awk works properly in all languages
71
 
unset LC_ALL
72
 
LC_NUMERIC=C
73
 
export LC_NUMERIC
74
 
 
75
 
#### check if we have tac
76
 
# perhaps try `type -t` instead of `which`, as it is more POSIXy
77
 
if [ -x "`which tac`" ] ; then
78
 
   TAC=tac
79
 
else
80
 
   TAC=awk_tac
81
 
fi
82
 
 
83
 
awk_tac()
84
 
{
85
 
   awk '1 { last = NR; line[last] = $0; }
86
 
          END { for (i = last; i > 0; i--) { print line[i]; } }'
87
 
}
88
 
 
89
 
 
90
 
OPTS=""
91
 
 
92
 
FILE="$GIS_OPT_INPUT"
93
 
if [ ! -e "$FILE" ] ; then
94
 
   g.message -e "Input file <$FILE> not found."
95
 
   exit 1
96
 
fi
97
 
 
98
 
if [ -n "$GIS_OPT_OUTPUT" ] ; then
99
 
    NAME="$GIS_OPT_OUTPUT"
100
 
else
101
 
    unset NAME
102
 
fi
103
 
 
104
 
 
105
 
# -z implies -f
106
 
if [ $GIS_FLAG_Z -eq 1 ] && [ $GIS_FLAG_F -eq 0 ] ; then
107
 
   GIS_FLAG_F=1
108
 
fi
109
 
 
110
 
if [ $GIS_FLAG_Z -eq 1 ] ; then
111
 
  # make a 3D vector file
112
 
  do3D="-z"
113
 
else
114
 
  do3D=""
115
 
fi
116
 
 
117
 
#### setup temporary file
118
 
TMPFILE="`g.tempfile pid=$$`"
119
 
if [ $? -ne 0 ] || [ -z "$TMPFILE" ] ; then
120
 
    g.message -e "unable to create temporary files"
121
 
    exit 1
122
 
fi
123
 
 
124
 
#### trap ctrl-c so that we can clean up tmp
125
 
trap 'rm -f "$TMPFILE"' 2 3 15
126
 
 
127
 
#### create ascii vector file
128
 
if [ $GIS_FLAG_F -eq 1 ] ; then
129
 
  ## HB:  OLD v.in.mapgen.sh Matlab import command follows.
130
 
  ##    I have no idea what it's all about, so "new" matlab format will be
131
 
  ##    a series of x y with "nan nan" breaking lines. (as NOAA provides)
132
 
  ##  Old command:
133
 
  #  tac $FILE | $AWK 'BEGIN { FS="," ; R=0 }
134
 
  #    $1~/\d*/   { printf("L %d\n", R) }
135
 
  #    $1~/   .*/ { printf(" %lf %lf\n", $2, $1) ; ++R }
136
 
  #    $1~/END/   { }' | tac > "$TMPFILE"
137
 
 
138
 
  ## matlab format. Cleanse with NaN->nan and spaces to tabs,
139
 
  ##   and skip blank and comment lines
140
 
  grep -v '^#\|^$' "$FILE" | "$TAC" | awk '
141
 
    {print $1 "\t" $2 "\t" $3}
142
 
    END {if ($3~/[0-9Nna]/) print "NaN\tNaN\tNaN"
143
 
         else print "NaN\tNaN"}' | \
144
 
   tr 'N' 'n' | awk '
145
 
    BEGIN { FS="\t" ; R=0 }
146
 
    $1~/nan.*/ { printf("L %d\n", R) ; R=0 ; next }
147
 
    $1~/\d*\d*/ { printf(" %.15g %.15g %.15g\n", $1, $2, $3) ; ++R }
148
 
    END {;}' | \
149
 
   grep -v "^L 0$" | "$TAC" > "$TMPFILE"
150
 
 
151
 
else
152
 
  ## mapgen format.
153
 
  "$TAC" "$FILE" | awk '
154
 
    BEGIN { FS="\t" ; R=0 }
155
 
    $1~/#.*/ { printf("L %d\n", R) ; R=0 }
156
 
    $1~/\d*\.\d*/ { printf(" %.8f %.8f\n", $1, $2) ; ++R }
157
 
    END {;}' | "$TAC" > "$TMPFILE"
158
 
fi
159
 
 
160
 
#### create digit header
161
 
cat << EOF > "${TMPFILE}.dig"
162
 
ORGANIZATION: GRASSroots organization
163
 
DIGIT DATE:   `date +%D`
164
 
DIGIT NAME:   `echo $USER@$HOSTNAME`
165
 
MAP NAME:     $NAME
166
 
MAP DATE:     `date +%Y`
167
 
MAP SCALE:    1
168
 
OTHER INFO:   Imported with $PROG
169
 
ZONE:         0
170
 
MAP THRESH:   0
171
 
VERTI:
172
 
EOF
173
 
 
174
 
#### process points list to ascii vector file (merge in vertices)
175
 
cat "${TMPFILE}" >> "${TMPFILE}.dig"
176
 
 
177
 
#### if no name for vector file given, cat to stdout
178
 
if [ -z "$NAME" ] ; then
179
 
    g.message "Output to stdout" 
180
 
    cat "${TMPFILE}.dig" 2>/dev/null
181
 
else
182
 
    #### import to binary vector file
183
 
    g.message "Importing with v.in.ascii ..." 
184
 
    v.in.ascii $do3D input="${TMPFILE}.dig" output="$NAME" format=standard
185
 
 
186
 
    #### check success/failure
187
 
    if [ $? -eq 0 ] ; then
188
 
        g.message message="<$NAME> successfully created." 
189
 
    else
190
 
        g.message -e "An error occured on creating <$NAME>, please check"'!'
191
 
    fi
192
 
fi
193
 
 
194
 
#### clean up the mess
195
 
rm -f "$TMPFILE" "${TMPFILE}.dig"
196
 
 
197
 
#### end
198
 
exit 0