r.blend.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  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(
  63. "$$output.$ch = "
  64. 'if(isnull("$$first"), $ch#"$$second", '
  65. 'if(isnull("$$second"), $ch#"$$first", '
  66. '$$frac1 * $ch#"$$first" + '
  67. '$$frac2 * $ch#"$$second"))'
  68. )
  69. cmd = [template.substitute(ch=ch) for ch in ["r", "g", "b"]]
  70. cmd = ";".join(cmd)
  71. gscript.mapcalc(
  72. cmd, output=output, first=first, second=second, frac1=frac1, frac2=frac2
  73. )
  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(
  78. "r.support",
  79. map=map,
  80. history="",
  81. title="Color blend of %s and %s" % (first, second),
  82. description="generated by r.blend",
  83. )
  84. gscript.run_command("r.support", map=map, history="r.blend %s channel." % ch)
  85. gscript.run_command(
  86. "r.support",
  87. map=map,
  88. history=" %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second),
  89. )
  90. gscript.run_command("r.support", map=map, history="")
  91. gscript.run_command("r.support", map=map, history=os.environ["CMDLINE"])
  92. if flags["c"]:
  93. gscript.run_command(
  94. "r.composite",
  95. r="%s.r" % output,
  96. g="%s.g" % output,
  97. b="%s.b" % output,
  98. output=output,
  99. )
  100. gscript.run_command(
  101. "r.support",
  102. map=output,
  103. history="",
  104. title="Color blend of %s and %s" % (first, second),
  105. description="generated by r.blend",
  106. )
  107. gscript.run_command(
  108. "r.support",
  109. map=output,
  110. history=" %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second),
  111. )
  112. gscript.run_command("r.support", map=output, history="")
  113. gscript.run_command("r.support", map=output, history=os.environ["CMDLINE"])
  114. else:
  115. gscript.message(
  116. _("Done. Use the following command to visualize " "the result:")
  117. )
  118. gscript.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
  119. if __name__ == "__main__":
  120. options, flags = gscript.parser()
  121. main()