i.fusion.brovey.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, fusion, Brovey
  31. #%End
  32. #%Flag
  33. #% key: l
  34. #% description: LANDSAT sensor
  35. #% guisection: Sensor
  36. #%END
  37. #%Flag
  38. #% key: q
  39. #% description: QuickBird sensor
  40. #% guisection: Sensor
  41. #%END
  42. #%Flag
  43. #% key: s
  44. #% description: SPOT sensor
  45. #% guisection: Sensor
  46. #%END
  47. #%option
  48. #% key: ms1
  49. #% type: string
  50. #% gisprompt: old,cell,raster
  51. #% description: Name of input raster map (green: tm2 | qbird_green | spot1)
  52. #% required : yes
  53. #%end
  54. #%option
  55. #% key: ms2
  56. #% type: string
  57. #% gisprompt: old,cell,raster
  58. #% description: Name of input raster map (NIR: tm4 | qbird_nir | spot2
  59. #% required : yes
  60. #%end
  61. #%option
  62. #% key: ms3
  63. #% type: string
  64. #% gisprompt: old,cell,raster
  65. #% description: Name of input raster map (MIR; tm5 | qbird_red | spot3
  66. #% required : yes
  67. #%end
  68. #%option
  69. #% key: pan
  70. #% type: string
  71. #% gisprompt: old,cell,raster
  72. #% description: Name of input raster map (etmpan | qbird_pan | spotpan)
  73. #% required : yes
  74. #%end
  75. #%option
  76. #% key: outputprefix
  77. #% type: string
  78. #% gisprompt: new,cell,raster
  79. #% description: Name for output raster map prefix (e.g. 'brov')
  80. #% required : yes
  81. #%end
  82. import sys
  83. import os
  84. from grass.script import core, raster as grass
  85. def main():
  86. global tmp
  87. landsat = flags['l']
  88. quickbird = flags['q']
  89. spot = flags['s']
  90. ms1 = options['ms1']
  91. ms2 = options['ms2']
  92. ms3 = options['ms3']
  93. pan = options['pan']
  94. out = options['outputprefix']
  95. tmp = str(os.getpid())
  96. if not landsat and not quickbird and not spot:
  97. grass.fatal("Please select a flag to specify the satellite sensor")
  98. #get PAN resolution:
  99. kv = grass.raster_info(map = pan)
  100. nsres = kv['nsres']
  101. ewres = kv['ewres']
  102. panres = (nsres + ewres) / 2
  103. # clone current region
  104. grass.use_temp_region()
  105. grass.verbose("Using resolution from PAN: %f" % panres)
  106. grass.run_command('g.region', flags = 'a', res = panres)
  107. grass.verbose("Performing Brovey transformation...")
  108. # The formula was originally developed for LANDSAT-TM5 and SPOT,
  109. # but it also works well with LANDSAT-TM7
  110. # LANDSAT formula:
  111. # r.mapcalc "brov.red=1. * tm.5 / (tm.2 + tm.4 + tm.5) * etmpan"
  112. # r.mapcalc "brov.green=1. * tm.4 /(tm.2 + tm.4 + tm.5) * etmpan"
  113. # r.mapcalc "brov.blue=1. * tm.2 / (tm.2 + tm.4 + tm.5) * etmpan"
  114. #
  115. # SPOT formula:
  116. # r.mapcalc "brov.red= 1. * spot.ms.3 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  117. # r.mapcalc "brov.green=1. * spot.ms.2 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  118. # r.mapcalc "brov.blue= 1. * spot.ms.1 / (spot.ms.1 + spot.ms.2 + spot.ms.3) * spot.p"
  119. # note: for RGB composite then revert brov.red and brov.green!
  120. grass.message("Calculating %s.{red,green,blue}: ..." % out)
  121. e = '''eval(k = float("$pan") / ("$ms1" + "$ms2" + "$ms3"))
  122. "$out.red" = "$ms3" * k
  123. "$out.green" = "$ms2" * k
  124. "$out.blue" = "$ms1" * k'''
  125. grass.mapcalc(e, out = out, pan = pan, ms1 = ms1, ms2 = ms2, ms3 = ms3)
  126. # Maybe?
  127. #r.colors $GIS_OPT_OUTPUTPREFIX.red col=grey
  128. #r.colors $GIS_OPT_OUTPUTPREFIX.green col=grey
  129. #r.colors $GIS_OPT_OUTPUTPREFIX.blue col=grey
  130. #to blue-ish, therefore we modify
  131. #r.colors $GIS_OPT_OUTPUTPREFIX.blue col=rules << EOF
  132. #5 0 0 0
  133. #20 200 200 200
  134. #40 230 230 230
  135. #67 255 255 255
  136. #EOF
  137. if spot:
  138. #apect table is nice for SPOT:
  139. grass.message("Assigning color tables for SPOT...")
  140. for ch in ['red', 'green', 'blue']:
  141. grass.run_command('r.colors', map = "%s.%s" % (out, ch), col = 'aspect')
  142. grass.message("Fixing output names...")
  143. for s, d in [('green','tmp'),('red','green'),('tmp','red')]:
  144. src = "%s.%s" % (out, s)
  145. dst = "%s.%s" % (out, d)
  146. grass.run_command('g.rename', rast = (src, dst), quiet = True)
  147. else:
  148. #aspect table is nice for LANDSAT and QuickBird:
  149. grass.message("Assigning color tables for LANDSAT or QuickBird...")
  150. for ch in ['red', 'green', 'blue']:
  151. grass.run_command('r.colors', map = "%s.%s" % (out, ch), col = 'aspect')
  152. grass.message("Following pan-sharpened output maps have been generated:")
  153. for ch in ['red', 'green', 'blue']:
  154. grass.message("%s.%s" % (out, ch))
  155. grass.verbose("To visualize output, run:")
  156. grass.verbose("g.region -p rast=%s.red" % out)
  157. grass.verbose("d.rgb r=%s.red g=%s.green b=%s.blue" % (out, out, out))
  158. grass.verbose("If desired, combine channels with 'r.composite' to a single map.")
  159. # write cmd history:
  160. for ch in ['red', 'green', 'blue']:
  161. grass.raster_history("%s.%s" % (out, ch))
  162. if __name__ == "__main__":
  163. options, flags = grass.parser()
  164. main()