-
Notifications
You must be signed in to change notification settings - Fork 1
/
d.hyperlink
326 lines (268 loc) · 7.6 KB
/
d.hyperlink
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /bin/sh
#
############################################################################
#
# MODULE: d.hyperlink for Grass 6.0/6.1
#
# AUTHOR(S): Eric Patton
#
# PURPOSE: To allow display of hyperlinked documents from a vector
# attribute table.
#
# COPYRIGHT: (C) 2006 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
# Last Modified: March 19, 2006
#
#############################################################################
#%Module
#% description: Allows the interactive display of hyperlinked documents from a vector attribute table.
#%END
#%flag
#% key: c
#% description: list columns in attached database
#%END
#%flag
#% key: d
#% description: list directories in Location's MAPSET
#%END
#%flag
#% key: i
#% description: enable interactive mode (uses d.what.vect)
#%END
#%flag
#% key: p
#% description: print command-line parameters given (for testing)
#%END
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% description: vector map to load hyperlink from
#% required : no
#%END
#%option
#% key: linkdir
#% type: string
#% description: directory containing hyperlinks
#% answer: hyperlinks
#% required: no
#%END
#%option
#% key: layer
#% type: integer
#% description: layer of the linked table
#% answer: 1
#% required : no
#%END
#%option
#% key: column
#% type: string
#% description: attribute table column containing document to be hyperlinked
#% answer: hyperlink
#% required : no
#%END
#%option
#% key: where
#% type: string
#% description: WHERE conditions of SQL statement without 'where' keyword. (Ex: cat=26 or Station_ID=12)
#% required: no
#%END
#%option
#% key: viewer
#% type: string
#% description: image viewer to open hyperlinks
#% options: eog,gimp,gpdf,gthumb,inkscape
#% answer: gthumb
#% required: no
#%END
SCRIPT=`basename $0`
if [ -z "$GISBASE" ] ; then
echo "You must be in GRASS GIS to run this program."
exit 1
fi
if [ "$1" != "@ARGS_PARSED@" ] ; then
exec g.parser "$0" "$@"
fi
# Capture CTRL-C and similar breaks.
trap 'echo -e "\n\nUser break! Exiting.\n" ; exit 1' 2 3 15
# Check if we have awk.
if [ ! -x "`which awk`" ] ; then
echo "$SCRIPT: awk required, please install awk or gawk first" 2>&1
exit 1
fi
# Setting environment, so that awk works properly in all languages.
unset LC_ALL
export LC_NUMERIC=C
# Capture Location, Mapset, etc. to feed directory path for image viewer.
eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
LOCATION=$GISDBASE/$LOCATION_NAME/$MAPSET
FOLDER="$LOCATION/$GIS_OPT_linkdir"
INPUT="$GIS_OPT_map"
COLUMN="$GIS_OPT_column"
LAYER="$GIS_OPT_layer"
VIEWER="$GIS_OPT_viewer"
# Get current database connection parameters from db.connect:
DRIVER=`db.connect -p | grep 'driver' | awk -F: '{print $2}'`
DATABASE=`db.connect -p | grep 'database' | awk -F: '{print $2}'`
TABLE="`v.db.connect -g "$INPUT" | grep $LAYER | cut -f2 -d' '`"
# Provide driectory listing in MAPSET for -d flag.
if [ "$GIS_FLAG_d" -eq 1 ] ; then
echo ""
echo "======================================="
echo "Directory listing for MAPSET $MAPSET:"
echo "======================================="
echo ""
ls -l $LOCATION | grep '^d' | awk '{print $8}'
exit 0
fi
# Fetch the input vector map.
if [ -n "$INPUT" ] ; then
eval `g.findfile element=vector mapset=$MAPSET file=$INPUT`
if [ ! "$file" ] ; then
echo ""
echo "$SCRIPT: Input map not found\n" 2>&1
exit 1
fi
else
echo "$SCRIPT: Please enter an input vector.\n" 2>&1
exit 1
fi
# Provide list of columns in connected table for -c flag.
if [ "$GIS_FLAG_c" -eq 1 ] ; then
db.columns table=$TABLE driver=$DRIVER database=$DATABASE
exit 0
fi
# Provide command-line parameters for -p flag. For testing only...
if [ "$GIS_FLAG_p" -eq 1 ] ; then
echo ""
echo "================================================================="
echo "Input is: $INPUT"
echo "Attribute table is: $TABLE"
echo "Hyperlink folder is: $FOLDER"
echo "Link Column is: $COLUMN"
echo "Layer is: $LAYER"
echo "================================================================="
echo ""
exit 0
fi
# Check if the hyperlink folder is a valid directory.
if [ ! -d "$FOLDER" ] ; then
echo ""
echo -e "$SCRIPT: Specified link folder is not a directory. Exiting." 2>&1
exit 1
fi
# Check and see if an attribute table is connected on given layer
# FIXME: This test fails if map is in another mapset!!! (eg map@othermapset).
if [ -z "$TABLE" ] ; then
echo "\n$SCRIPT: No table connection for layer $LAYER on vector $INPUT."
echo "Use v.db.connect first."
exit 1
fi
# Check whether column parameter exists and is present in attribute table - there must be a better way of doing this.
COLTEST=`db.columns table=$TABLE driver=$DRIVER database=$DATABASE | grep "$COLUMN"`
if [ $? -ne 0 -o -z "$COLTEST" ] ; then
echo ""
echo "$SCRIPT: Column '$COLUMN' not found in table $TABLE. Exiting."
echo ""
exit 1
fi
# Check if we have selected image viewer - insert support for other viewers here as needed
case "$VIEWER" in
eog) if [ ! -x "`which eog`" ] ; then
echo ""
echo "$SCRIPT: Eye of Gnome viewer required, please install first or select another viewer." 2>&1
echo ""
exit 1
fi
;;
gimp) if [ ! -x "`which gimp`" ] ; then
echo ""
echo "$SCRIPT: Gimp required, please install first or select another viewer." 2>&1
echo ""
exit 1
fi
;;
gpdf) if [ ! -x "`which gpdf`" ] ; then
echo ""
echo "$SCRIPT: gpdf required, please install first or select another viewer." 2>&1
echo ""
exit 1
fi
;;
gthumb) if [ ! -x "`which gthumb`" ] ; then
echo ""
echo "$SCRIPT: gthumb required, please install first or select another viewer." 2>&1
echo ""
exit 1
fi
;;
inkscape) if [ ! -x "`which inkscape`" ] ; then
echo ""
echo "$SCRIPT: inkscape required, please install first or select another viewer." 2>&1
echo ""
exit 1
fi
;;
*) echo ""
echo "$SCRIPT: $VIEWER is not one of the supported viewers - please select another." 2>&1
echo ""
exit 1
;;
esac
# Now query vector map and load hyperlink into viewer.
if [ "$GIS_FLAG_i" -eq 1 ] ; then
STATUS=1
while [ "$STATUS" -eq 1 ] ; do
echo ""
echo "Click on a vector point to load hyperlink..."
LINK_DOC=`d.what.vect -1t map=$INPUT | grep $COLUMN | awk -F": " '{print $2}'`
if [ -z "$LINK_DOC" ] ; then
echo ""
echo "Null statement received for vector hyperlink. Exiting."
echo ""
exit 1
else
echo ""
echo "Received hyperlink document named '$LINK_DOC'..."
echo "The image pathname will be $FOLDER/$LINK_DOC..."
echo "Loading image into $VIEWER..."
fi
# Load hyperlink in selected image viewer.
$VIEWER $FOLDER/$LINK_DOC
echo ""
echo "Continue? Type 'q' to quit, or any other key plus RETURN to continue."
read CONFIRM
if [ "$CONFIRM" = "q" -o "$CONFIRM" = "Q" ] ; then
echo ""
echo "Exiting - Goodbye!"
STATUS=0
fi
done
else #Use sql query to select vector
QUERY="$GIS_OPT_where"
if [ -z "$QUERY" ] ; then
echo ""
echo "Please enter an SQL 'where' query (example: cat=36)"
echo ""
exit 1
fi
LINK_DOC=`echo "select $COLUMN from $TABLE where $QUERY" | db.select -c`
if [ -z "$LINK_DOC" ] ; then
echo ""
echo "$SCRIPT: No hyperlink match found in $TABLE for '$QUERY'. Exiting."
echo ""
exit 1
fi
echo ""
echo "Received hyperlink document named '$LINK_DOC'..."
echo "The image pathname will be $FOLDER/$LINK_DOC..."
echo "Loading image into $VIEWER..."
# Load hyperlink in selected image viewer.
$VIEWER $FOLDER/$LINK_DOC
fi
exit 0