main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if __name__ == "__main__":
  20. sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython'))
  21. from core import globalvar
  22. import wx
  23. from core.gcmd import RunCommand
  24. from core.render import Map
  25. from mapdisp.frame import MapFrame
  26. from grass.script import core as grass
  27. # for standalone app
  28. monFile = { 'cmd' : None,
  29. 'map' : None,
  30. 'env' : None,
  31. }
  32. monName = None
  33. monSize = list(globalvar.MAP_WINDOW_SIZE)
  34. class MapApp(wx.App):
  35. def OnInit(self):
  36. wx.InitAllImageHandlers()
  37. if __name__ == "__main__":
  38. self.cmdTimeStamp = os.path.getmtime(monFile['cmd'])
  39. self.Map = Map(cmdfile = monFile['cmd'], mapfile = monFile['map'],
  40. envfile = monFile['env'], monitor = monName)
  41. else:
  42. self.Map = None
  43. self.mapFrm = MapFrame(parent = None, id = wx.ID_ANY, Map = self.Map,
  44. size = monSize)
  45. # self.SetTopWindow(Map)
  46. self.mapFrm.Show()
  47. if __name__ == "__main__":
  48. self.timer = wx.PyTimer(self.watcher)
  49. #check each 0.5s
  50. global mtime
  51. mtime = 500
  52. self.timer.Start(mtime)
  53. return True
  54. def OnExit(self):
  55. if __name__ == "__main__":
  56. # stop the timer
  57. # self.timer.Stop()
  58. # terminate thread
  59. for f in monFile.itervalues():
  60. grass.try_remove(f)
  61. def watcher(self):
  62. """!Redraw, if new layer appears (check's timestamp of
  63. cmdfile)
  64. """
  65. # todo: events
  66. if os.path.getmtime(monFile['cmd']) > self.cmdTimeStamp:
  67. self.timer.Stop()
  68. self.cmdTimeStamp = os.path.getmtime(monFile['cmd'])
  69. self.mapFrm.OnDraw(None)
  70. self.mapFrm.GetMap().GetLayersFromCmdFile()
  71. self.timer.Start(mtime)
  72. if __name__ == "__main__":
  73. # set command variable
  74. if len(sys.argv) < 5:
  75. print __doc__
  76. sys.exit(1)
  77. monName = sys.argv[1]
  78. monFile = { 'map' : sys.argv[2],
  79. 'cmd' : sys.argv[3],
  80. 'env' : sys.argv[4],
  81. }
  82. if len(sys.argv) >= 6:
  83. try:
  84. monSize[0] = int(sys.argv[5])
  85. except ValueError:
  86. pass
  87. if len(sys.argv) == 7:
  88. try:
  89. monSize[1] = int(sys.argv[6])
  90. except ValueError:
  91. pass
  92. import gettext
  93. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  94. grass.verbose(_("Starting map display <%s>...") % (monName))
  95. RunCommand('g.gisenv',
  96. set = 'MONITOR_%s_PID=%d' % (monName, os.getpid()))
  97. gmMap = MapApp(0)
  98. # set title
  99. gmMap.mapFrm.SetTitle(_("GRASS GIS Map Display: " +
  100. monName +
  101. " - Location: " + grass.gisenv()["LOCATION_NAME"]))
  102. gmMap.MainLoop()
  103. grass.verbose(_("Stopping map display <%s>...") % (monName))
  104. # clean up GRASS env variables
  105. env = grass.gisenv()
  106. env_name = 'MONITOR_%s' % monName
  107. for key in env.keys():
  108. if key.find(env_name) == 0:
  109. RunCommand('g.gisenv',
  110. set = '%s=' % key)
  111. if key == 'MONITOR' and env[key] == monName:
  112. RunCommand('g.gisenv',
  113. set = '%s=' % key)
  114. sys.exit(0)