r.blend.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #% keyword: raster
  18. #% keyword: 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 G_OPT_R_BASENAME_OUTPUT
  29. #% description: Basename for red, green and blue output raster maps
  30. #%end
  31. #%option
  32. #% key: percent
  33. #% type: double
  34. #% answer: 50
  35. #% options : 0-100
  36. #% description: Percentage weight of first map for color blending
  37. #% required : no
  38. #%end
  39. #% flag
  40. #% key: c
  41. #% description: Combine resulting R,G,B layers into single output map
  42. #%end
  43. import sys
  44. import os
  45. import string
  46. import grass.script as grass
  47. def main():
  48. first = options['first']
  49. second = options['second']
  50. output = options['output']
  51. percent = options['percent']
  52. mapset = grass.gisenv()['MAPSET']
  53. if not grass.overwrite():
  54. for ch in ['r','g','b']:
  55. map = '%s.%s' % (output, ch)
  56. if grass.find_file(map, element = 'cell', mapset = mapset)['file']:
  57. grass.fatal(_("Raster map <%s> already exists.") % map)
  58. percent = float(percent)
  59. perc_inv = 100.0 - percent
  60. frac1 = percent / 100.0
  61. frac2 = perc_inv / 100.0
  62. grass.message(_("Calculating the three component maps..."))
  63. template = string.Template("$$output.$ch = if(isnull($$first), $ch#$$second, if(isnull($$second), $ch#$$first, $$frac1 * $ch#$$first + $$frac2 * $ch#$$second))")
  64. cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
  65. cmd = ';'.join(cmd)
  66. grass.mapcalc(cmd,
  67. output = output,
  68. first = first, second = second,
  69. frac1 = frac1, frac2 = frac2)
  70. for ch in ['r','g','b']:
  71. map = "%s.%s" % (output, ch)
  72. grass.run_command('r.colors', map = map, color = 'grey255')
  73. grass.run_command('r.support', map = map, history="",
  74. title = "Color blend of %s and %s" % (first, second),
  75. description = "generated by r.blend")
  76. grass.run_command('r.support', map = map,
  77. history = "r.blend %s channel." % ch)
  78. grass.run_command('r.support', map = map,
  79. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  80. grass.run_command('r.support', map = map, history = "")
  81. grass.run_command('r.support', map = map, history = os.environ['CMDLINE'])
  82. if flags['c']:
  83. grass.run_command('r.composite', r = '%s.r' % output,
  84. g = '%s.g' % output, b = '%s.b' % output, output = output)
  85. grass.run_command('r.support', map = output, history="",
  86. title = "Color blend of %s and %s" % (first, second),
  87. description = "generated by r.blend")
  88. grass.run_command('r.support', map = output,
  89. history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second))
  90. grass.run_command('r.support', map = output, history = "")
  91. grass.run_command('r.support', map = output, history = os.environ['CMDLINE'])
  92. else:
  93. grass.message(_("Done. Use the following command to visualize the result:"))
  94. grass.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. main()