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 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. # TODO GRASS 7: rename parameters to map1, map2 or better multiple map=map1[,map2[...]]
  17. #############################################################################
  18. #%Module
  19. #% description: Prints a graph of the correlation between data layers (in pairs).
  20. #% keywords: display, diagram
  21. #%End
  22. #%option
  23. #% key: layers
  24. #% type: string
  25. #% gisprompt: old,cell,raster
  26. #% description: raster input map
  27. #% required: yes
  28. #% multiple: yes
  29. #%end
  30. import sys
  31. import os
  32. import grass
  33. def main():
  34. layers = options['layers'].split(',')
  35. if len(layers) < 2:
  36. grass.error("At least 2 layers are required")
  37. tmpfile = grass.tempfile()
  38. for map in layers:
  39. if not grass.find_file(map, element = 'cell')['file']:
  40. grass.fatal("Input map <%s> not found" % map)
  41. grass.write_command('d.text', color = 'black', size = 4, line = 1, stdin = "CORRELATION")
  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. grass.write_command('d.text', color = color, size = 4, line = line, stdin = "%s %s" % (i, j))
  53. line += 1
  54. ofile = file(tmpfile, 'w')
  55. grass.run_command('r.stats', flags = 'cnA', input = (i, j), stdout = ofile)
  56. ofile.close()
  57. ifile = file(tmpfile, 'r')
  58. first = True
  59. for l in ifile:
  60. f = l.rstrip('\r\n').split(' ')
  61. x = float(f[0])
  62. y = float(f[1])
  63. if first:
  64. minx = maxx = x
  65. miny = maxy = y
  66. first = False
  67. if minx > x: minx = x
  68. if maxx < x: maxx = x
  69. if miny > y: miny = y
  70. if maxy < y: maxy = y
  71. ifile.close()
  72. kx = 100.0/(maxx-minx+1)
  73. ky = 100.0/(maxy-miny+1)
  74. p = grass.feed_command('d.graph', color = color)
  75. ofile = p.stdin
  76. ifile = file(tmpfile, 'r')
  77. for l in ifile:
  78. f = l.rstrip('\r\n').split(' ')
  79. x = float(f[0])
  80. y = float(f[1])
  81. ofile.write("icon + 0.1 %f %f\n" % ((x-minx+1) * kx, (y-miny+1) * ky))
  82. ifile.close()
  83. ofile.close()
  84. p.wait()
  85. grass.try_remove(tmpfile)
  86. if __name__ == "__main__":
  87. options, flags = grass.parser()
  88. main()