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

« back to all changes in this revision

Viewing changes to scripts/v.colors/v.colors

  • 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
 
# MODULE:       v.colors
5
 
# AUTHOR:       M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
6
 
#                 New Zealand
7
 
# PURPOSE:      Populate a GRASSRGB column with a color map and data column
8
 
#               Helper script for thematic mapping tasks
9
 
#
10
 
# COPYRIGHT:    (c) 2008 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
 
#%Module
18
 
#% description: Set color rules for features in a vector using a numeric attribute column.
19
 
#% keywords: vector, color table
20
 
#%End
21
 
#% option
22
 
#% key: map
23
 
#% type: string
24
 
#% gisprompt: old,vector,vector
25
 
#% key_desc: name
26
 
#% description: Name of input vector map 
27
 
#% required: yes
28
 
#%end
29
 
#%option
30
 
#% key: column
31
 
#% type: string
32
 
#% description: Name of column containing numeric data
33
 
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
34
 
#% required : yes
35
 
#%end
36
 
#%option
37
 
#% key: layer
38
 
#% type: integer
39
 
#% description: Layer number of data column
40
 
#% gisprompt: old_layer,layer,layer
41
 
#% answer: 1
42
 
#% required: no
43
 
#%end
44
 
#%Option
45
 
#% key: rgb_column
46
 
#% type: string
47
 
#% required: no
48
 
#% description: Name of color column to populate
49
 
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
50
 
#% answer: GRASSRGB
51
 
#% guisection: Colors
52
 
#%End
53
 
#% option
54
 
#% key: range
55
 
#% type: double
56
 
#% required: no
57
 
#% multiple: no
58
 
#% key_desc: min,max
59
 
#% description: Manually set range (min,max)
60
 
#%End
61
 
#% option
62
 
#% key: color
63
 
#% type: string
64
 
#% key_desc: style
65
 
#% options: aspect,aspectcolr,bcyr,bgyr,byg,byr,celsius,corine,curvature,differences,elevation,etopo2,evi,gdd,grey,grey1.0,grey255,grey.eq,grey.log,gyr,haxby,ndvi,population,population_dens,precipitation,precipitation_monthly,rainbow,ramp,random,rstcurv,ryb,ryg,sepia,slope,srtm,terrain,wave
66
 
#% description: Type of color table
67
 
#% required: no
68
 
#% guisection: Colors
69
 
#%end
70
 
#%Option
71
 
#% key: raster
72
 
#% type: string
73
 
#% required: no
74
 
#% description: Raster map name from which to copy color table
75
 
#% gisprompt: old,cell,raster
76
 
#% guisection: Colors
77
 
#%End
78
 
#%Option
79
 
#% key: rules
80
 
#% type: string
81
 
#% required: no
82
 
#% description: Path to rules file
83
 
#% gisprompt: old_file,file,input
84
 
#% guisection: Colors
85
 
#%End
86
 
#%Option
87
 
#% key: placeholder
88
 
#% type: string
89
 
#% required: no
90
 
#% label: Dummy raster map name containing color rules for use with d.legend
91
 
#% description: Use with the -s flag (using this option implies -s)
92
 
#% gisprompt: new,cell,raster
93
 
#% guisection: Colors
94
 
#%End
95
 
#%Flag
96
 
#% key: s
97
 
#% description: Save placeholder raster map for use with d.legend
98
 
#%End
99
 
#%Flag
100
 
#% key: n
101
 
#% description: Invert colors
102
 
#% guisection: Colors
103
 
#%End
104
 
 
105
 
 
106
 
## TODO: implement -e (equalized) and -g (logarithmic) methods in r.colors
107
 
##   'v.db.select column= | wc -l' to set region size (1xLength)
108
 
##   then create r.in.ascii 1xLength matrix with data (WITHOUT uniq)
109
 
##   and run r.colors on that raster map.
110
 
##      or
111
 
##   v.to.rast, r.colors -g, then parse colr/ file. But that's resolution dependent
112
 
 
113
 
 
114
 
if  [ -z "$GISBASE" ] ; then
115
 
    echo "You must be in GRASS GIS to run this program." 1>&2
116
 
    exit 1
117
 
fi
118
 
 
119
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
120
 
    exec g.parser "$0" "$@"
121
 
fi
122
 
 
123
 
 
124
 
cleanup()
125
 
{
126
 
   if [ -n "$TEMPFILE" ] ; then
127
 
      \rm "$TEMPFILE" "${TEMPFILE}_vcol.sql"
128
 
   fi
129
 
   eval `g.findfile elem=windows file="tmp_vcolors.$$" | grep '^name='`
130
 
   if [ -n "$name" ] ; then
131
 
      unset WIND_OVERRIDE
132
 
      g.remove region="tmp_vcolors.$$" --quiet
133
 
   fi
134
 
   eval `g.findfile elem=cell file="tmp_colr_$$" | grep '^name='`
135
 
   if [ -n "$name" ] ; then
136
 
      g.remove rast="tmp_colr_$$" --quiet
137
 
   fi
138
 
}
139
 
trap "cleanup" 2 3 15
140
 
 
141
 
 
142
 
### setup enviro vars ###
143
 
MAPSET=`g.gisenv get=MAPSET`
144
 
 
145
 
#### does map exist in CURRENT mapset?
146
 
eval `g.findfile element=vector file="$GIS_OPT_MAP" mapset="$MAPSET"`
147
 
VECT_MAPSET=`echo "$GIS_OPT_MAP" | grep '@' | cut -f2 -d'@'`
148
 
if [ -z "$VECT_MAPSET" ] ; then
149
 
   VECT_MAPSET="$MAPSET"
150
 
fi
151
 
if [ ! "$file" ] || [ "$VECT_MAPSET" != "$MAPSET" ] ; then
152
 
   g.message -e "Vector map <$GIS_OPT_MAP> not found in current mapset"
153
 
   exit 1
154
 
else
155
 
   VECTOR=`echo "$GIS_OPT_MAP" | cut -f1 -d'@'`
156
 
fi
157
 
 
158
 
 
159
 
 
160
 
# sanity check mutually exclusive color options
161
 
CTEST=0
162
 
for OPT in COLOR RASTER RULES ; do
163
 
   eval "OPTNAME=\$GIS_OPT_${OPT}"
164
 
   if [ -n "$OPTNAME" ] ; then
165
 
      CTEST=`expr $CTEST + 1`
166
 
   fi
167
 
done
168
 
if [ "$CTEST" -ne 1 ] ; then
169
 
    g.message -e "Pick one of color, rules, or raster options"
170
 
    exit 1
171
 
fi
172
 
 
173
 
if [ -n "$GIS_OPT_COLOR" ] ; then
174
 
   #### check the color rule is valid
175
 
   COLOR_OPTS=`ls "$GISBASE/etc/colors/"`
176
 
   COLOR_OPTS="`echo "$COLOR_OPTS" | tr '\n' ' '` random grey.eq grey.log rules"
177
 
 
178
 
   HAVE_COLR=0
179
 
   for COLR in $COLOR_OPTS ; do
180
 
       if [ "$COLR" = "$GIS_OPT_COLOR" ] ; then
181
 
          HAVE_COLR=1
182
 
          break
183
 
       fi
184
 
   done
185
 
   if [ "$HAVE_COLR" -eq 0 ] ; then
186
 
       g.message -e "Invalid color rule <$GIS_OPT_COLOR>"
187
 
       echo "       Valid options are: $COLOR_OPTS"
188
 
       exit 1
189
 
   fi
190
 
elif [ -n "$GIS_OPT_RASTER" ] ; then
191
 
   r.info -r "$GIS_OPT_RASTER" > /dev/null
192
 
   if [ $? -ne 0 ] ; then
193
 
       g.message -e "Unable to open raster map <$GIS_OPT_RASTER>"
194
 
       exit 1
195
 
   fi
196
 
elif [ -n "$GIS_OPT_RULES" ] ; then
197
 
   if [ ! -r "$GIS_OPT_RULES" ] ; then
198
 
       g.message -e "Unable to read color rules file <$GIS_OPT_RULES>"
199
 
       exit 1
200
 
   fi
201
 
fi
202
 
 
203
 
 
204
 
#### column checks
205
 
# check input data column 
206
 
NCOLUMN_TYPE=`v.info -c map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" --quiet | grep -i "|$GIS_OPT_COLUMN$" | cut -f1 -d'|'`
207
 
if [ -z "$NCOLUMN_TYPE" ] ; then
208
 
    g.message -e "Column <$GIS_OPT_COLUMN> not found"
209
 
    exit 1
210
 
elif [ "$NCOLUMN_TYPE" != "INTEGER" ] && [ "$NCOLUMN_TYPE" != "DOUBLE PRECISION" ] ; then
211
 
    g.message -e "Column <$GIS_OPT_COLUMN> is not numeric"
212
 
    exit 1
213
 
fi
214
 
#g.message "column <$GIS_OPT_COLUMN> is type [$NCOLUMN_TYPE]"
215
 
 
216
 
# check if GRASSRGB column exists, make it if it doesn't
217
 
TABLE=`v.db.connect -gl map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" fs="|" | cut -f2 -d'|'`
218
 
COLUMN_TYPE=`v.info -c map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" --quiet | grep -i "|$GIS_OPT_RGB_COLUMN$" | cut -f1 -d'|'`
219
 
if [ -z "$COLUMN_TYPE" ] ; then
220
 
    # RGB Column not found, create it
221
 
    g.message "Creating column <$GIS_OPT_RGB_COLUMN> ..."
222
 
    v.db.addcol map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" column="$GIS_OPT_RGB_COLUMN varchar(11)"
223
 
    if [ $? -ne 0 ] ; then
224
 
       g.message -e "Creating color column"
225
 
       exit 1
226
 
    fi
227
 
elif [ "$COLUMN_TYPE" != "CHARACTER" -a "$COLUMN_TYPE" != "TEXT" ] ; then
228
 
    g.message -e "Column <$GIS_OPT_RGB_COLUMN> is not of compatible type (found $COLUMN_TYPE)"
229
 
    exit 1
230
 
else
231
 
    NUM_CHARS=`db.describe -c "$TABLE" | grep -i " $GIS_OPT_RGB_COLUMN:" | cut -f4 -d':'`
232
 
    if [ "$NUM_CHARS" -lt 11 ] ; then
233
 
       g.message -e "Color column <$GIS_OPT_RGB_COLUMN> is not wide enough (needs 11 characters)"
234
 
       exit 1
235
 
    fi
236
 
fi
237
 
 
238
 
 
239
 
# find data range
240
 
if [ -n "$GIS_OPT_RANGE" ] ; then
241
 
   #order doesn't matter
242
 
   MINVAL=`echo "$GIS_OPT_RANGE" | grep '[[:digit:]]' | grep ',' | cut -f1 -d','`
243
 
   MAXVAL=`echo "$GIS_OPT_RANGE" | grep '[[:digit:]]' | grep ',' | cut -f2 -d','`
244
 
else
245
 
   g.message "Scanning values ..."
246
 
   MINVAL=`v.db.select map="$GIS_OPT_MAP" column="$GIS_OPT_COLUMN" layer="$GIS_OPT_LAYER" | sort -n | grep '^[-0-9]' | head -n 1`
247
 
   MAXVAL=`v.db.select map="$GIS_OPT_MAP" column="$GIS_OPT_COLUMN" layer="$GIS_OPT_LAYER" | sort -n | grep '^[-0-9]' | tail -n 1`
248
 
fi
249
 
g.message -v message=" min=[$MINVAL]  max=[$MAXVAL]"
250
 
if [ -z "$MINVAL" ] || [ -z "$MAXVAL" ] ; then
251
 
   g.message -e "Scanning data range"
252
 
   exit 1
253
 
fi
254
 
 
255
 
 
256
 
# setup internal region
257
 
g.region save="tmp_vcolors.$$"
258
 
WIND_OVERRIDE="tmp_vcolors.$$"
259
 
export WIND_OVERRIDE
260
 
g.region rows=2 cols=2
261
 
 
262
 
# create dummy raster map
263
 
if [ "$NCOLUMN_TYPE" = "INTEGER" ] ; then
264
 
   r.mapcalc "tmp_colr_$$ = if( row() == 1, $MINVAL, $MAXVAL)"
265
 
else
266
 
   r.mapcalc "tmp_colr_$$ = double( if( row() == 1, $MINVAL, $MAXVAL) )"
267
 
fi
268
 
 
269
 
 
270
 
if [ -n "$GIS_OPT_COLOR" ] ; then
271
 
   COLOR_CMD="color=$GIS_OPT_COLOR"
272
 
elif [ -n "$GIS_OPT_RASTER" ] ; then
273
 
   COLOR_CMD="raster=$GIS_OPT_RASTER"
274
 
elif [ -n "$GIS_OPT_RULES" ] ; then
275
 
   COLOR_CMD="rules=$GIS_OPT_RULES"
276
 
fi
277
 
if [ $GIS_FLAG_N -eq 1 ] ; then
278
 
   FLIP_FLAG="-n"
279
 
else
280
 
   FLIP_FLAG=""
281
 
fi
282
 
r.colors map="tmp_colr_$$" "$COLOR_CMD" $FLIP_FLAG --quiet
283
 
 
284
 
 
285
 
 
286
 
TEMPFILE="`g.tempfile pid=$$`"
287
 
if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
288
 
    g.message -e "Unable to create temporary file"
289
 
    cleanup
290
 
    exit 1
291
 
fi
292
 
 
293
 
 
294
 
# calculate colors and write SQL command file
295
 
g.message "Looking up colors ..."
296
 
v.db.select map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" column="$GIS_OPT_COLUMN" | \
297
 
  sort -n | grep '^[-0-9]' | uniq | \
298
 
  r.what.color -i in="tmp_colr_$$" | sed -e 's/: /|/' | grep -v '|\*$' | \
299
 
  ( while read LINE ; do
300
 
      #g.message message="LINE=[$LINE]"
301
 
      VALUE=`echo $LINE | cut -f1 -d'|'`
302
 
      COLR=`echo $LINE | cut -f2 -d'|'`
303
 
      echo "UPDATE $TABLE SET $GIS_OPT_RGB_COLUMN = '$COLR' WHERE $GIS_OPT_COLUMN = $VALUE;" >> "${TEMPFILE}_vcol.sql"
304
 
  done )
305
 
 
306
 
 
307
 
if [ ! -e "${TEMPFILE}_vcol.sql" ] ; then
308
 
    g.message -e "No values found in color range"
309
 
    cleanup
310
 
    exit 1
311
 
fi
312
 
 
313
 
# apply SQL commands to update the table with values
314
 
NUM_RULES=`wc -l < "${TEMPFILE}_vcol.sql"`
315
 
g.message "Writing $NUM_RULES colors ..."
316
 
#less "$TEMPFILE"
317
 
db.execute input="${TEMPFILE}_vcol.sql"
318
 
if [ $? -ne 0 ] ; then
319
 
   g.message -e "Processing SQL transaction"
320
 
   cleanup
321
 
   exit 1
322
 
fi
323
 
 
324
 
 
325
 
if [ -n "$GIS_OPT_PLACEHOLDER" ] || [ $GIS_FLAG_S -eq 1 ] ; then
326
 
 
327
 
   if [ -n "$GIS_OPT_PLACEHOLDER" ] ; then
328
 
      DUMMY_NAME="$GIS_OPT_PLACEHOLDER"
329
 
   else
330
 
      DUMMY_NAME="vcolors_$$"
331
 
   fi
332
 
 
333
 
   g.rename rast="tmp_colr_$$","$DUMMY_NAME" --quiet
334
 
   g.message "Raster map containing color rules saved to <$DUMMY_NAME>"
335
 
 
336
 
   # TODO save full v.colors command line history
337
 
   r.support map="$DUMMY_NAME" history="" \
338
 
        source1="vector map=$GIS_OPT_MAP" \
339
 
        source2="column=$GIS_OPT_COLUMN" \
340
 
        title="Dummy raster to use as thematic vector legend" \
341
 
        description="generated by v.colors using r.mapcalc"
342
 
   r.support map="$DUMMY_NAME" \
343
 
        history="RGB saved into <$GIS_OPT_RGB_COLUMN> using <$GIS_OPT_COLOR$GIS_OPT_RASTER$GIS_OPT_RULES>"
344
 
else
345
 
   g.remove rast="tmp_colr_$$" --quiet
346
 
fi
347
 
cleanup
348
 
 
349
 
exit 0
350
 
#v.db.dropcol map=vcol_test col=GRASSRGB
351
 
#d.vect -a vcol_test icon=basic/circle color=none size=8
352