test_mapdisp.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: Map window and mapdisplay test module
  5. # AUTHOR(S): Vaclav Petras
  6. # PURPOSE: Test functionality using small GUI applications.
  7. # COPYRIGHT: (C) 2013 by Vaclav Petras, and the GRASS Development Team
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. ############################################################################
  20. #%module
  21. #% description: Georectifies a map and allows to manage Ground Control Points.
  22. #% keywords: general
  23. #% keywords: GUI
  24. #% keywords: georectification
  25. #%end
  26. #%option
  27. #% key: test
  28. #% description: Test to run
  29. #% options: mapwindow,mapdisplay,apitest,distance,profile
  30. #% descriptions: mapwindow;Opens map window ;mapdisplay;Opens map display; apitest;Open an application to test API of map window; distance;Starts map window with distance measurement activated; profile;Starts map window with profile tool activated
  31. #% required: yes
  32. #%end
  33. #%option G_OPT_R_INPUT
  34. #% key: raster
  35. #% multiple: yes
  36. #% required: no
  37. #%end
  38. #%option G_OPT_V_INPUT
  39. #% key: vector
  40. #% multiple: yes
  41. #% required: no
  42. #%end
  43. """
  44. Module to run test map window (BufferedWidnow) and map display (MapFrame).
  45. @author Vaclav Petras <wenzeslaus gmail.com>
  46. """
  47. import os
  48. import sys
  49. import wx
  50. import grass.script as grass
  51. # adding a path to wxGUI modules
  52. if __name__ == '__main__':
  53. WXGUIBASE = os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython')
  54. if WXGUIBASE not in sys.path:
  55. sys.path.append(WXGUIBASE)
  56. from core.utils import _
  57. from core.settings import UserSettings
  58. from core.globalvar import CheckWxVersion
  59. from core.giface import StandaloneGrassInterface
  60. from mapwin.base import MapWindowProperties
  61. from mapwin.buffered import BufferedMapWindow
  62. from core.render import Map
  63. from rlisetup.sampling_frame import RLiSetupMapPanel
  64. from mapdisp.main import LayerList
  65. class MapdispGrassInterface(StandaloneGrassInterface):
  66. """!@implements GrassInterface"""
  67. def __init__(self, map_):
  68. StandaloneGrassInterface.__init__(self)
  69. self._map = map_
  70. self.mapWindow = None
  71. def GetLayerList(self):
  72. return LayerList(self._map, giface=self)
  73. def GetMapWindow(self):
  74. return self.mapWindow
  75. # this is a copy of method from some frame class
  76. def copyOfInitMap(map_, width, height):
  77. """!Initialize map display, set dimensions and map region
  78. """
  79. if not grass.find_program('g.region', '--help'):
  80. sys.exit(_("GRASS module '%s' not found. Unable to start map "
  81. "display window.") % 'g.region')
  82. map_.ChangeMapSize((width, height))
  83. map_.region = map_.GetRegion() # g.region -upgc
  84. # self.Map.SetRegion() # adjust region to match display window
  85. class TextShower(object):
  86. def __init__(self, parent, title):
  87. self._cf = wx.Frame(parent=parent, title=title)
  88. self._cp = wx.Panel(parent=self._cf, id=wx.ID_ANY)
  89. self._cs = wx.BoxSizer(wx.VERTICAL)
  90. self._cl = wx.StaticText(parent=self._cp, id=wx.ID_ANY, label="No text set yet")
  91. self._cs.Add(item=self._cl, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  92. self._cp.SetSizer(self._cs)
  93. self._cp.Layout()
  94. self._cf.Show()
  95. def SetLabel(self, text):
  96. self._cl.SetLabel(text)
  97. class Tester(object):
  98. def _listenToAllMapWindowSignals(self, window):
  99. output = sys.stderr
  100. # will make bad thigs after it is closed but who cares
  101. coordinatesShower = TextShower(window, "Coordinates")
  102. window.zoomChanged.connect(lambda: output.write("zoomChanged\n"))
  103. window.zoomHistoryUnavailable.connect(lambda: output.write("zoomHistoryUnavailable\n"))
  104. window.zoomHistoryAvailable.connect(lambda: output.write("zoomHistoryAvailable\n"))
  105. window.mapQueried.connect(lambda: output.write("mapQueried\n"))
  106. window.mouseEntered.connect(lambda: output.write("mouseEntered\n"))
  107. window.mouseLeftUpPointer.connect(lambda: output.write("mouseLeftUpPointer\n"))
  108. window.mouseLeftUp.connect(lambda: output.write("mouseLeftUp\n"))
  109. window.mouseMoving.connect(lambda x, y: coordinatesShower.SetLabel("%s , %s" % (x, y)))
  110. window.mouseHandlerRegistered.connect(lambda: output.write("mouseHandlerRegistered\n"))
  111. window.mouseHandlerUnregistered.connect(lambda: output.write("mouseHandlerUnregistered\n"))
  112. def testMapWindow(self, giface, map_):
  113. self.frame = wx.Frame(parent=None, title=_("Map window test frame"))
  114. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  115. sizer = wx.BoxSizer(wx.VERTICAL)
  116. mapWindowProperties = MapWindowProperties()
  117. mapWindowProperties.setValuesFromUserSettings()
  118. width, height = self.frame.GetClientSize()
  119. copyOfInitMap(map_, width, height)
  120. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  121. properties=mapWindowProperties)
  122. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  123. panel.SetSizer(sizer)
  124. panel.Layout()
  125. self.frame.Show()
  126. def testMapDisplay(self, giface, map_):
  127. from mapdisp.frame import MapFrame
  128. # known issues (should be similar with d.mon):
  129. # * opening map in digitizer ends with: vdigit/toolbars.py:723: 'selection' referenced before assignment
  130. # * nviz start fails (closes window? segfaults?) after mapdisp/frame.py:306: 'NoneType' object has no attribute 'GetLayerNotebook'
  131. frame = MapFrame(parent=None, title=_("Map display test"),
  132. giface=giface, Map=map_)
  133. # this is questionable: how complete the giface when creating objects
  134. # which are in giface
  135. giface.mapWindow = frame.GetMapWindow()
  136. frame.GetMapWindow().ZoomToMap()
  137. frame.Show()
  138. def testMapWindowApi(self, giface, map_):
  139. self.frame = wx.Frame(parent=None, title=_("Map window API test frame"))
  140. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  141. sizer = wx.BoxSizer(wx.VERTICAL)
  142. mapWindowProperties = MapWindowProperties()
  143. mapWindowProperties.setValuesFromUserSettings()
  144. mapWindowProperties.showRegion = True
  145. width, height = self.frame.GetClientSize()
  146. copyOfInitMap(map_, width, height)
  147. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  148. properties=mapWindowProperties)
  149. giface.mapWindow = window
  150. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  151. panel.SetSizer(sizer)
  152. panel.Layout()
  153. window.ZoomToWind()
  154. from mapdisp.frame import MeasureController
  155. self.measureController = MeasureController(giface)
  156. self.measureController.StartMeasurement()
  157. self._listenToAllMapWindowSignals(window)
  158. self.frame.Show()
  159. def testMapWindowDistance(self, giface, map_):
  160. self.frame = wx.Frame(parent=None,
  161. title=_("Map window distance measurement test frame"))
  162. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  163. sizer = wx.BoxSizer(wx.VERTICAL)
  164. mapWindowProperties = MapWindowProperties()
  165. mapWindowProperties.setValuesFromUserSettings()
  166. mapWindowProperties.showRegion = True
  167. width, height = self.frame.GetClientSize()
  168. copyOfInitMap(map_, width, height)
  169. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  170. properties=mapWindowProperties)
  171. giface.mapWindow = window
  172. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  173. panel.SetSizer(sizer)
  174. panel.Layout()
  175. window.ZoomToWind()
  176. self._listenToAllMapWindowSignals(window)
  177. self.frame.Show()
  178. from mapwin.analysis import MeasureDistanceController
  179. self.controller = MeasureDistanceController(giface, window)
  180. self.controller.Start()
  181. def testMapWindowProfile(self, giface, map_):
  182. self.frame = wx.Frame(parent=None,
  183. title=_("Map window profile tool test frame"))
  184. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  185. sizer = wx.BoxSizer(wx.VERTICAL)
  186. mapWindowProperties = MapWindowProperties()
  187. mapWindowProperties.setValuesFromUserSettings()
  188. mapWindowProperties.showRegion = True
  189. width, height = self.frame.GetClientSize()
  190. copyOfInitMap(map_, width, height)
  191. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  192. properties=mapWindowProperties)
  193. giface.mapWindow = window
  194. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  195. panel.SetSizer(sizer)
  196. panel.Layout()
  197. window.ZoomToWind()
  198. self._listenToAllMapWindowSignals(window)
  199. self.frame.Show()
  200. from mapwin.analysis import ProfileController
  201. self.controller = ProfileController(giface, window)
  202. self.controller.Start()
  203. rasters = []
  204. for layer in giface.GetLayerList().GetSelectedLayers():
  205. if layer.maplayer.GetType() == 'raster':
  206. rasters.append(layer.maplayer.GetName())
  207. from wxplot.profile import ProfileFrame
  208. profileWindow = ProfileFrame(parent=self.frame,
  209. controller=self.controller,
  210. units=map_.projinfo['units'],
  211. rasterList=rasters)
  212. profileWindow.CentreOnParent()
  213. profileWindow.Show()
  214. # Open raster select dialog to make sure that a raster (and
  215. # the desired raster) is selected to be profiled
  216. profileWindow.OnSelectRaster(None)
  217. def testMapWindowRlisetup(self, map_):
  218. self.frame = wx.Frame(parent=None,
  219. title=_("Map window rlisetup test frame"))
  220. RLiSetupMapPanel(parent=self.frame, map_=map_)
  221. self.frame.Show()
  222. def main():
  223. """!Sets the GRASS display driver
  224. """
  225. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  226. if driver == 'png':
  227. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  228. else:
  229. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  230. # TODO: message format should not be GUI
  231. # TODO: should messages here be translatable?
  232. # (for test its great, for translator not)
  233. options, flags = grass.parser()
  234. test = options['test']
  235. app = wx.PySimpleApp()
  236. if not CheckWxVersion([2, 9]):
  237. wx.InitAllImageHandlers()
  238. map_ = Map()
  239. if options['raster']:
  240. names = options['raster']
  241. for name in names.split(','):
  242. cmdlist = ['d.rast', 'map=%s' % name]
  243. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  244. name=name, hidden=False, opacity=1.0,
  245. render=True)
  246. if options['vector']:
  247. names = options['vector']
  248. for name in names.split(','):
  249. cmdlist = ['d.vect', 'map=%s' % name]
  250. map_.AddLayer(ltype='vector', command=cmdlist, active=True,
  251. name=name, hidden=False, opacity=1.0,
  252. render=True)
  253. giface = MapdispGrassInterface(map_=map_)
  254. tester = Tester()
  255. if test == 'mapwindow':
  256. tester.testMapWindow(giface, map_)
  257. elif test == 'mapdisplay':
  258. tester.testMapDisplay(giface, map_)
  259. elif test == 'apitest':
  260. tester.testMapWindowApi(giface, map_)
  261. elif test == 'distance':
  262. tester.testMapWindowDistance(giface, map_)
  263. elif test == 'profile':
  264. tester.testMapWindowProfile(giface, map_)
  265. elif test == 'rlisetup':
  266. tester.testMapWindowRlisetup(map_)
  267. else:
  268. # TODO: this should not happen but happens
  269. import grass.script as sgrass
  270. sgrass.fatal(_("Unknown value %s of test parameter." % test))
  271. app.MainLoop()
  272. if __name__ == '__main__':
  273. main()