d.correlate.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: d.correlate for GRASS 6; based on dcorrelate.sh for GRASS 4,5
  6. # AUTHOR(S): CERL - Michael Shapiro; updated to GRASS 6 by Markus Neteler 5/2005
  7. # Converted to Python by Glynn Clements
  8. # PURPOSE: prints a graph of the correlation between data layers (in pairs)
  9. # derived from <grass5>/src.local/d.correlate.sh
  10. # COPYRIGHT: (C) 2005, 2008, 2011 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Prints a graph of the correlation between raster maps (in pairs).
  19. #% keyword: display
  20. #% keyword: statistics
  21. #% keyword: raster
  22. #% keyword: diagram
  23. #% keyword: correlation
  24. #%end
  25. #%option G_OPT_R_MAPS
  26. #%end
  27. import sys
  28. import os
  29. from grass.script.utils import try_remove
  30. from grass.script import core as gcore
  31. from grass.exceptions import CalledModuleError
  32. def main():
  33. layers = options['map'].split(',')
  34. if len(layers) < 2:
  35. gcore.error(_("At least 2 maps are required"))
  36. tmpfile = gcore.tempfile()
  37. for map in layers:
  38. if not gcore.find_file(map, element='cell')['file']:
  39. gcore.fatal(_("Raster map <%s> not found") % map)
  40. try:
  41. gcore.write_command('d.text', color='black', size=4, line=1,
  42. stdin="CORRELATION")
  43. except CalledModuleError:
  44. return 1
  45. os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
  46. colors = "red black blue green gray violet".split()
  47. line = 2
  48. iloop = 0
  49. jloop = 0
  50. for iloop, i in enumerate(layers):
  51. for jloop, j in enumerate(layers):
  52. if i != j and iloop <= jloop:
  53. color = colors[0]
  54. colors = colors[1:]
  55. colors.append(color)
  56. gcore.write_command('d.text', color=color, size=4, line=line,
  57. stdin="%s %s" % (i, j))
  58. line += 1
  59. ofile = file(tmpfile, 'w')
  60. gcore.run_command('r.stats', flags='cnA', input=(i, j),
  61. stdout=ofile)
  62. ofile.close()
  63. ifile = file(tmpfile, 'r')
  64. first = True
  65. for l in ifile:
  66. f = l.rstrip('\r\n').split(' ')
  67. x = float(f[0])
  68. y = float(f[1])
  69. if first:
  70. minx = maxx = x
  71. miny = maxy = y
  72. first = False
  73. if minx > x:
  74. minx = x
  75. if maxx < x:
  76. maxx = x
  77. if miny > y:
  78. miny = y
  79. if maxy < y:
  80. maxy = y
  81. ifile.close()
  82. kx = 100.0 / (maxx - minx + 1)
  83. ky = 100.0 / (maxy - miny + 1)
  84. p = gcore.feed_command('d.graph', color=color)
  85. ofile = p.stdin
  86. ifile = file(tmpfile, 'r')
  87. for l in ifile:
  88. f = l.rstrip('\r\n').split(' ')
  89. x = float(f[0])
  90. y = float(f[1])
  91. ofile.write("icon + 0.1 %f %f\n" % ((x - minx + 1) * kx,
  92. (y - miny + 1) * ky))
  93. ifile.close()
  94. ofile.close()
  95. p.wait()
  96. try_remove(tmpfile)
  97. return 0
  98. if __name__ == "__main__":
  99. options, flags = gcore.parser()
  100. sys.exit(main())