i.fusion.brovey.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: i.fusion.brovey
  5. # AUTHOR(S): Markus Neteler. <neteler itc it>
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Brovey transform to merge
  8. # - LANDSAT-7 MS (2, 4, 5) and pan (high res)
  9. # - SPOT MS and pan (high res)
  10. # - QuickBird MS and pan (high res)
  11. #
  12. # COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
  13. #
  14. # This program is free software under the GNU General Public
  15. # License (>=v2). Read the file COPYING that comes with GRASS
  16. # for details.
  17. #
  18. # REFERENCES:
  19. # (?) Roller, N.E.G. and Cox, S., 1980. Comparison of Landsat MSS
  20. # and merged MSS/RBV data for analysis of natural vegetation.
  21. # Proc. of the 14th International Symposium on Remote Sensing
  22. # of Environment, San Jose, Costa Rica, 23-30 April, pp. 1001-1007.
  23. #
  24. # for LANDSAT 5: see Pohl, C 1996 and others
  25. #
  26. # TODO: add overwrite test at beginning of the script
  27. #############################################################################
  28. #%Module
  29. #% description: Brovey transform to merge multispectral and high-res panchromatic channels
  30. #% keywords: imagery
  31. #% keywords: fusion
  32. #% keywords: Brovey
  33. #%End
  34. #%Flag
  35. #% key: l
  36. #% description: LANDSAT sensor
  37. #% guisection: Sensor
  38. #%END
  39. #%Flag
  40. #% key: q
  41. #% description: QuickBird sensor
  42. #% guisection: Sensor
  43. #%END
  44. #%Flag
  45. #% key: s
  46. #% description: SPOT sensor
  47. #% guisection: Sensor
  48. #%END
  49. #%option
  50. #% key: ms1
  51. #% type: string
  52. #% gisprompt: old,cell,raster
  53. #% description: Name of input raster map (green: tm2 | qbird_green | spot1)
  54. #% required : yes
  55. #%end
  56. #%option
  57. #% key: ms2
  58. #% type: string
  59. #% gisprompt: old,cell,raster
  60. #% description: Name of input raster map (NIR: tm4 | qbird_nir | spot2
  61. #% required : yes
  62. #%end
  63. #%option
  64. #% key: ms3
  65. #% type: string
  66. #% gisprompt: old,cell,raster
  67. #% description: Name of input raster map (MIR; tm5 | qbird_red | spot3
  68. #% required : yes
  69. #%end
  70. #%option
  71. #% key: pan
  72. #% type: string
  73. #% gisprompt: old,cell,raster
  74. #% description: Name of input raster map (etmpan | qbird_pan | spotpan)
  75. #% required : yes
  76. #%end
  77. #%option
  78. #% key: outputprefix
  79. #% type: string
  80. #% gisprompt: new,cell,raster
  81. #% description: Name for output raster map prefix (e.g. 'brov')
  82. #% required : yes
  83. #%end
  84. import sys
  85. import os
  86. import grass.script as grass
  87. def main():
  88. global tmp
  89. landsat = flags['l']
  90. quickbird = flags['q']
  91. spot = flags['s']
  92. ms1 = options['ms1']
  93. ms2 = options['ms2']
  94. ms3 = options['ms3']
  95. pan = options['pan']
  96. out = options['outputprefix']
  97. tmp = str(os.getpid())
  98. if not landsat and not quickbird and not spot:
  99. grass.fatal(_("Please select a flag to specify the satellite sensor"))
  100. #get PAN resolution:
  101. kv = grass.raster_info(map = pan)
  102. nsres = kv['nsres']
  103. ewres = kv['ewres']
  104. panres = (nsres + ewres) / 2
  105. # clone current region
  106. grass.use_temp_region()
  107. grass.verbose("Using resolution from PAN: %f" % panres)
  108. grass.run_command('g.region', flags = 'a', res = panres)
  109. grass.verbose("Performing Brovey transformation...")
  110. # The formula was originally developed for LANDSAT-TM5 and SPOT,
  111. # but it also works well with LANDSAT-TM7
  112. # LANDSAT formula:
  113. # r.mapcalc "brov.red=1. * tm.5 / (tm.2 + tm.4 + tm.5) * etmpan"
  114. # r.mapcalc "brov.green=1. * tm.4 /(tm.2 + tm.4 + tm.5) * etmpan"
  115. # r.mapcalc "brov.blue=1. * tm.2 / (tm.2 + tm.4 + tm.5) * etmpan"
  116. #
  117. # SPOT formula:
  118. # r.mapcalc "brov.red= 1. * spot.ms.3 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  119. # r.mapcalc "brov.green=1. * spot.ms.2 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  120. # r.mapcalc "brov.blue= 1. * spot.ms.1 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  121. # note: for RGB composite then revert brov.red and brov.green!
  122. grass.message(_("Calculating %s.{red,green,blue}: ...") % out)
  123. e = '''eval(k = float("$pan") / ("$ms1" + "$ms2" + "$ms3"))
  124. "$out.red" = "$ms3" * k
  125. "$out.green" = "$ms2" * k
  126. "$out.blue" = "$ms1" * k'''
  127. grass.mapcalc(e, out = out, pan = pan, ms1 = ms1, ms2 = ms2, ms3 = ms3)
  128. # Maybe?
  129. #r.colors $GIS_OPT_OUTPUTPREFIX.red col=grey
  130. #r.colors $GIS_OPT_OUTPUTPREFIX.green col=grey
  131. #r.colors $GIS_OPT_OUTPUTPREFIX.blue col=grey
  132. #to blue-ish, therefore we modify
  133. #r.colors $GIS_OPT_OUTPUTPREFIX.blue col=rules << EOF
  134. #5 0 0 0
  135. #20 200 200 200
  136. #40 230 230 230
  137. #67 255 255 255
  138. #EOF
  139. if spot:
  140. #apect table is nice for SPOT:
  141. grass.message(_("Assigning color tables for SPOT..."))
  142. for ch in ['red', 'green', 'blue']:
  143. grass.run_command('r.colors', map = "%s.%s" % (out, ch), col = 'aspect')
  144. grass.message(_("Fixing output names..."))
  145. for s, d in [('green','tmp'),('red','green'),('tmp','red')]:
  146. src = "%s.%s" % (out, s)
  147. dst = "%s.%s" % (out, d)
  148. grass.run_command('g.rename', rast = (src, dst), quiet = True)
  149. else:
  150. #aspect table is nice for LANDSAT and QuickBird:
  151. grass.message(_("Assigning color tables for LANDSAT or QuickBird..."))
  152. for ch in ['red', 'green', 'blue']:
  153. grass.run_command('r.colors', map = "%s.%s" % (out, ch), col = 'aspect')
  154. grass.message(_("Following pan-sharpened output maps have been generated:"))
  155. for ch in ['red', 'green', 'blue']:
  156. grass.message(_("%s.%s") % (out, ch))
  157. grass.verbose("To visualize output, run:")
  158. grass.verbose("g.region -p rast=%s.red" % out)
  159. grass.verbose("d.rgb r=%s.red g=%s.green b=%s.blue" % (out, out, out))
  160. grass.verbose("If desired, combine channels with 'r.composite' to a single map.")
  161. # write cmd history:
  162. for ch in ['red', 'green', 'blue']:
  163. grass.raster_history("%s.%s" % (out, ch))
  164. if __name__ == "__main__":
  165. options, flags = grass.parser()
  166. main()