r.blend.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 os
  44. import string
  45. import grass.script as gscript
  46. def main():
  47. first = options['first']
  48. second = options['second']
  49. output = options['output']
  50. percent = options['percent']
  51. mapset = gscript.gisenv()['MAPSET']
  52. if not gscript.overwrite():
  53. for ch in ['r', 'g', 'b']:
  54. map = '%s.%s' % (output, ch)
  55. if gscript.find_file(map, element='cell', mapset=mapset)['file']:
  56. gscript.fatal(_("Raster map <%s> already exists.") % map)
  57. percent = float(percent)
  58. perc_inv = 100.0 - percent
  59. frac1 = percent / 100.0
  60. frac2 = perc_inv / 100.0
  61. gscript.message(_("Calculating the three component maps..."))
  62. template = string.Template("$$output.$ch = "
  63. "if(isnull($$first), $ch#$$second, "
  64. "if(isnull($$second), $ch#$$first, "
  65. "$$frac1 * $ch#$$first + "
  66. "$$frac2 * $ch#$$second))")
  67. cmd = [template.substitute(ch=ch) for ch in ['r', 'g', 'b']]
  68. cmd = ';'.join(cmd)
  69. gscript.mapcalc(cmd, output=output, first=first, second=second,
  70. frac1=frac1, frac2=frac2)
  71. for ch in ['r', 'g', 'b']:
  72. map = "%s.%s" % (output, ch)
  73. gscript.run_command('r.colors', map=map, color='grey255')
  74. gscript.run_command('r.support', map=map, history="",
  75. title="Color blend of %s and %s" % (first, second),
  76. description="generated by r.blend")
  77. gscript.run_command('r.support', map=map,
  78. history="r.blend %s channel." % ch)
  79. gscript.run_command('r.support', map=map,
  80. history=" %d%% of %s, %d%% of %s" %
  81. (percent, first, perc_inv, second))
  82. gscript.run_command('r.support', map=map, history="")
  83. gscript.run_command('r.support', map=map, history=os.environ['CMDLINE'])
  84. if flags['c']:
  85. gscript.run_command('r.composite', r='%s.r' % output,
  86. g='%s.g' % output, b='%s.b' % output,
  87. output=output)
  88. gscript.run_command('r.support', map=output, history="",
  89. title="Color blend of %s and %s" % (first, second),
  90. description="generated by r.blend")
  91. gscript.run_command('r.support', map=output,
  92. history=" %d%% of %s, %d%% of %s" %
  93. (percent, first, perc_inv, second))
  94. gscript.run_command('r.support', map=output, history="")
  95. gscript.run_command('r.support', map=output,
  96. history=os.environ['CMDLINE'])
  97. else:
  98. gscript.message(_("Done. Use the following command to visualize "
  99. "the result:"))
  100. gscript.message(_("d.rgb r=%s.r g=%s.g b=%s.b") %
  101. (output, output, output))
  102. if __name__ == "__main__":
  103. options, flags = gscript.parser()
  104. main()