d.rast.leg.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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
  50. #% key: map
  51. #% type: string
  52. #% gisprompt: old,cell,raster
  53. #% description: Name of raster map
  54. #% required : yes
  55. #%end
  56. #%option
  57. #% key: num_of_lines
  58. #% type: integer
  59. #% description: Number of lines to appear in the legend
  60. #% required : no
  61. #%end
  62. #%option
  63. #% key: rast
  64. #% type: string
  65. #% gisprompt: old,cell,raster
  66. #% description: Name of raster map to generate legend from
  67. #% required : no
  68. #%end
  69. import sys
  70. import os
  71. import grass
  72. def make_frame(f, b, t, l, r):
  73. (ft, fb, fl, fr) = f
  74. t /= 100.0
  75. b /= 100.0
  76. l /= 100.0
  77. r /= 100.0
  78. rt = fb + t * (ft - fb)
  79. rb = fb + b * (ft - fb)
  80. rl = fl + l * (fr - fl)
  81. rr = fl + r * (fr - fl)
  82. s = '%f,%f,%f,%f' % (rt, rb, rl, rr)
  83. os.environ['GRASS_FRAME'] = s
  84. def main():
  85. map = options['map']
  86. nlines = options['num_of_lines']
  87. rast = options['rast']
  88. omit = flags['n']
  89. flip = flags['f']
  90. #for -n flag of d.legend
  91. if not grass.find_file(map)['file']:
  92. grass.fatal("Raster map <%s> not found in mapset search path" % map)
  93. # for rast=
  94. if rast and not grass.find_file(rast)['file']:
  95. grass.fatal("Raster map <%s> not found in mapset search path" % rast)
  96. s = grass.read_command('d.info', flags = 'f')
  97. f = tuple([float(x) for x in s.split()[1:5]])
  98. grass.run_command('d.erase')
  99. os.environ['GRASS_PNG_READ'] = 'TRUE'
  100. #draw title
  101. make_frame(f, 90, 100, 65, 100)
  102. grass.write_command('d.text', color = 'black', size = 30, stdin = map)
  103. #draw legend
  104. if not nlines:
  105. nlines = None
  106. if rast:
  107. lmap = rast
  108. else:
  109. lmap = map
  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)
  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()