3
############################################################################
7
# Jianping Xu and Scott Madry, Rutgers University. October 19, 1993
8
# Markus Neteler 8/2002: added simple d.legend logic
9
# Markus Neteler 10/2003: added g.parser
10
# Michael Barton 12/2004: remove reference to (null)
11
# Glynn Clements 10/2008: converted to Python
13
# PURPOSE: Displays a raster map and its legend on a graphics window.
15
# Description: d.rast.leg clears the entire screen, divides it into a main
16
# (left) and a minor (right) frames, and then display a raster
17
# map in the main frame and the map legend in the minor frame.
18
# The user can run the program interactively or
21
# See also: d.rast, d.legend.
23
# COPYRIGHT: (C) 1993-2014 by the GRASS Development Team
25
# This program is free software under the GNU General Public
26
# License (>=v2). Read the file COPYING that comes with GRASS
29
#############################################################################
32
#% description: Displays a raster map and its legend on a graphics window
34
#% keyword: cartography
39
#% description: Flip legend
43
#% description: Omit entries with missing label
46
#% description: Name of raster map to display
50
#% description: Draw smooth gradient
55
#% description: Number of lines to appear in the legend
58
#%option G_OPT_R_INPUT
60
#% description: Name of input raster map to generate legend from
66
import grass.script as grass
68
def make_frame(f, b, t, l, r):
76
rt = fb + t * (ft - fb)
77
rb = fb + b * (ft - fb)
78
rl = fl + l * (fr - fl)
79
rr = fl + r * (fr - fl)
80
s = '%f,%f,%f,%f' % (rt, rb, rl, rr)
81
os.environ['GRASS_RENDER_FRAME'] = s
85
nlines = options['lines']
86
rast = options['raster']
91
#for -n flag of d.legend
92
if not grass.find_file(map)['file']:
93
grass.fatal(_("Raster map <%s> not found") % map)
96
if rast and not grass.find_file(rast)['file']:
97
grass.fatal(_("Raster map <%s> not found") % rast)
99
s = grass.read_command('d.info', flags = 'f')
103
f = tuple([float(x) for x in s.split()[1:5]])
105
grass.run_command('d.erase')
106
os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
110
# set vertical divide at 65 instead of 80 if real labels in cats/ file??
111
make_frame(f, 90, 100, 70, 100)
112
# use map name without mapset suffix
113
mapname = map.split('@')[0]
114
grass.run_command('d.text', color='black', size=5, at='5,97', align='cl', text=mapname)
118
# set legend vertical position and size based on number of categories
119
cats = grass.read_command('r.describe', map=map, flags = '1n')
120
ncats = len(cats.strip().split('\n'))
122
# Only need to adjust legend size if number of categories is between 1 and 10
123
if ncats < 2: ncats = 2
124
if ncats > 10: ncats = 10
126
VSpacing = (100 - (ncats * 10) + 10)
136
kv = grass.raster_info(map = lmap)
137
if kv['datatype'] is 'CELL':
140
leg_at = '%f,95,5,10' %VSpacing
142
# checking for histogram causes more problems than it solves
143
# histfiledir = grass.find_file(lmap, 'cell_misc')['file']
144
# has_hist = os.path.isfile(os.path.join(histfiledir, 'histogram'))
154
# if has_hist or omit:
157
make_frame(f, 0, 90, 70, 100)
158
grass.run_command('d.legend', flags = lflags, raster = lmap, lines = nlines, at = leg_at)
161
make_frame(f, 0, 100, 0, 70)
162
grass.run_command('d.rast', map = map)
165
if __name__ == "__main__":
166
options, flags = grass.parser()