d.correlate.py 3.7 KB

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