[GRASS-SVN] r46779 - grass/trunk/scripts/v.colors
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Jun 25 17:59:42 EDT 2011
Author: martinl
Date: 2011-06-25 14:59:42 -0700 (Sat, 25 Jun 2011)
New Revision: 46779
Modified:
grass/trunk/scripts/v.colors/v.colors.py
Log:
v.colors: minor clean up
Modified: grass/trunk/scripts/v.colors/v.colors.py
===================================================================
--- grass/trunk/scripts/v.colors/v.colors.py 2011-06-25 21:32:43 UTC (rev 46778)
+++ grass/trunk/scripts/v.colors/v.colors.py 2011-06-25 21:59:42 UTC (rev 46779)
@@ -8,18 +8,18 @@
# PURPOSE: Populate a GRASSRGB column with a color map and data column
# Helper script for thematic mapping tasks
#
-# COPYRIGHT: (c) 2008, 2010 Hamish Bowman, and the GRASS Development Team
+# COPYRIGHT: (c) 2008, 2010-2011 Hamish Bowman, and 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.
#
#############################################################################
-#%Module
+#%module
#% description: Sets color rules for features in a vector map using a numeric attribute column.
#% keywords: vector
#% keywords: color table
-#%End
+#%end
#% option
#% key: map
#% type: string
@@ -43,7 +43,7 @@
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
#% required : yes
#%end
-#%Option
+#%option
#% key: rgb_column
#% type: string
#% required: no
@@ -51,16 +51,16 @@
#% gisprompt: new_dbcolumn,dbcolumn,dbcolumn
#% answer: GRASSRGB
#% guisection: Colors
-#%End
-#% option
+#%end
+#%option
#% key: range
#% type: double
#% required: no
#% multiple: no
#% key_desc: min,max
#% description: Manually set range (min,max)
-#%End
-#% option
+#%end
+#%option
#% key: color
#% type: string
#% key_desc: string
@@ -69,7 +69,7 @@
#% required: no
#% guisection: Colors
#%end
-#%Option
+#%option
#% key: raster
#% type: string
#% key_desc: name
@@ -77,26 +77,25 @@
#% description: Name of raster map from which to copy color table
#% gisprompt: old,cell,raster
#% guisection: Colors
-#%End
-#%Option
+#%end
+#%option
#% key: rules
#% type: string
#% required: no
#% description: Name of file containing rules
#% gisprompt: old_file,file,input
#% guisection: Colors
-#%End
-#%Flag
+#%end
+#%flag
#% key: s
#% description: Save placeholder raster map for use with d.legend
-#%End
-#%Flag
+#%end
+#%flag
#% key: n
#% description: Invert colors
#% guisection: Colors
-#%End
+#%end
-
## TODO: implement -e (equalized) and -g (logarithmic) methods in r.colors
## 'v.db.select column= | wc -l' to set region size (1xLength)
## then create r.in.ascii 1xLength matrix with data (WITHOUT uniq)
@@ -104,7 +103,6 @@
## or
## v.to.rast, r.colors -g, then parse colr/ file. But that's resolution dependent
-
import sys
import os
import atexit
@@ -120,62 +118,49 @@
grass.run_command('g.remove', rast = tmp_colr, quiet = True)
def main():
- color = options['color']
+ color = options['color']
column = options['column']
- layer = options['layer']
- map = options['map']
- range = options['range']
+ layer = options['layer']
+ map = options['map']
+ range = options['range']
raster = options['raster']
rgb_column = options['rgb_column']
- rules = options['rules']
- flip = flags['n']
-
+ rules = options['rules']
+ flip = flags['n']
+
global tmp, tmp_colr, tmp_vcol
pid = os.getpid()
- tmp = None
- tmp_colr = None
- tmp_vcol = None
-
- kv = grass.gisenv()
- mapset = kv['MAPSET']
+ tmp = tmp_colr = tmp_vcol = None
+
+ mapset = grass.gisenv()['MAPSET']
gisbase = os.getenv('GISBASE')
-
- #### does map exist in CURRENT mapset?
+
+ # does map exist in CURRENT mapset?
kv = grass.find_file(map, element = 'vector', mapset = mapset)
- map_split = map.split('@')
- vect_mapset = map_split[1:]
- if vect_mapset == []:
- vect_mapset = mapset
- else:
- vect_mapset = vect_mapset[0]
- if not kv['file'] or vect_mapset != mapset:
+ if not kv['file']:
grass.fatal(_("Vector map <%s> not found in current mapset") % map)
-
- vector = map_split[0]
-
+
+ vector = map.split('@', 1)
+
# sanity check mutually exclusive color options
- ctest = 0
- for opt in ['color', 'raster', 'rules']:
- if options[opt]:
- ctest += 1
- if ctest != 1:
+ if not options['color'] and not options['raster'] and not options['rules']:
grass.fatal(_("Pick one of color, rules, or raster options"))
-
+
if color:
#### check the color rule is valid
- color_opts = os.listdir(os.path.join(gisbase,'etc','colors'))
+ color_opts = os.listdir(os.path.join(gisbase, 'etc', 'colors'))
color_opts += ['random', 'grey.eq', 'grey.log', 'rules']
if color not in color_opts:
- grass.fatal(("Invalid color rule <%s>\n" % color) +
- ("Valid options are: %s" % ' '.join(color_opts)))
+ grass.fatal(_("Invalid color rule <%s>\n") % color +
+ _("Valid options are: %s") % ' '.join(color_opts))
elif raster:
if not grass.find_file(raster)['name']:
- grass.fatal(_("Unable to open raster map <%s>") % raster)
+ grass.fatal(_("Raster raster map <%s> not found") % raster)
elif rules:
if not os.access(rules, os.R_OK):
grass.fatal(_("Unable to read color rules file <%s>") % rules)
-
- #### column checks
+
+ # column checks
# check input data column
cols = grass.vector_columns(map, layer = layer)
if column not in cols:
@@ -183,9 +168,7 @@
ncolumn_type = cols[column]['type']
if ncolumn_type not in ["INTEGER", "DOUBLE PRECISION"]:
grass.fatal(_("Column <%s> is not numeric but %s") % (column, ncolumn_type))
-
- #g.message "column <$GIS_OPT_COLUMN> is type [$NCOLUMN_TYPE]"
-
+
# check if GRASSRGB column exists, make it if it doesn't
table = grass.vector_db(map)[int(layer)]['table']
if rgb_column not in cols:
@@ -201,57 +184,57 @@
num_chars = dict([(v[0], int(v[2])) for v in grass.db_describe(table)['cols']])[rgb_column]
if num_chars < 11:
grass.fatal(_("Color column <%s> is not wide enough (needs 11 characters)"), rgb_column)
-
+
cvals = grass.vector_db_select(map, layer = int(layer), columns = column)['values'].values()
-
+
# find data range
if range:
- #order doesn't matter
+ # order doesn't matter
vals = range.split(',')
else:
- grass.message(_("Scanning values ..."))
+ grass.message(_("Scanning values..."))
vals = [float(x[0]) for x in cvals]
-
+
minval = min(vals)
maxval = max(vals)
- grass.verbose(_(" min=[%s] max=[$%s]") % (minval, maxval))
+ grass.verbose(_("Range: [%s, %s]") % (minval, maxval))
if not minval or not maxval:
grass.fatal(_("Scanning data range"))
# setup internal region
grass.use_temp_region()
grass.run_command('g.region', rows = 2, cols = 2)
-
+
tmp_colr = "tmp_colr_%d" % pid
-
+
# create dummy raster map
if ncolumn_type == "INTEGER":
- grass.mapcalc("$tmp_colr = if(row() == 1, $minval, $maxval)",
+ grass.mapcalc("$tmp_colr = int(if(row() == 1, $minval, $maxval))",
tmp_colr = tmp_colr, minval = minval, maxval = maxval)
else:
grass.mapcalc("$tmp_colr = double(if(row() == 1, $minval, $maxval))",
tmp_colr = tmp_colr, minval = minval, maxval = maxval)
-
+
if color:
color_cmd = {'color': color}
elif raster:
color_cmd = {'raster': raster}
elif rules:
color_cmd = {'rules': rules}
-
+
if flip:
flip_flag = 'n'
else:
flip_flag = ''
-
+
grass.run_command('r.colors', map = tmp_colr, flags = flip_flag, quiet = True, **color_cmd)
-
+
tmp = grass.tempfile()
-
+
# calculate colors and write SQL command file
grass.message(_("Looking up colors ..."))
-
+
f = open(tmp, 'w')
p = grass.feed_command('r.what.color', flags = 'i', input = tmp_colr, stdout = f)
lastval = None
@@ -262,7 +245,7 @@
p.stdin.close()
p.wait()
f.close()
-
+
tmp_vcol = "%s_vcol.sql" % tmp
fi = open(tmp, 'r')
fo = open(tmp_vcol, 'w')
@@ -273,7 +256,6 @@
colr = colr.strip()
if len(colr.split(':')) != 3:
continue
- #grass.debug('LINE=[%s]' % line)
fo.write(t.substitute(table = table, rgb_column = rgb_column, colr = colr, column = column, value = value))
found += 1
fi.close()
@@ -281,13 +263,13 @@
if not found:
grass.fatal(_("No values found in color range"))
-
+
# apply SQL commands to update the table with values
- grass.message(_("Writing %s colors ...") % found)
- # less "$TMP"
+ grass.message(_("Writing %s colors...") % found)
+
if 0 != grass.run_command('db.execute', input = tmp_vcol):
grass.fatal(_("Processing SQL transaction"))
-
+
if flags['s']:
vcolors = "vcolors_%d" % pid
grass.run_command('g.rename', rast = (tmp_colr, vcolors), quiet = True)
@@ -297,14 +279,11 @@
history = "",
source1 = "vector map = %s" % map,
source2 = "column = %s" % column,
- title = "Dummy raster to use as thematic vector legend",
+ title = _("Dummy raster to use as thematic vector legend"),
description = "generated by v.colors using r.mapcalc")
grass.run_command('r.support', map = vcolors,
- history = "RGB saved into <%s> using <%s%s%s>" % (rgb_column, color, raster, rules))
-
- #v.db.dropcolumn map=vcol_test col=GRASSRGB
- #d.vect -a vcol_test icon=basic/circle color=none size=8
-
+ history = _("RGB saved into <%s> using <%s%s%s>") % (rgb_column, color, raster, rules))
+
if __name__ == "__main__":
options, flags = grass.parser()
atexit.register(cleanup)
More information about the grass-commit
mailing list