r.blend.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.blend for GRASS 5.7; based on blend.sh for GRASS 5
  5. # AUTHOR(S): CERL?; updated to GRASS 5.7 by Michael Barton
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: To redraw current displayed maps to 24 bit PNG output
  8. # COPYRIGHT: (C) 2004-2012 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #%module
  16. #% description: Blends color components of two raster maps by a given ratio.
  17. #% keywords: raster
  18. #% keywords: composite
  19. #%end
  20. #%option G_OPT_R_INPUT
  21. #% key: first
  22. #% description: Name of first raster map for blending
  23. #%end
  24. #%option G_OPT_R_INPUT
  25. #% key: second
  26. #% description: Name of second raster map for blending
  27. #%end
  28. #%option
  29. #% key: output_prefix
  30. #% type: string
  31. #% description: Prefix for red, green and blue output raster maps containing
  32. #% key_desc : name
  33. #% required : yes
  34. #%end
  35. #%option
  36. #% key: percent
  37. #% type: double
  38. #% answer: 50
  39. #% options : 0-100
  40. #% description: Percentage weight of first map for color blending
  41. #% required : no
  42. #%end
  43. #% flag
  44. #% key: c
  45. #% description: Combine resulting R,G,B layers into single output map
  46. #%end
  47. import sys
  48. import os
  49. import string
  50. import grass.script as grass
  51. def main():
  52. first = options['first']
  53. second = options['second']
  54. output = options['output_prefix']
  55. percent = options['percent']
  56. mapset = grass.gisenv()['MAPSET']
  57. if not grass.overwrite():
  58. for ch in ['r','g','b']:
  59. map = '%s.%s' % (output, ch)
  60. if grass.find_file(map, element = 'cell', mapset = mapset)['file']:
  61. grass.fatal(_("Raster map <%s> already exists.") % map)
  62. percent = float(percent)
  63. perc_inv = 100.0 - percent
  64. frac1 = percent / 100.0
  65. frac2 = perc_inv / 100.0
  66. grass.message(_("Calculating the three component maps..."))
  67. template = string.Template("$$output.$ch = if(isnull($$first), $ch#$$second, if(isnull($$second), $ch#$$first, $$frac1 * $ch#$$first + $$frac2 * $ch#$$second))")
  68. cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
  69. cmd = ';'.join(cmd)
  70. grass.mapcalc(cmd,
  71. output = output,
  72. first = first, second = second,
  73. frac1 = frac1, frac2 = frac2)
  74. for ch in ['r','g','b']:
  75. map = "%s.%s" % (output, ch)
  76. grass.run_command('r.colors', map = map, color = 'grey255')
  77. grass.run_command('r.support', map = map, history="",
  78. title = "Color blend of %s and %s" % (first, second),
  79. description = "generated by r.blend")
  80. grass.run_command('r.support', map = map,
  81. history = "r.blend %s channel." % ch)
  82. grass.run_command('r.support', map = map,
  83. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  84. grass.run_command('r.support', map = map, history = "")
  85. grass.run_command('r.support', map = map, history = os.environ['CMDLINE'])
  86. if flags['c']:
  87. grass.run_command('r.composite', r = '%s.r' % output,
  88. g = '%s.g' % output, b = '%s.b' % output, output = output)
  89. grass.run_command('r.support', map = output, history="",
  90. title = "Color blend of %s and %s" % (first, second),
  91. description = "generated by r.blend")
  92. grass.run_command('r.support', map = output,
  93. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  94. grass.run_command('r.support', map = output, history = "")
  95. grass.run_command('r.support', map = output, history = os.environ['CMDLINE'])
  96. else:
  97. grass.message(_("Done. Use the following command to visualize the result:"))
  98. grass.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
  99. if __name__ == "__main__":
  100. options, flags = grass.parser()
  101. main()