main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """!
  2. @package mapdisp.main
  3. @brief Start Map Display as standalone application
  4. Classes:
  5. - mapdisp::MapApp
  6. Usage:
  7. python mapdisp/main.py monitor-identifier /path/to/map/file /path/to/command/file /path/to/env/file
  8. (C) 2006-2011 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Michael Barton
  12. @author Jachym Cepicky
  13. @author Martin Landa <landa.martin gmail.com>
  14. @author Vaclav Petras <wenzeslaus gmail.com> (MapFrameBase)
  15. @author Anna Kratochvilova <kratochanna gmail.com> (MapFrameBase)
  16. """
  17. import os
  18. import sys
  19. import wx
  20. sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython'))
  21. from core import globalvar
  22. from core.gcmd import RunCommand
  23. from core.render import Map
  24. from mapdisp.frame import MapFrame
  25. from grass.script import core as grass
  26. # for standalone app
  27. monFile = { 'cmd' : None,
  28. 'map' : None,
  29. 'env' : None,
  30. }
  31. monName = None
  32. monSize = list(globalvar.MAP_WINDOW_SIZE)
  33. class MapApp(wx.App):
  34. def OnInit(self):
  35. wx.InitAllImageHandlers()
  36. if __name__ == "__main__":
  37. self.cmdTimeStamp = os.path.getmtime(monFile['cmd'])
  38. self.Map = Map(cmdfile = monFile['cmd'], mapfile = monFile['map'],
  39. envfile = monFile['env'], monitor = monName)
  40. else:
  41. self.Map = None
  42. self.mapFrm = MapFrame(parent = None, id = wx.ID_ANY, Map = self.Map,
  43. size = monSize)
  44. # self.SetTopWindow(Map)
  45. self.mapFrm.Show()
  46. if __name__ == "__main__":
  47. self.timer = wx.PyTimer(self.watcher)
  48. #check each 0.5s
  49. global mtime
  50. mtime = 500
  51. self.timer.Start(mtime)
  52. return True
  53. def OnExit(self):
  54. if __name__ == "__main__":
  55. # stop the timer
  56. # self.timer.Stop()
  57. # terminate thread
  58. for f in monFile.itervalues():
  59. grass.try_remove(f)
  60. def watcher(self):
  61. """!Redraw, if new layer appears (check's timestamp of
  62. cmdfile)
  63. """
  64. # todo: events
  65. if os.path.getmtime(monFile['cmd']) > self.cmdTimeStamp:
  66. self.timer.Stop()
  67. self.cmdTimeStamp = os.path.getmtime(monFile['cmd'])
  68. self.mapFrm.OnDraw(None)
  69. self.mapFrm.GetMap().GetLayersFromCmdFile()
  70. self.timer.Start(mtime)
  71. if __name__ == "__main__":
  72. # set command variable
  73. if len(sys.argv) < 5:
  74. print __doc__
  75. sys.exit(1)
  76. monName = sys.argv[1]
  77. monFile = { 'map' : sys.argv[2],
  78. 'cmd' : sys.argv[3],
  79. 'env' : sys.argv[4],
  80. }
  81. if len(sys.argv) >= 6:
  82. try:
  83. monSize[0] = int(sys.argv[5])
  84. except ValueError:
  85. pass
  86. if len(sys.argv) == 7:
  87. try:
  88. monSize[1] = int(sys.argv[6])
  89. except ValueError:
  90. pass
  91. import gettext
  92. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  93. grass.verbose(_("Starting map display <%s>...") % (monName))
  94. RunCommand('g.gisenv',
  95. set = 'MONITOR_%s_PID=%d' % (monName, os.getpid()))
  96. gmMap = MapApp(0)
  97. # set title
  98. gmMap.mapFrm.SetTitle(_("GRASS GIS Map Display: " +
  99. monName +
  100. " - Location: " + grass.gisenv()["LOCATION_NAME"]))
  101. gmMap.MainLoop()
  102. grass.verbose(_("Stopping map display <%s>...") % (monName))
  103. # clean up GRASS env variables
  104. env = grass.gisenv()
  105. env_name = 'MONITOR_%s' % monName
  106. for key in env.keys():
  107. if key.find(env_name) == 0:
  108. RunCommand('g.gisenv',
  109. set = '%s=' % key)
  110. if key == 'MONITOR' and env[key] == monName:
  111. RunCommand('g.gisenv',
  112. set = '%s=' % key)
  113. sys.exit(0)