3
############################################################################
7
# AUTHOR(S): Andreas Lange, andreas.lange@rhein-main.de
8
# Updated for GRASS 6 by Hamish Bowman
10
# PURPOSE: Import data from Mapgen or Matlab into a GRASS vector map
12
# COPYRIGHT: Original version (c) Andreas Lange
13
# Updates by Hamish Bowman (c) the GRASS Development Team
15
# This program is free software under the GNU General Public
16
# License (>=v2). Read the file COPYING that comes with GRASS
18
#############################################################################
22
# DATA AVAILABILITY: e.g., NOAA's Online Coastline Extractor
23
# http://www.ngdc.noaa.gov/mgg/shorelines/shorelines.html
27
#% description: Imports Mapgen or Matlab-ASCII vector maps into GRASS.
28
#% keywords: vector, import
32
#% description: Input map is in Matlab format
36
#% description: Create a 3D vector points map from 3 column Matlab data
41
#% gisprompt: old_file,file,input
42
#% description: Name of input file in Mapgen/Matlab format
48
#% gisprompt: new,vector,vector
49
#% description: Name for output vector map (omit for display to stdout)
53
if [ -z "$GISBASE" ] ; then
54
echo "You must be in GRASS GIS to run this program." >&2
58
if [ "$1" != "@ARGS_PARSED@" ] ; then
59
exec g.parser "$0" "$@"
64
#### check if we have awk
65
if [ ! -x "`which awk`" ] ; then
66
g.message -e "awk required, please install awk or gawk first"
70
# set environment so that awk works properly in all languages
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
85
awk '1 { last = NR; line[last] = $0; }
86
END { for (i = last; i > 0; i--) { print line[i]; } }'
93
if [ ! -e "$FILE" ] ; then
94
g.message -e "Input file <$FILE> not found."
98
if [ -n "$GIS_OPT_OUTPUT" ] ; then
99
NAME="$GIS_OPT_OUTPUT"
106
if [ $GIS_FLAG_Z -eq 1 ] && [ $GIS_FLAG_F -eq 0 ] ; then
110
if [ $GIS_FLAG_Z -eq 1 ] ; then
111
# make a 3D vector file
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"
124
#### trap ctrl-c so that we can clean up tmp
125
trap 'rm -f "$TMPFILE"' 2 3 15
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)
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"
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"}' | \
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 }
149
grep -v "^L 0$" | "$TAC" > "$TMPFILE"
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"
160
#### create digit header
161
cat << EOF > "${TMPFILE}.dig"
162
ORGANIZATION: GRASSroots organization
163
DIGIT DATE: `date +%D`
164
DIGIT NAME: `echo $USER@$HOSTNAME`
168
OTHER INFO: Imported with $PROG
174
#### process points list to ascii vector file (merge in vertices)
175
cat "${TMPFILE}" >> "${TMPFILE}.dig"
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
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
186
#### check success/failure
187
if [ $? -eq 0 ] ; then
188
g.message message="<$NAME> successfully created."
190
g.message -e "An error occured on creating <$NAME>, please check"'!'
194
#### clean up the mess
195
rm -f "$TMPFILE" "${TMPFILE}.dig"