test_mapdisp.py 12 KB

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