render_cmd.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. import tempfile
  5. from grass.script import core as grass
  6. from grass.script import task as gtask
  7. # read environment variables from file
  8. def read_env_file(env_file):
  9. width = height = None
  10. fd = open(env_file, 'r')
  11. if fd is None:
  12. grass.fatal("Unable to open file '{}'".format(env_file))
  13. lines = fd.readlines()
  14. for l in lines:
  15. if l.startswith('#'):
  16. continue
  17. k, v = l.rstrip('\n').split('#', 1)[0].strip().split('=', 1)
  18. os.environ[k] = v
  19. if width is None and k == 'GRASS_RENDER_WIDTH':
  20. width = int(v)
  21. if height is None and k == 'GRASS_RENDER_HEIGHT':
  22. height = int(v)
  23. fd.close()
  24. if width is None or height is None:
  25. grass.fatal("Unknown monitor size")
  26. return width, height
  27. # run display command
  28. def render(cmd, mapfile):
  29. env = os.environ.copy()
  30. if mapfile:
  31. env['GRASS_RENDER_FILE'] = mapfile
  32. try:
  33. grass.run_command(cmd[0], env=env, **cmd[1])
  34. except Exception as e:
  35. grass.fatal("Unable to render: {}".format(e))
  36. # update cmd file
  37. def update_cmd_file(cmd_file, cmd, mapfile):
  38. if cmd[0] in ('d.colorlist', 'd.font', 'd.fontlist',
  39. 'd.frame', 'd.info', 'd.mon', 'd.out.file',
  40. 'd.redraw', 'd.to.rast', 'd.what.rast',
  41. 'd.what.vect', 'd.where'):
  42. return
  43. mode = 'w' if cmd[0] == 'd.erase' else 'a'
  44. # update cmd file
  45. fd = open(cmd_file, mode)
  46. if fd is None:
  47. grass.fatal("Unable to open file '{}'".format(cmd_file))
  48. if mode == 'a':
  49. frame = os.getenv('GRASS_RENDER_FRAME', None)
  50. if frame:
  51. fd.write('# GRASS_RENDER_FRAME={}\n'.format(frame))
  52. if mapfile:
  53. fd.write('# GRASS_RENDER_FILE={}\n'.format(mapfile))
  54. fd.write(' '.join(gtask.cmdtuple_to_list(cmd)))
  55. fd.write('\n')
  56. else:
  57. fd.write('')
  58. fd.close()
  59. # adjust region
  60. def adjust_region(width, height):
  61. region = grass.region()
  62. mapwidth = abs(region["e"] - region["w"])
  63. mapheight = abs(region['n'] - region['s'])
  64. region["nsres"] = mapheight / height
  65. region["ewres"] = mapwidth / width
  66. region['rows'] = int(round(mapheight / region["nsres"]))
  67. region['cols'] = int(round(mapwidth / region["ewres"]))
  68. region['cells'] = region['rows'] * region['cols']
  69. kwdata = [('proj', 'projection'),
  70. ('zone', 'zone'),
  71. ('north', 'n'),
  72. ('south', 's'),
  73. ('east', 'e'),
  74. ('west', 'w'),
  75. ('cols', 'cols'),
  76. ('rows', 'rows'),
  77. ('e-w resol', 'ewres'),
  78. ('n-s resol', 'nsres')]
  79. grass_region = ''
  80. for wkey, rkey in kwdata:
  81. grass_region += '%s: %s;' % (wkey, region[rkey])
  82. os.environ['GRASS_REGION'] = grass_region
  83. if __name__ == "__main__":
  84. cmd = gtask.cmdstring_to_tuple(sys.argv[1])
  85. if not cmd[0] or cmd[0] == 'd.mon':
  86. sys.exit(0)
  87. path = os.path.dirname(os.path.abspath(__file__))
  88. mon = os.path.split(path)[-1]
  89. width, height = read_env_file(os.path.join(path, 'env'))
  90. if mon.startswith('wx'):
  91. mapfile = tempfile.NamedTemporaryFile(dir=path).name
  92. if cmd[0] in ('d.barscale', 'd.legend', 'd.northarrow'):
  93. mapfile += '.png'
  94. else:
  95. mapfile += '.ppm'
  96. else:
  97. mapfile = None
  98. adjust_region(width, height)
  99. render(cmd, mapfile)
  100. update_cmd_file(os.path.join(path, 'cmd'), cmd, mapfile)
  101. sys.exit(0)