test_mapdisp.py 12 KB

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