i.tasscap.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: i.tasscap
  6. # AUTHOR(S): Markus Neteler. neteler itc.it
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: At-satellite reflectance based tasseled cap transformation.
  9. # COPYRIGHT: (C) 1997-2004,2008 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. # TODO: Check if MODIS Tasseled Cap makes sense to be added
  15. # http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=1025776
  16. #############################################################################
  17. # References:
  18. # LANDSAT-4/LANDSAT-5:
  19. # script based on i.tasscap.tm4 from Dr. Agustin Lobo - alobo@ija.csic.es
  20. # TC-factor changed to CRIST et al. 1986, p.1467 (Markus Neteler 1/99)
  21. # Proc. IGARSS 1986
  22. #
  23. # LANDSAT-7:
  24. # TASSCAP factors cited from:
  25. # DERIVATION OF A TASSELED CAP TRANSFORMATION BASED ON LANDSAT 7 AT-SATELLITE REFLECTANCE
  26. # Chengquan Huang, Bruce Wylie, Limin Yang, Collin Homer and Gregory Zylstra Raytheon ITSS,
  27. # USGS EROS Data Center Sioux Falls, SD 57198, USA
  28. # http://landcover.usgs.gov/pdf/tasseled.pdf
  29. #
  30. # This is published as well in INT. J. OF RS, 2002, VOL 23, NO. 8, 1741-1748.
  31. # Compare discussion:
  32. # http://adis.cesnet.cz/cgi-bin/lwgate/IMAGRS-L/archives/imagrs-l.log0211/date/article-14.html
  33. #############################################################################
  34. #
  35. #%Module
  36. #% description: Tasseled Cap (Kauth Thomas) transformation for LANDSAT-TM data
  37. #% keywords: raster, imagery
  38. #%End
  39. #%flag
  40. #% key: 4
  41. #% description: use transformation rules for LANDSAT-4
  42. #%END
  43. #%flag
  44. #% key: 5
  45. #% description: use transformation rules for LANDSAT-5
  46. #%END
  47. #%flag
  48. #% key: 7
  49. #% description: use transformation rules for LANDSAT-7
  50. #%END
  51. #%option
  52. #% key: band1
  53. #% type: string
  54. #% gisprompt: old,cell,raster
  55. #% description: raster input map (LANDSAT channel 1)
  56. #% required : yes
  57. #%end
  58. #%option
  59. #% key: band2
  60. #% type: string
  61. #% gisprompt: old,cell,raster
  62. #% description: raster input map (LANDSAT channel 2)
  63. #% required : yes
  64. #%end
  65. #%option
  66. #% key: band3
  67. #% type: string
  68. #% gisprompt: old,cell,raster
  69. #% description: raster input map (LANDSAT channel 3)
  70. #% required : yes
  71. #%end
  72. #%option
  73. #% key: band4
  74. #% type: string
  75. #% gisprompt: old,cell,raster
  76. #% description: raster input map (LANDSAT channel 4)
  77. #% required : yes
  78. #%end
  79. #%option
  80. #% key: band5
  81. #% type: string
  82. #% gisprompt: old,cell,raster
  83. #% description: raster input map (LANDSAT channel 5)
  84. #% required : yes
  85. #%end
  86. #%option
  87. #% key: band7
  88. #% type: string
  89. #% gisprompt: old,cell,raster
  90. #% description: raster input map (LANDSAT channel 7)
  91. #% required : yes
  92. #%end
  93. #%option
  94. #% key: outprefix
  95. #% type: string
  96. #% gisprompt: new,cell,raster
  97. #% description: raster output TC maps prefix
  98. #% required : yes
  99. #%end
  100. import sys
  101. import os
  102. import string
  103. import grass
  104. parms = [[( 0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863),
  105. (-0.2848,-0.2435,-0.5435, 0.7243, 0.0840,-0.1800),
  106. ( 0.1509, 0.1973, 0.3279, 0.3406,-0.7112,-0.4572)],
  107. [( 0.2909, 0.2493, 0.4806, 0.5568, 0.4438, 0.1706, 10.3695),
  108. (-0.2728,-0.2174,-0.5508, 0.7221, 0.0733,-0.1648, -0.7310),
  109. ( 0.1446, 0.1761, 0.3322, 0.3396,-0.6210,-0.4186, -3.3828),
  110. ( 0.8461,-0.0731,-0.4640,-0.0032,-0.0492,-0.0119, 0.7879)],
  111. [( 0.3561, 0.3972, 0.3904, 0.6966, 0.2286, 0.1596),
  112. (-0.3344,-0.3544,-0.4556, 0.6966,-0.0242,-0.2630),
  113. ( 0.2626, 0.2141, 0.0926, 0.0656,-0.7629,-0.5388),
  114. ( 0.0805,-0.0498, 0.1950,-0.1327, 0.5752,-0.7775)]]
  115. ordinals = ["first", "second", "third", "fourth"]
  116. names = ["Brightness", "Greenness", "Wetness", "Haze"]
  117. def calc1(out, bands, k1, k2, k3, k4, k5, k7, k0 = 0):
  118. t = string.Template("$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0")
  119. e = t.substitute(out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
  120. grass.run_command('r.mapcalc', expression = e)
  121. grass.run_command('r.colors', map = out, color = 'grey')
  122. def calcN(options, i, n):
  123. outpre = options['outprefix']
  124. grass.message("LANDSAT-%d..." % n)
  125. for j, p in enumerate(parms[i]):
  126. out = "%s.%d" % (outpre, j + 1)
  127. ord = ordinals[j]
  128. if n == 4:
  129. name = ''
  130. else:
  131. name = " (%s)" % names[j]
  132. grass.message("Calculating %s TC component %s%s ..." % (ord, out, name))
  133. calc1(out, options, *p)
  134. def main():
  135. flag4 = flags['4']
  136. flag5 = flags['5']
  137. flag7 = flags['7']
  138. if (flag4 and flag5) or (flag4 and flag7) or (flag5 and flag7):
  139. grass.fatal("Select only one flag")
  140. if flag4:
  141. calcN(options, 0, 4)
  142. elif flag5:
  143. calcN(options, 1, 5)
  144. elif flag7:
  145. calcN(options, 2, 7)
  146. else:
  147. grass.fatal("Select LANDSAT satellite by flag!")
  148. grass.message("Tasseled Cap components calculated.")
  149. if __name__ == "__main__":
  150. options, flags = grass.parser()
  151. main()