d.correlate.py 2.8 KB

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