d.correlate.py 2.8 KB

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