i.oif.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: i.oif
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: calculates the Optimum Index factor of all band combinations
  8. # for LANDSAT TM 1,2,3,4,5,7
  9. # COPYRIGHT: (C) 1999,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. #
  15. #############################################################################
  16. # Ref.: Jensen: Introductory digital image processing 1996, p.98
  17. #
  18. # Input: tm1 - tm5, tm7 (not tm6)
  19. #
  20. # written by Markus Neteler 21.July 1998
  21. # neteler geog.uni-hannover.de
  22. # updated for GRASS 5.7 by Michael Barton 2004/04/05
  23. #% Module
  24. #% description: Calculates Optimum-Index-Factor table for LANDSAT TM bands 1-5, & 7
  25. #% keywords: raster, imagery, statistics
  26. #% End
  27. #% option
  28. #% key: image1
  29. #% type: string
  30. #% gisprompt: old,cell,raster
  31. #% description: LANDSAT TM band 1.
  32. #% required : yes
  33. #% end
  34. #% option
  35. #% key: image2
  36. #% type: string
  37. #% gisprompt: old,cell,raster
  38. #% description: LANDSAT TM band 2.
  39. #% required : yes
  40. #% end
  41. #% option
  42. #% key: image3
  43. #% type: string
  44. #% gisprompt: old,cell,raster
  45. #% description: LANDSAT TM band 3.
  46. #% required : yes
  47. #% end
  48. #% option
  49. #% key: image4
  50. #% type: string
  51. #% gisprompt: old,cell,raster
  52. #% description: LANDSAT TM band 4.
  53. #% required : yes
  54. #% end
  55. #% option
  56. #% key: image5
  57. #% type: string
  58. #% gisprompt: old,cell,raster
  59. #% description: LANDSAT TM band 5.
  60. #% required : yes
  61. #% end
  62. #% option
  63. #% key: image7
  64. #% type: string
  65. #% gisprompt: old,cell,raster
  66. #% description: LANDSAT TM band 7.
  67. #% required : yes
  68. #% end
  69. #% Flag
  70. #% key: g
  71. #% description: Print in shell script style
  72. #% End
  73. import sys
  74. import os
  75. from grass.script import core as grass
  76. bands = [1,2,3,4,5,7]
  77. def oifcalc(sdev, corr, k1, k2, k3):
  78. # calculate SUM of Stddeviations:
  79. ssdev = [sdev[k1], sdev[k2], sdev[k3]]
  80. numer = sum(ssdev)
  81. # calculate SUM of absolute(Correlation values):
  82. scorr = [corr[k1,k2], corr[k1,k3], corr[k2,k3]]
  83. denom = sum(map(abs, scorr))
  84. # Calculate OIF index:
  85. # Divide (SUM of Stddeviations) and (SUM of Correlation)
  86. return numer / denom
  87. def perms():
  88. for i in range(0,4):
  89. for j in range(i+1,5):
  90. for k in range(j+1,6):
  91. yield (bands[i], bands[j], bands[k])
  92. def main():
  93. shell = flags['g']
  94. image = {}
  95. for band in bands:
  96. image[band] = options['image%d' % band]
  97. # calculate the Stddev for TM bands
  98. grass.verbose("Calculating Standard deviations for all bands:")
  99. stddev = {}
  100. for band in bands:
  101. grass.verbose("band %d" % band)
  102. s = grass.read_command('r.univar', flags = 'g', map = image[band])
  103. kv = grass.parse_key_val(s)
  104. stddev[band] = float(kv['stddev'])
  105. grass.verbose("Calculating Correlation Matrix")
  106. correlation = {}
  107. s = grass.read_command('r.covar', flags = 'r', map = [image[band] for band in bands])
  108. for i, row in zip(bands, s.splitlines()):
  109. for j, cell in zip(bands, row.split(' ')):
  110. correlation[i,j] = float(cell)
  111. # Calculate all combinations
  112. grass.verbose("Calculating OIF for the 20 band combinations...")
  113. oif = []
  114. for p in perms():
  115. oif.append((oifcalc(stddev, correlation, *p), p))
  116. oif.sort(reverse = True)
  117. grass.verbose("Ready.")
  118. grass.verbose("The Optimum Index Factor analysis result:")
  119. grass.verbose(" (Best combination comes first.)")
  120. if shell:
  121. fmt = "%d%d%d:%f\n"
  122. else:
  123. fmt = "%d%d%d: %f\n"
  124. outf = file('i.oif.result', 'w')
  125. for v, p in oif:
  126. sys.stdout.write(fmt % (p + (v,)))
  127. outf.write(fmt % (p + (v,)))
  128. outf.close()
  129. if __name__ == "__main__":
  130. options, flags = grass.parser()
  131. main()