test_mapdisp.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. from core.utils import _
  52. from core.settings import UserSettings
  53. from core.globalvar import CheckWxVersion
  54. from core.giface import StandaloneGrassInterface
  55. from mapwin.base import MapWindowProperties
  56. from mapwin.buffered import BufferedMapWindow
  57. from core.render import Map
  58. from rlisetup.sampling_frame import RLiSetupMapPanel
  59. from mapdisp.main import LayerList
  60. class MapdispGrassInterface(StandaloneGrassInterface):
  61. """@implements GrassInterface"""
  62. def __init__(self, map_):
  63. StandaloneGrassInterface.__init__(self)
  64. self._map = map_
  65. self.mapWindow = None
  66. def GetLayerList(self):
  67. return LayerList(self._map, giface=self)
  68. def GetMapWindow(self):
  69. return self.mapWindow
  70. # this is a copy of method from some frame class
  71. def copyOfInitMap(map_, width, height):
  72. """Initialize map display, set dimensions and map region
  73. """
  74. if not grass.find_program('g.region', '--help'):
  75. sys.exit(_("GRASS module '%s' not found. Unable to start map "
  76. "display window.") % 'g.region')
  77. map_.ChangeMapSize((width, height))
  78. map_.region = map_.GetRegion() # g.region -upgc
  79. # self.Map.SetRegion() # adjust region to match display window
  80. class TextShower(object):
  81. def __init__(self, parent, title):
  82. self._cf = wx.Frame(parent=parent, title=title)
  83. self._cp = wx.Panel(parent=self._cf, id=wx.ID_ANY)
  84. self._cs = wx.BoxSizer(wx.VERTICAL)
  85. self._cl = wx.StaticText(parent=self._cp, id=wx.ID_ANY, label="No text set yet")
  86. self._cs.Add(item=self._cl, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  87. self._cp.SetSizer(self._cs)
  88. self._cp.Layout()
  89. self._cf.Show()
  90. def SetLabel(self, text):
  91. self._cl.SetLabel(text)
  92. class Tester(object):
  93. def _listenToAllMapWindowSignals(self, window):
  94. output = sys.stderr
  95. # will make bad thigs after it is closed but who cares
  96. coordinatesShower = TextShower(window, "Coordinates")
  97. window.zoomChanged.connect(lambda: output.write("zoomChanged\n"))
  98. window.zoomHistoryUnavailable.connect(lambda: output.write("zoomHistoryUnavailable\n"))
  99. window.zoomHistoryAvailable.connect(lambda: output.write("zoomHistoryAvailable\n"))
  100. window.mapQueried.connect(lambda: output.write("mapQueried\n"))
  101. window.mouseEntered.connect(lambda: output.write("mouseEntered\n"))
  102. window.mouseLeftUpPointer.connect(lambda: output.write("mouseLeftUpPointer\n"))
  103. window.mouseLeftUp.connect(lambda: output.write("mouseLeftUp\n"))
  104. window.mouseMoving.connect(lambda x, y: coordinatesShower.SetLabel("%s , %s" % (x, y)))
  105. window.mouseHandlerRegistered.connect(lambda: output.write("mouseHandlerRegistered\n"))
  106. window.mouseHandlerUnregistered.connect(lambda: output.write("mouseHandlerUnregistered\n"))
  107. def testMapWindow(self, giface, map_):
  108. self.frame = wx.Frame(parent=None, title=_("Map window test frame"))
  109. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  110. sizer = wx.BoxSizer(wx.VERTICAL)
  111. mapWindowProperties = MapWindowProperties()
  112. mapWindowProperties.setValuesFromUserSettings()
  113. width, height = self.frame.GetClientSize()
  114. copyOfInitMap(map_, width, height)
  115. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  116. properties=mapWindowProperties)
  117. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  118. panel.SetSizer(sizer)
  119. panel.Layout()
  120. self.frame.Show()
  121. def testMapDisplay(self, giface, map_):
  122. from mapdisp.frame import MapFrame
  123. # known issues (should be similar with d.mon):
  124. # * opening map in digitizer ends with: vdigit/toolbars.py:723: 'selection' referenced before assignment
  125. # * nviz start fails (closes window? segfaults?) after mapdisp/frame.py:306: 'NoneType' object has no attribute 'GetLayerNotebook'
  126. frame = MapFrame(parent=None, title=_("Map display test"),
  127. giface=giface, Map=map_)
  128. # this is questionable: how complete the giface when creating objects
  129. # which are in giface
  130. giface.mapWindow = frame.GetMapWindow()
  131. frame.GetMapWindow().ZoomToMap()
  132. frame.Show()
  133. def testMapWindowApi(self, giface, map_):
  134. self.frame = wx.Frame(parent=None, title=_("Map window API test frame"))
  135. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  136. sizer = wx.BoxSizer(wx.VERTICAL)
  137. mapWindowProperties = MapWindowProperties()
  138. mapWindowProperties.setValuesFromUserSettings()
  139. mapWindowProperties.showRegion = True
  140. width, height = self.frame.GetClientSize()
  141. copyOfInitMap(map_, width, height)
  142. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  143. properties=mapWindowProperties)
  144. giface.mapWindow = window
  145. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  146. panel.SetSizer(sizer)
  147. panel.Layout()
  148. window.ZoomToWind()
  149. from mapdisp.frame import MeasureController
  150. self.measureController = MeasureController(giface)
  151. self.measureController.StartMeasurement()
  152. self._listenToAllMapWindowSignals(window)
  153. self.frame.Show()
  154. def testMapWindowDistance(self, giface, map_):
  155. self.frame = wx.Frame(parent=None,
  156. title=_("Map window distance measurement test frame"))
  157. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  158. sizer = wx.BoxSizer(wx.VERTICAL)
  159. mapWindowProperties = MapWindowProperties()
  160. mapWindowProperties.setValuesFromUserSettings()
  161. mapWindowProperties.showRegion = True
  162. width, height = self.frame.GetClientSize()
  163. copyOfInitMap(map_, width, height)
  164. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  165. properties=mapWindowProperties)
  166. giface.mapWindow = window
  167. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  168. panel.SetSizer(sizer)
  169. panel.Layout()
  170. window.ZoomToWind()
  171. self._listenToAllMapWindowSignals(window)
  172. self.frame.Show()
  173. from mapwin.analysis import MeasureDistanceController
  174. self.controller = MeasureDistanceController(giface, window)
  175. self.controller.Start()
  176. def testMapWindowProfile(self, giface, map_):
  177. self.frame = wx.Frame(parent=None,
  178. title=_("Map window profile tool test frame"))
  179. panel = wx.Panel(parent=self.frame, id=wx.ID_ANY)
  180. sizer = wx.BoxSizer(wx.VERTICAL)
  181. mapWindowProperties = MapWindowProperties()
  182. mapWindowProperties.setValuesFromUserSettings()
  183. mapWindowProperties.showRegion = True
  184. width, height = self.frame.GetClientSize()
  185. copyOfInitMap(map_, width, height)
  186. window = BufferedMapWindow(parent=panel, giface=giface, Map=map_,
  187. properties=mapWindowProperties)
  188. giface.mapWindow = window
  189. sizer.Add(item=window, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  190. panel.SetSizer(sizer)
  191. panel.Layout()
  192. window.ZoomToWind()
  193. self._listenToAllMapWindowSignals(window)
  194. self.frame.Show()
  195. from mapwin.analysis import ProfileController
  196. self.controller = ProfileController(giface, window)
  197. self.controller.Start()
  198. rasters = []
  199. for layer in giface.GetLayerList().GetSelectedLayers():
  200. if layer.maplayer.GetType() == 'raster':
  201. rasters.append(layer.maplayer.GetName())
  202. from wxplot.profile import ProfileFrame
  203. profileWindow = ProfileFrame(parent=self.frame,
  204. controller=self.controller,
  205. units=map_.projinfo['units'],
  206. rasterList=rasters)
  207. profileWindow.CentreOnParent()
  208. profileWindow.Show()
  209. # Open raster select dialog to make sure that a raster (and
  210. # the desired raster) is selected to be profiled
  211. profileWindow.OnSelectRaster(None)
  212. def testMapWindowRlisetup(self, map_):
  213. self.frame = wx.Frame(parent=None,
  214. title=_("Map window rlisetup test frame"))
  215. RLiSetupMapPanel(parent=self.frame, map_=map_)
  216. self.frame.Show()
  217. def main():
  218. """Sets the GRASS display driver
  219. """
  220. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  221. if driver == 'png':
  222. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  223. else:
  224. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  225. # TODO: message format should not be GUI
  226. # TODO: should messages here be translatable?
  227. # (for test its great, for translator not)
  228. options, flags = grass.parser()
  229. test = options['test']
  230. app = wx.App()
  231. if not CheckWxVersion([2, 9]):
  232. wx.InitAllImageHandlers()
  233. map_ = Map()
  234. if options['raster']:
  235. names = options['raster']
  236. for name in names.split(','):
  237. cmdlist = ['d.rast', 'map=%s' % name]
  238. map_.AddLayer(ltype='raster', command=cmdlist, active=True,
  239. name=name, hidden=False, opacity=1.0,
  240. render=True)
  241. if options['vector']:
  242. names = options['vector']
  243. for name in names.split(','):
  244. cmdlist = ['d.vect', 'map=%s' % name]
  245. map_.AddLayer(ltype='vector', command=cmdlist, active=True,
  246. name=name, hidden=False, opacity=1.0,
  247. render=True)
  248. giface = MapdispGrassInterface(map_=map_)
  249. tester = Tester()
  250. if test == 'mapwindow':
  251. tester.testMapWindow(giface, map_)
  252. elif test == 'mapdisplay':
  253. tester.testMapDisplay(giface, map_)
  254. elif test == 'apitest':
  255. tester.testMapWindowApi(giface, map_)
  256. elif test == 'distance':
  257. tester.testMapWindowDistance(giface, map_)
  258. elif test == 'profile':
  259. tester.testMapWindowProfile(giface, map_)
  260. elif test == 'rlisetup':
  261. tester.testMapWindowRlisetup(map_)
  262. else:
  263. # TODO: this should not happen but happens
  264. import grass.script as sgrass
  265. sgrass.fatal(_("Unknown value %s of test parameter." % test))
  266. app.MainLoop()
  267. if __name__ == '__main__':
  268. main()