d.rast.leg.py 4.5 KB

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