d.rast.leg.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #%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. import sys
  64. import os
  65. import grass.script as grass
  66. def make_frame(f, b, t, l, r):
  67. (ft, fb, fl, fr) = f
  68. t /= 100.0
  69. b /= 100.0
  70. l /= 100.0
  71. r /= 100.0
  72. rt = fb + t * (ft - fb)
  73. rb = fb + b * (ft - fb)
  74. rl = fl + l * (fr - fl)
  75. rr = fl + r * (fr - fl)
  76. s = '%f,%f,%f,%f' % (rt, rb, rl, rr)
  77. os.environ['GRASS_FRAME'] = s
  78. def main():
  79. map = options['map']
  80. nlines = options['lines']
  81. rast = options['rast']
  82. omit = flags['n']
  83. flip = flags['f']
  84. #for -n flag of d.legend
  85. if not grass.find_file(map)['file']:
  86. grass.fatal(_("Raster map <%s> not found in mapset search path") % map)
  87. # for rast=
  88. if rast and not grass.find_file(rast)['file']:
  89. grass.fatal(_("Raster map <%s> not found in mapset search path") % rast)
  90. s = grass.read_command('d.info', flags = 'f')
  91. f = tuple([float(x) for x in s.split()[1:5]])
  92. grass.run_command('d.erase')
  93. os.environ['GRASS_PNG_READ'] = 'TRUE'
  94. #draw title
  95. # set vertical divide at 65 instead of 80 if real labels in cats/ file??
  96. make_frame(f, 90, 100, 80, 100)
  97. grass.write_command('d.text', color = 'black', size = 30, stdin = map)
  98. #draw legend
  99. if not nlines:
  100. nlines = None
  101. if rast:
  102. lmap = rast
  103. else:
  104. lmap = map
  105. kv = grass.raster_info(map = lmap)
  106. if kv['datatype'] is 'CELL':
  107. leg_at = None
  108. else:
  109. leg_at = '7,93,3,18'
  110. histfiledir = grass.find_file(lmap, 'cell_misc')['file']
  111. has_hist = os.path.isfile(os.path.join(histfiledir, 'histogram'))
  112. lflags = ''
  113. if flip:
  114. lflags += 'f'
  115. if has_hist or omit:
  116. lflags += 'n'
  117. make_frame(f, 0, 90, 65, 100)
  118. grass.run_command('d.legend', flags = lflags, map = lmap, lines = nlines, at = leg_at)
  119. #draw map
  120. make_frame(f, 0, 100, 0, 65)
  121. grass.run_command('d.rast', map = map)
  122. if __name__ == "__main__":
  123. options, flags = grass.parser()
  124. main()