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

« back to all changes in this revision

Viewing changes to scripts/d.rast.leg/d.rast.leg.py

  • 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
#!/usr/bin/env python
 
2
 
 
3
############################################################################
 
4
#
 
5
# MODULE:       d.rast.leg
 
6
# AUTHOR(S):
 
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
 
12
#
 
13
# PURPOSE:      Displays a raster map and its legend on a graphics window.
 
14
#
 
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 
 
19
#               non-interactively.
 
20
#
 
21
# See also:     d.rast, d.legend.
 
22
#
 
23
# COPYRIGHT:    (C) 1993-2014 by the GRASS Development Team
 
24
#
 
25
#               This program is free software under the GNU General Public
 
26
#               License (>=v2). Read the file COPYING that comes with GRASS
 
27
#               for details.
 
28
#
 
29
#############################################################################
 
30
 
 
31
#%module
 
32
#% description: Displays a raster map and its legend on a graphics window
 
33
#% keyword: display
 
34
#% keyword: cartography
 
35
#% keyword: legend
 
36
#%end
 
37
#%flag
 
38
#%  key: f
 
39
#%  description: Flip legend
 
40
#%end
 
41
#%flag
 
42
#%  key: n
 
43
#%  description: Omit entries with missing label
 
44
#%end
 
45
#%option G_OPT_R_MAP
 
46
#% description: Name of raster map to display
 
47
#%end
 
48
#%flag
 
49
#%  key: s
 
50
#%  description: Draw smooth gradient
 
51
#%end
 
52
#%option
 
53
#% key: lines
 
54
#% type: integer
 
55
#% description: Number of lines to appear in the legend
 
56
#% required: no
 
57
#%end
 
58
#%option G_OPT_R_INPUT
 
59
#% key: raster
 
60
#% description: Name of input raster map to generate legend from
 
61
#% required: no
 
62
#%end
 
63
 
 
64
import sys
 
65
import os
 
66
import grass.script as grass
 
67
 
 
68
def make_frame(f, b, t, l, r):
 
69
    (fl, fr, ft, fb) = f
 
70
 
 
71
    t /= 100.0
 
72
    b /= 100.0
 
73
    l /= 100.0
 
74
    r /= 100.0
 
75
 
 
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
 
82
 
 
83
def main():
 
84
    map = options['map']
 
85
    nlines = options['lines']
 
86
    rast = options['raster']
 
87
    omit = flags['n']
 
88
    flip = flags['f']
 
89
    smooth = flags['s']
 
90
 
 
91
    #for -n flag of d.legend
 
92
    if not grass.find_file(map)['file']:
 
93
        grass.fatal(_("Raster map <%s> not found") % map)
 
94
 
 
95
    # for rast=
 
96
    if rast and not grass.find_file(rast)['file']:
 
97
        grass.fatal(_("Raster map <%s> not found") % rast)
 
98
 
 
99
    s = grass.read_command('d.info', flags = 'f')
 
100
    if not s:
 
101
        sys.exit(1)
 
102
    
 
103
    f = tuple([float(x) for x in s.split()[1:5]])
 
104
    
 
105
    grass.run_command('d.erase')
 
106
    os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
 
107
 
 
108
    #draw title
 
109
    
 
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)
 
115
 
 
116
    #draw legend
 
117
    
 
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'))
 
121
    
 
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
 
125
    
 
126
    VSpacing = (100 - (ncats * 10) + 10)
 
127
    
 
128
    if not nlines:
 
129
        nlines = None
 
130
 
 
131
    if rast:
 
132
        lmap = rast
 
133
    else:
 
134
        lmap = map
 
135
 
 
136
    kv = grass.raster_info(map = lmap)
 
137
    if kv['datatype'] is 'CELL':
 
138
        leg_at = None
 
139
    else:
 
140
        leg_at = '%f,95,5,10' %VSpacing        
 
141
 
 
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'))
 
145
 
 
146
    lflags = ''
 
147
    if flip:
 
148
        lflags += 'f'
 
149
    if omit:
 
150
        lflags += 'n'
 
151
    if smooth:
 
152
        lflags += 's'
 
153
 
 
154
#    if has_hist or omit:
 
155
#        lflags += 'n'
 
156
 
 
157
    make_frame(f, 0, 90, 70, 100)
 
158
    grass.run_command('d.legend', flags = lflags, raster = lmap, lines = nlines, at = leg_at)
 
159
 
 
160
    #draw map
 
161
    make_frame(f, 0, 100, 0, 70)
 
162
    grass.run_command('d.rast', map = map)
 
163
 
 
164
 
 
165
if __name__ == "__main__":
 
166
    options, flags = grass.parser()
 
167
    main()
 
168