d.rast.leg.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #% keywords: cartography
  41. #%end
  42. #%flag
  43. #% key: f
  44. #% description: Flip legend
  45. #%end
  46. #%flag
  47. #% key: n
  48. #% description: Omit entries with missing label
  49. #%end
  50. #%option G_OPT_R_MAP
  51. #% description: Name of raster map to display
  52. #%end
  53. #%flag
  54. #% key: s
  55. #% description: Draw smooth gradient
  56. #%end
  57. #%option
  58. #% key: lines
  59. #% type: integer
  60. #% description: Number of lines to appear in the legend
  61. #% required: no
  62. #%end
  63. #%option G_OPT_R_INPUT
  64. #% key: raster
  65. #% description: Name of input raster map to generate legend from
  66. #% required: no
  67. #%end
  68. import sys
  69. import os
  70. import grass.script as grass
  71. def make_frame(f, b, t, l, r):
  72. (fl, fr, ft, fb) = f
  73. t /= 100.0
  74. b /= 100.0
  75. l /= 100.0
  76. r /= 100.0
  77. rt = fb + t * (ft - fb)
  78. rb = fb + b * (ft - fb)
  79. rl = fl + l * (fr - fl)
  80. rr = fl + r * (fr - fl)
  81. s = '%f,%f,%f,%f' % (rt, rb, rl, rr)
  82. os.environ['GRASS_FRAME'] = s
  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. #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. # set vertical divide at 65 instead of 80 if real labels in cats/ file??
  102. make_frame(f, 90, 100, 70, 100)
  103. # use map name without mapset suffix
  104. mapname = map.split('@')[0]
  105. grass.run_command('d.text', color='black', size=50, at='5,50', align='cl', text=mapname)
  106. #draw legend
  107. # set legend vertical position and size based on number of categories
  108. cats = grass.read_command('r.describe', map=map, flags = '1n')
  109. ncats = len(cats.strip().split('\n'))
  110. # Only need to adjust legend size if number of categories is between 1 and 10
  111. if ncats < 2: ncats = 2
  112. if ncats > 10: 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,15' %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, map = lmap, lines = nlines, at = leg_at)
  139. #draw map
  140. make_frame(f, 0, 100, 0, 70)
  141. grass.run_command('d.rast', map = map)
  142. if __name__ == "__main__":
  143. options, flags = grass.parser()
  144. main()