mapdisplay.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. """
  2. @package gcp.mapdisplay
  3. @brief Display to manage ground control points with two toolbars, one
  4. for various display management functions, one for manipulating GCPs.
  5. Classes:
  6. - mapdisplay::MapFrame
  7. (C) 2006-2011 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Markus Metz
  11. """
  12. import os
  13. import math
  14. import platform
  15. from core import globalvar
  16. import wx
  17. import wx.aui
  18. from mapdisp.toolbars import MapToolbar
  19. from gcp.toolbars import GCPDisplayToolbar, GCPManToolbar
  20. from mapdisp.gprint import PrintOptions
  21. from core.gcmd import GMessage
  22. from core.utils import _
  23. from gui_core.dialogs import GetImageHandlers, ImageSizeDialog
  24. from gui_core.mapdisp import SingleMapFrame
  25. from core.settings import UserSettings
  26. from mapwin.buffered import BufferedMapWindow
  27. from mapwin.base import MapWindowProperties
  28. import mapdisp.statusbar as sb
  29. import gcp.statusbar as sbgcp
  30. # for standalone app
  31. cmdfilename = None
  32. class MapFrame(SingleMapFrame):
  33. """Main frame for map display window. Drawing takes place in
  34. child double buffered drawing window.
  35. """
  36. def __init__(self, parent, giface,
  37. title=_("GRASS GIS Manage Ground Control Points"),
  38. toolbars=["gcpdisp"], Map=None, auimgr=None,
  39. name='GCPMapWindow', **kwargs):
  40. """Main map display window with toolbars, statusbar and
  41. DrawWindow
  42. :param giface: GRASS interface instance
  43. :param title: window title
  44. :param toolbars: array of activated toolbars, e.g. ['map', 'digit']
  45. :param map: instance of render.Map
  46. :param auimgs: AUI manager
  47. :param kwargs: wx.Frame attribures
  48. """
  49. SingleMapFrame.__init__(self, parent = parent, giface = giface, title = title,
  50. Map = Map, auimgr = auimgr, name = name, **kwargs)
  51. self._giface = giface
  52. # properties are shared in other objects, so defining here
  53. self.mapWindowProperties = MapWindowProperties()
  54. self.mapWindowProperties.setValuesFromUserSettings()
  55. self.mapWindowProperties.alignExtent = True
  56. #
  57. # Add toolbars
  58. #
  59. for toolb in toolbars:
  60. self.AddToolbar(toolb)
  61. self.activemap = self.toolbars['gcpdisp'].togglemap
  62. self.activemap.SetSelection(0)
  63. self.SrcMap = self.grwiz.SrcMap # instance of render.Map
  64. self.TgtMap = self.grwiz.TgtMap # instance of render.Map
  65. self._mgr.SetDockSizeConstraint(0.5, 0.5)
  66. #
  67. # Add statusbar
  68. #
  69. # items for choice
  70. self.statusbarItems = [sb.SbCoordinates,
  71. sb.SbRegionExtent,
  72. sb.SbCompRegionExtent,
  73. sb.SbShowRegion,
  74. sb.SbResolution,
  75. sb.SbDisplayGeometry,
  76. sb.SbMapScale,
  77. sb.SbProjection,
  78. sbgcp.SbGoToGCP,
  79. sbgcp.SbRMSError]
  80. # create statusbar and its manager
  81. statusbar = self.CreateStatusBar(number = 4, style = 0)
  82. statusbar.SetStatusWidths([-5, -2, -1, -1])
  83. self.statusbarManager = sb.SbManager(mapframe = self, statusbar = statusbar)
  84. # fill statusbar manager
  85. self.statusbarManager.AddStatusbarItemsByClass(self.statusbarItems, mapframe = self, statusbar = statusbar)
  86. self.statusbarManager.AddStatusbarItem(sb.SbMask(self, statusbar = statusbar, position = 2))
  87. self.statusbarManager.AddStatusbarItem(sb.SbRender(self, statusbar = statusbar, position = 3))
  88. self.statusbarManager.SetMode(8) # goto GCP
  89. #
  90. # Init map display (buffered DC & set default cursor)
  91. #
  92. self.grwiz.SwitchEnv('source')
  93. self.SrcMapWindow = BufferedMapWindow(parent=self, giface=self._giface, id=wx.ID_ANY,
  94. properties=self.mapWindowProperties,
  95. Map=self.SrcMap)
  96. self.grwiz.SwitchEnv('target')
  97. self.TgtMapWindow = BufferedMapWindow(parent=self, giface=self._giface, id=wx.ID_ANY,
  98. properties=self.mapWindowProperties,
  99. Map=self.TgtMap)
  100. self.MapWindow = self.SrcMapWindow
  101. self.Map = self.SrcMap
  102. self._setUpMapWindow(self.SrcMapWindow)
  103. self._setUpMapWindow(self.TgtMapWindow)
  104. self.SrcMapWindow.SetNamedCursor('cross')
  105. self.TgtMapWindow.SetNamedCursor('cross')
  106. # used to switch current map (combo box in toolbar)
  107. self.SrcMapWindow.mouseEntered.connect(
  108. lambda:
  109. self._setActiveMapWindow(self.SrcMapWindow))
  110. self.TgtMapWindow.mouseEntered.connect(
  111. lambda:
  112. self._setActiveMapWindow(self.TgtMapWindow))
  113. #
  114. # initialize region values
  115. #
  116. self._initMap(Map = self.SrcMap)
  117. self._initMap(Map = self.TgtMap)
  118. self.GetMapToolbar().SelectDefault()
  119. #
  120. # Bind various events
  121. #
  122. self.activemap.Bind(wx.EVT_CHOICE, self.OnUpdateActive)
  123. self.Bind(wx.EVT_SIZE, self.OnSize)
  124. #
  125. # Update fancy gui style
  126. #
  127. # AuiManager wants a CentrePane, workaround to get two equally sized windows
  128. self.list = self.CreateGCPList()
  129. #self.SrcMapWindow.SetSize((300, 300))
  130. #self.TgtMapWindow.SetSize((300, 300))
  131. self.list.SetSize((100, 150))
  132. self._mgr.AddPane(self.list, wx.aui.AuiPaneInfo().
  133. Name("gcplist").Caption(_("GCP List")).LeftDockable(False).
  134. RightDockable(False).PinButton().FloatingSize((600,200)).
  135. CloseButton(False).DestroyOnClose(True).
  136. Top().Layer(1).MinSize((200,100)))
  137. self._mgr.AddPane(self.SrcMapWindow, wx.aui.AuiPaneInfo().
  138. Name("source").Caption(_("Source Display")).Dockable(False).
  139. CloseButton(False).DestroyOnClose(True).Floatable(False).
  140. Centre())
  141. self._mgr.AddPane(self.TgtMapWindow, wx.aui.AuiPaneInfo().
  142. Name("target").Caption(_("Target Display")).Dockable(False).
  143. CloseButton(False).DestroyOnClose(True).Floatable(False).
  144. Right().Layer(0))
  145. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  146. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  147. srcwidth = (srcwidth + tgtwidth) / 2
  148. self._mgr.GetPane("target").Hide()
  149. self._mgr.Update()
  150. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  151. self._mgr.GetPane("target").BestSize((srcwidth, srcheight))
  152. if self.show_target:
  153. self._mgr.GetPane("target").Show()
  154. else:
  155. self.activemap.Enable(False)
  156. # needed by Mac OS, does not harm on Linux, breaks display on Windows
  157. if platform.system() != 'Windows':
  158. self._mgr.Update()
  159. #
  160. # Init print module and classes
  161. #
  162. self.printopt = PrintOptions(self, self.MapWindow)
  163. #
  164. # Initialization of digitization tool
  165. #
  166. self.digit = None
  167. # set active map
  168. self.MapWindow = self.SrcMapWindow
  169. self.Map = self.SrcMap
  170. # do not init zoom history here, that happens when zooming to map(s)
  171. #
  172. # Re-use dialogs
  173. #
  174. self.dialogs = {}
  175. self.dialogs['attributes'] = None
  176. self.dialogs['category'] = None
  177. self.dialogs['barscale'] = None
  178. self.dialogs['legend'] = None
  179. self.decorationDialog = None # decoration/overlays
  180. # doing nice things in statusbar when other things are ready
  181. self.statusbarManager.Update()
  182. def _setUpMapWindow(self, mapWindow):
  183. # TODO: almost the smae implementation as for MapFrameBase (only names differ)
  184. # enable or disable zoom history tool
  185. mapWindow.zoomHistoryAvailable.connect(
  186. lambda:
  187. self.GetMapToolbar().Enable('zoomback', enable=True))
  188. mapWindow.zoomHistoryUnavailable.connect(
  189. lambda:
  190. self.GetMapToolbar().Enable('zoomback', enable=False))
  191. mapWindow.mouseMoving.connect(self.CoordinatesChanged)
  192. def AddToolbar(self, name):
  193. """Add defined toolbar to the window
  194. Currently known toolbars are:
  195. - 'map' - basic map toolbar
  196. - 'vdigit' - vector digitizer
  197. - 'gcpdisp' - GCP Manager, Display
  198. - 'gcpman' - GCP Manager, points management
  199. - 'nviz' - 3D view mode
  200. """
  201. # default toolbar
  202. if name == "map":
  203. self.toolbars['map'] = MapToolbar(self, self._toolSwitcher)
  204. self._mgr.AddPane(self.toolbars['map'],
  205. wx.aui.AuiPaneInfo().
  206. Name("maptoolbar").Caption(_("Map Toolbar")).
  207. ToolbarPane().Top().
  208. LeftDockable(False).RightDockable(False).
  209. BottomDockable(False).TopDockable(True).
  210. CloseButton(False).Layer(2).
  211. BestSize((self.toolbars['map'].GetSize())))
  212. # GCP display
  213. elif name == "gcpdisp":
  214. self.toolbars['gcpdisp'] = GCPDisplayToolbar(self, self._toolSwitcher)
  215. self._mgr.AddPane(self.toolbars['gcpdisp'],
  216. wx.aui.AuiPaneInfo().
  217. Name("gcpdisplaytoolbar").Caption(_("GCP Display toolbar")).
  218. ToolbarPane().Top().
  219. LeftDockable(False).RightDockable(False).
  220. BottomDockable(False).TopDockable(True).
  221. CloseButton(False).Layer(2))
  222. if self.show_target == False:
  223. self.toolbars['gcpdisp'].Enable('zoommenu', enable = False)
  224. self.toolbars['gcpman'] = GCPManToolbar(self)
  225. self._mgr.AddPane(self.toolbars['gcpman'],
  226. wx.aui.AuiPaneInfo().
  227. Name("gcpmanagertoolbar").Caption(_("GCP Manager toolbar")).
  228. ToolbarPane().Top().Row(1).
  229. LeftDockable(False).RightDockable(False).
  230. BottomDockable(False).TopDockable(True).
  231. CloseButton(False).Layer(2))
  232. self._mgr.Update()
  233. def OnUpdateProgress(self, event):
  234. """
  235. Update progress bar info
  236. """
  237. self.GetProgressBar().UpdateProgress(event.layer, event.map)
  238. event.Skip()
  239. def OnFocus(self, event):
  240. """
  241. Change choicebook page to match display.
  242. Or set display for georectifying
  243. """
  244. # was in if layer manager but considering the state it was executed
  245. # always, moreover, there is no layer manager dependent code
  246. # in GCP Management, set focus to current MapWindow for mouse actions
  247. self.OnPointer(event)
  248. self.MapWindow.SetFocus()
  249. event.Skip()
  250. def OnDraw(self, event):
  251. """Re-display current map composition
  252. """
  253. self.MapWindow.UpdateMap(render = False)
  254. def OnRender(self, event):
  255. """Re-render map composition (each map layer)
  256. """
  257. # FIXME: remove qlayer code or use RemoveQueryLayer() now in mapdisp.frame
  258. # delete tmp map layers (queries)
  259. qlayer = self.Map.GetListOfLayers(name=globalvar.QUERYLAYER)
  260. for layer in qlayer:
  261. self.Map.DeleteLayer(layer)
  262. self.SrcMapWindow.UpdateMap(render=True)
  263. if self.show_target:
  264. self.TgtMapWindow.UpdateMap(render=True)
  265. # update statusbar
  266. self.StatusbarUpdate()
  267. def OnPointer(self, event):
  268. """Pointer button clicked
  269. """
  270. self.SrcMapWindow.SetModePointer()
  271. self.TgtMapWindow.SetModePointer()
  272. # change the default cursor
  273. self.SrcMapWindow.SetNamedCursor('cross')
  274. self.TgtMapWindow.SetNamedCursor('cross')
  275. def OnZoomIn(self, event):
  276. """Zoom in the map."""
  277. self.SrcMapWindow.SetModeZoomIn()
  278. self.TgtMapWindow.SetModeZoomIn()
  279. def OnZoomOut(self, event):
  280. """Zoom out the map."""
  281. self.SrcMapWindow.SetModeZoomOut()
  282. self.TgtMapWindow.SetModeZoomOut()
  283. def OnPan(self, event):
  284. """Panning, set mouse to drag"""
  285. self.SrcMapWindow.SetModePan()
  286. self.TgtMapWindow.SetModePan()
  287. def OnErase(self, event):
  288. """
  289. Erase the canvas
  290. """
  291. self.MapWindow.EraseMap()
  292. if self.MapWindow == self.SrcMapWindow:
  293. win = self.TgtMapWindow
  294. elif self.MapWindow == self.TgtMapWindow:
  295. win = self.SrcMapWindow
  296. win.EraseMap()
  297. def SaveToFile(self, event):
  298. """Save map to image
  299. """
  300. img = self.MapWindow.img
  301. if not img:
  302. GMessage(parent = self,
  303. message = _("Nothing to render (empty map). Operation canceled."))
  304. return
  305. filetype, ltype = GetImageHandlers(img)
  306. # get size
  307. dlg = ImageSizeDialog(self)
  308. dlg.CentreOnParent()
  309. if dlg.ShowModal() != wx.ID_OK:
  310. dlg.Destroy()
  311. return
  312. width, height = dlg.GetValues()
  313. dlg.Destroy()
  314. # get filename
  315. dlg = wx.FileDialog(parent = self,
  316. message = _("Choose a file name to save the image "
  317. "(no need to add extension)"),
  318. wildcard = filetype,
  319. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
  320. if dlg.ShowModal() == wx.ID_OK:
  321. path = dlg.GetPath()
  322. if not path:
  323. dlg.Destroy()
  324. return
  325. base, ext = os.path.splitext(path)
  326. fileType = ltype[dlg.GetFilterIndex()]['type']
  327. extType = ltype[dlg.GetFilterIndex()]['ext']
  328. if ext != extType:
  329. path = base + '.' + extType
  330. self.MapWindow.SaveToFile(path, fileType,
  331. width, height)
  332. dlg.Destroy()
  333. def PrintMenu(self, event):
  334. """
  335. Print options and output menu for map display
  336. """
  337. point = wx.GetMousePosition()
  338. printmenu = wx.Menu()
  339. # Add items to the menu
  340. setup = wx.MenuItem(printmenu, wx.ID_ANY, _('Page setup'))
  341. printmenu.AppendItem(setup)
  342. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  343. preview = wx.MenuItem(printmenu, wx.ID_ANY, _('Print preview'))
  344. printmenu.AppendItem(preview)
  345. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  346. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _('Print display'))
  347. printmenu.AppendItem(doprint)
  348. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  349. # Popup the menu. If an item is selected then its handler
  350. # will be called before PopupMenu returns.
  351. self.PopupMenu(printmenu)
  352. printmenu.Destroy()
  353. def OnZoomToRaster(self, event):
  354. """
  355. Set display extents to match selected raster map (ignore NULLs)
  356. """
  357. self.MapWindow.ZoomToMap(ignoreNulls = True)
  358. def OnZoomToSaved(self, event):
  359. """Set display geometry to match extents in
  360. saved region file
  361. """
  362. self.MapWindow.SetRegion(zoomOnly=True)
  363. def OnDisplayToWind(self, event):
  364. """Set computational region (WIND file) to match display
  365. extents
  366. """
  367. self.MapWindow.DisplayToWind()
  368. def SaveDisplayRegion(self, event):
  369. """Save display extents to named region file.
  370. """
  371. self.MapWindow.SaveDisplayRegion()
  372. def OnZoomMenu(self, event):
  373. """Popup Zoom menu
  374. """
  375. point = wx.GetMousePosition()
  376. zoommenu = wx.Menu()
  377. # Add items to the menu
  378. zoomwind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to computational region (set with g.region)'))
  379. zoommenu.AppendItem(zoomwind)
  380. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  381. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to default region'))
  382. zoommenu.AppendItem(zoomdefault)
  383. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  384. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to saved region'))
  385. zoommenu.AppendItem(zoomsaved)
  386. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  387. savewind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Set computational region from display'))
  388. zoommenu.AppendItem(savewind)
  389. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  390. savezoom = wx.MenuItem(zoommenu, wx.ID_ANY, _('Save display geometry to named region'))
  391. zoommenu.AppendItem(savezoom)
  392. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  393. # Popup the menu. If an item is selected then its handler
  394. # will be called before PopupMenu returns.
  395. self.PopupMenu(zoommenu)
  396. zoommenu.Destroy()
  397. def IsStandalone(self):
  398. """Check if Map display is standalone"""
  399. # we do not know and we do not care, so always False
  400. return True
  401. def GetLayerManager(self):
  402. """Get reference to Layer Manager
  403. :return: always None
  404. """
  405. return None
  406. def GetSrcWindow(self):
  407. return self.SrcMapWindow
  408. def GetTgtWindow(self):
  409. return self.TgtMapWindow
  410. def GetShowTarget(self):
  411. return self.show_target
  412. def GetMapToolbar(self):
  413. """Returns toolbar with zooming tools"""
  414. return self.toolbars['gcpdisp']
  415. def _setActiveMapWindow(self, mapWindow):
  416. if not self.MapWindow == mapWindow:
  417. self.MapWindow = mapWindow
  418. self.Map = mapWindow.Map
  419. self.UpdateActive(mapWindow)
  420. # needed for wingrass
  421. self.SetFocus()