d.rast.leg.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: d.rast.leg
  5. # AUTHOR(S):
  6. # Jianping Xu and Scott Madry, Rutgers University. October 19, 1993
  7. # Markus Neteler 8/2002: added simple d.legend logic
  8. # Markus Neteler 10/2003: added g.parser
  9. # Michael Barton 12/2004: remove reference to (null)
  10. # Glynn Clements 10/2008: converted to Python
  11. #
  12. # PURPOSE: Displays a raster map and its legend on a graphics window.
  13. #
  14. # Description: d.rast.leg clears the entire screen, divides it into a main
  15. # (left) and a minor (right) frames, and then display a raster
  16. # map in the main frame and the map legend in the minor frame.
  17. # The user can run the program interactively or
  18. # non-interactively.
  19. #
  20. # See also: d.rast, d.legend.
  21. #
  22. # COPYRIGHT: (C) 1993-2014 by the GRASS Development Team
  23. #
  24. # This program is free software under the GNU General Public
  25. # License (>=v2). Read the file COPYING that comes with GRASS
  26. # for details.
  27. #
  28. #############################################################################
  29. #%module
  30. #% description: Displays a raster map and its legend on a graphics window
  31. #% keyword: display
  32. #% keyword: cartography
  33. #% keyword: legend
  34. #%end
  35. #%flag
  36. #% key: f
  37. #% description: Flip legend
  38. #%end
  39. #%flag
  40. #% key: n
  41. #% description: Omit entries with missing label
  42. #%end
  43. #%option G_OPT_R_MAP
  44. #% description: Name of raster map to display
  45. #%end
  46. #%flag
  47. #% key: s
  48. #% description: Draw smooth gradient
  49. #%end
  50. #%option
  51. #% key: lines
  52. #% type: integer
  53. #% description: Number of lines to appear in the legend
  54. #% required: no
  55. #%end
  56. #%option G_OPT_R_INPUT
  57. #% key: raster
  58. #% description: Name of input raster map to generate legend from
  59. #% required: no
  60. #%end
  61. import sys
  62. import os
  63. import grass.script as grass
  64. def make_frame(f, b, t, l, r):
  65. (fl, fr, ft, fb) = f
  66. t /= 100.0
  67. b /= 100.0
  68. l /= 100.0
  69. r /= 100.0
  70. rt = fb + t * (ft - fb)
  71. rb = fb + b * (ft - fb)
  72. rl = fl + l * (fr - fl)
  73. rr = fl + r * (fr - fl)
  74. s = '%f,%f,%f,%f' % (rt, rb, rl, rr)
  75. os.environ['GRASS_RENDER_FRAME'] = s
  76. def main():
  77. map = options['map']
  78. nlines = options['lines']
  79. rast = options['raster']
  80. omit = flags['n']
  81. flip = flags['f']
  82. smooth = flags['s']
  83. # for -n flag of d.legend
  84. if not grass.find_file(map)['file']:
  85. grass.fatal(_("Raster map <%s> not found") % map)
  86. # for rast=
  87. if rast and not grass.find_file(rast)['file']:
  88. grass.fatal(_("Raster map <%s> not found") % rast)
  89. s = grass.read_command('d.info', flags='f')
  90. if not s:
  91. sys.exit(1)
  92. # fixes trunk r64459
  93. s = s.split(':')[1]
  94. f = tuple([float(x) for x in s.split()])
  95. grass.run_command('d.erase')
  96. os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
  97. # draw title
  98. # set vertical divide at 65 instead of 80 if real labels in cats/ file??
  99. make_frame(f, 90, 100, 70, 100)
  100. # use map name without mapset suffix
  101. mapname = map.split('@')[0]
  102. grass.run_command('d.text', color='black', size=5, at='5,97', align='cl',
  103. text=mapname)
  104. # draw legend
  105. # set legend vertical position and size based on number of categories
  106. cats = grass.read_command('r.describe', map=map, flags='1n')
  107. ncats = len(cats.strip().split('\n'))
  108. # Only need to adjust legend size if number of categories is between 1 and 10
  109. if ncats < 2:
  110. ncats = 2
  111. if ncats > 10:
  112. ncats = 10
  113. VSpacing = (100 - (ncats * 10) + 10)
  114. if not nlines:
  115. nlines = None
  116. if rast:
  117. lmap = rast
  118. else:
  119. lmap = map
  120. kv = grass.raster_info(map=lmap)
  121. if kv['datatype'] is 'CELL':
  122. leg_at = None
  123. else:
  124. leg_at = '%f,95,5,10' % VSpacing
  125. # checking for histogram causes more problems than it solves
  126. # histfiledir = grass.find_file(lmap, 'cell_misc')['file']
  127. # has_hist = os.path.isfile(os.path.join(histfiledir, 'histogram'))
  128. lflags = ''
  129. if flip:
  130. lflags += 'f'
  131. if omit:
  132. lflags += 'n'
  133. if smooth:
  134. lflags += 's'
  135. # if has_hist or omit:
  136. # lflags += 'n'
  137. make_frame(f, 0, 90, 70, 100)
  138. grass.run_command('d.legend', flags=lflags, raster=lmap, lines=nlines,
  139. at=leg_at)
  140. # draw map
  141. make_frame(f, 0, 100, 0, 70)
  142. grass.run_command('d.rast', map=map)
  143. if __name__ == "__main__":
  144. options, flags = grass.parser()
  145. main()