d.correlate.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. def main():
  32. layers = options['map'].split(',')
  33. if len(layers) < 2:
  34. gcore.error(_("At least 2 maps are required"))
  35. tmpfile = gcore.tempfile()
  36. for map in layers:
  37. if not gcore.find_file(map, element='cell')['file']:
  38. gcore.fatal(_("Raster map <%s> not found") % map)
  39. gcore.write_command('d.text', color='black', size=4, line=1,
  40. stdin="CORRELATION")
  41. os.environ['GRASS_RENDER_FILE_READ'] = 'TRUE'
  42. colors = "red black blue green gray violet".split()
  43. line = 2
  44. iloop = 0
  45. jloop = 0
  46. for iloop, i in enumerate(layers):
  47. for jloop, j in enumerate(layers):
  48. if i != j and iloop <= jloop:
  49. color = colors[0]
  50. colors = colors[1:]
  51. colors.append(color)
  52. gcore.write_command('d.text', color=color, size=4, line=line,
  53. stdin="%s %s" % (i, j))
  54. line += 1
  55. ofile = file(tmpfile, 'w')
  56. gcore.run_command('r.stats', flags='cnA', input=(i, j),
  57. stdout=ofile)
  58. ofile.close()
  59. ifile = file(tmpfile, 'r')
  60. first = True
  61. for l in ifile:
  62. f = l.rstrip('\r\n').split(' ')
  63. x = float(f[0])
  64. y = float(f[1])
  65. if first:
  66. minx = maxx = x
  67. miny = maxy = y
  68. first = False
  69. if minx > x:
  70. minx = x
  71. if maxx < x:
  72. maxx = x
  73. if miny > y:
  74. miny = y
  75. if maxy < y:
  76. maxy = y
  77. ifile.close()
  78. kx = 100.0 / (maxx - minx + 1)
  79. ky = 100.0 / (maxy - miny + 1)
  80. p = gcore.feed_command('d.graph', color=color)
  81. ofile = p.stdin
  82. ifile = file(tmpfile, 'r')
  83. for l in ifile:
  84. f = l.rstrip('\r\n').split(' ')
  85. x = float(f[0])
  86. y = float(f[1])
  87. ofile.write("icon + 0.1 %f %f\n" % ((x - minx + 1) * kx,
  88. (y - miny + 1) * ky))
  89. ifile.close()
  90. ofile.close()
  91. p.wait()
  92. try_remove(tmpfile)
  93. if __name__ == "__main__":
  94. options, flags = gcore.parser()
  95. main()