r.blend.py 4.4 KB

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