test_mapdisp.py 12 KB

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