d.correlate.py 2.8 KB

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