mapdisplay.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 mapdisp.mapwindow import BufferedWindow
  27. from gui_core.mapwindow 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 = BufferedWindow(parent=self, giface=self._giface, id=wx.ID_ANY,
  94. properties=self.mapWindowProperties,
  95. Map=self.SrcMap, frame=self)
  96. self.grwiz.SwitchEnv('target')
  97. self.TgtMapWindow = BufferedWindow(parent=self, giface=self._giface, id=wx.ID_ANY,
  98. properties=self.mapWindowProperties,
  99. Map=self.TgtMap, frame=self)
  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. #
  119. # Bind various events
  120. #
  121. self.activemap.Bind(wx.EVT_CHOICE, self.OnUpdateActive)
  122. self.Bind(wx.EVT_SIZE, self.OnSize)
  123. #
  124. # Update fancy gui style
  125. #
  126. # AuiManager wants a CentrePane, workaround to get two equally sized windows
  127. self.list = self.CreateGCPList()
  128. #self.SrcMapWindow.SetSize((300, 300))
  129. #self.TgtMapWindow.SetSize((300, 300))
  130. self.list.SetSize((100, 150))
  131. self._mgr.AddPane(self.list, wx.aui.AuiPaneInfo().
  132. Name("gcplist").Caption(_("GCP List")).LeftDockable(False).
  133. RightDockable(False).PinButton().FloatingSize((600,200)).
  134. CloseButton(False).DestroyOnClose(True).
  135. Top().Layer(1).MinSize((200,100)))
  136. self._mgr.AddPane(self.SrcMapWindow, wx.aui.AuiPaneInfo().
  137. Name("source").Caption(_("Source Display")).Dockable(False).
  138. CloseButton(False).DestroyOnClose(True).Floatable(False).
  139. Centre())
  140. self._mgr.AddPane(self.TgtMapWindow, wx.aui.AuiPaneInfo().
  141. Name("target").Caption(_("Target Display")).Dockable(False).
  142. CloseButton(False).DestroyOnClose(True).Floatable(False).
  143. Right().Layer(0))
  144. srcwidth, srcheight = self.SrcMapWindow.GetSize()
  145. tgtwidth, tgtheight = self.TgtMapWindow.GetSize()
  146. srcwidth = (srcwidth + tgtwidth) / 2
  147. self._mgr.GetPane("target").Hide()
  148. self._mgr.Update()
  149. self._mgr.GetPane("source").BestSize((srcwidth, srcheight))
  150. self._mgr.GetPane("target").BestSize((srcwidth, srcheight))
  151. if self.show_target:
  152. self._mgr.GetPane("target").Show()
  153. else:
  154. self.activemap.Enable(False)
  155. # needed by Mac OS, does not harm on Linux, breaks display on Windows
  156. if platform.system() != 'Windows':
  157. self._mgr.Update()
  158. #
  159. # Init print module and classes
  160. #
  161. self.printopt = PrintOptions(self, self.MapWindow)
  162. #
  163. # Initialization of digitization tool
  164. #
  165. self.digit = None
  166. # set active map
  167. self.MapWindow = self.SrcMapWindow
  168. self.Map = self.SrcMap
  169. # do not init zoom history here, that happens when zooming to map(s)
  170. #
  171. # Re-use dialogs
  172. #
  173. self.dialogs = {}
  174. self.dialogs['attributes'] = None
  175. self.dialogs['category'] = None
  176. self.dialogs['barscale'] = None
  177. self.dialogs['legend'] = None
  178. self.decorationDialog = None # decoration/overlays
  179. # doing nice things in statusbar when other things are ready
  180. self.statusbarManager.Update()
  181. def _setUpMapWindow(self, mapWindow):
  182. # TODO: almost the smae implementation as for MapFrameBase (only names differ)
  183. # enable or disable zoom history tool
  184. mapWindow.zoomHistoryAvailable.connect(
  185. lambda:
  186. self.GetMapToolbar().Enable('zoomback', enable=True))
  187. mapWindow.zoomHistoryUnavailable.connect(
  188. lambda:
  189. self.GetMapToolbar().Enable('zoomback', enable=False))
  190. mapWindow.mouseMoving.connect(self.CoordinatesChanged)
  191. def AddToolbar(self, name):
  192. """!Add defined toolbar to the window
  193. Currently known toolbars are:
  194. - 'map' - basic map toolbar
  195. - 'vdigit' - vector digitizer
  196. - 'gcpdisp' - GCP Manager, Display
  197. - 'gcpman' - GCP Manager, points management
  198. - 'nviz' - 3D view mode
  199. """
  200. # default toolbar
  201. if name == "map":
  202. self.toolbars['map'] = MapToolbar(self, self.Map)
  203. self._mgr.AddPane(self.toolbars['map'],
  204. wx.aui.AuiPaneInfo().
  205. Name("maptoolbar").Caption(_("Map Toolbar")).
  206. ToolbarPane().Top().
  207. LeftDockable(False).RightDockable(False).
  208. BottomDockable(False).TopDockable(True).
  209. CloseButton(False).Layer(2).
  210. BestSize((self.toolbars['map'].GetSize())))
  211. # GCP display
  212. elif name == "gcpdisp":
  213. self.toolbars['gcpdisp'] = GCPDisplayToolbar(self)
  214. self._mgr.AddPane(self.toolbars['gcpdisp'],
  215. wx.aui.AuiPaneInfo().
  216. Name("gcpdisplaytoolbar").Caption(_("GCP Display toolbar")).
  217. ToolbarPane().Top().
  218. LeftDockable(False).RightDockable(False).
  219. BottomDockable(False).TopDockable(True).
  220. CloseButton(False).Layer(2))
  221. if self.show_target == False:
  222. self.toolbars['gcpdisp'].Enable('zoommenu', enable = False)
  223. self.toolbars['gcpman'] = GCPManToolbar(self)
  224. self._mgr.AddPane(self.toolbars['gcpman'],
  225. wx.aui.AuiPaneInfo().
  226. Name("gcpmanagertoolbar").Caption(_("GCP Manager toolbar")).
  227. ToolbarPane().Top().Row(1).
  228. LeftDockable(False).RightDockable(False).
  229. BottomDockable(False).TopDockable(True).
  230. CloseButton(False).Layer(2))
  231. self._mgr.Update()
  232. def OnUpdateProgress(self, event):
  233. """
  234. Update progress bar info
  235. """
  236. self.GetProgressBar().UpdateProgress(event.layer, event.map)
  237. event.Skip()
  238. def OnFocus(self, event):
  239. """
  240. Change choicebook page to match display.
  241. Or set display for georectifying
  242. """
  243. # was in if layer manager but considering the state it was executed
  244. # always, moreover, there is no layer manager dependent code
  245. # in GCP Management, set focus to current MapWindow for mouse actions
  246. self.OnPointer(event)
  247. self.MapWindow.SetFocus()
  248. event.Skip()
  249. def OnDraw(self, event):
  250. """!Re-display current map composition
  251. """
  252. self.MapWindow.UpdateMap(render = False)
  253. def OnRender(self, event):
  254. """!Re-render map composition (each map layer)
  255. """
  256. # FIXME: remove qlayer code or use RemoveQueryLayer() now in mapdisp.frame
  257. # delete tmp map layers (queries)
  258. qlayer = self.Map.GetListOfLayers(name=globalvar.QUERYLAYER)
  259. for layer in qlayer:
  260. self.Map.DeleteLayer(layer)
  261. self.SrcMapWindow.UpdateMap(render=True)
  262. if self.show_target:
  263. self.TgtMapWindow.UpdateMap(render=True)
  264. # update statusbar
  265. self.StatusbarUpdate()
  266. def OnPointer(self, event):
  267. """!Pointer button clicked
  268. """
  269. self.toolbars['gcpdisp'].OnTool(event)
  270. self.toolbars['gcpdisp'].action['desc'] = ''
  271. # change the cursor
  272. self.SrcMapWindow.SetNamedCursor('cross')
  273. self.SrcMapWindow.mouse['use'] = "pointer"
  274. self.SrcMapWindow.mouse['box'] = "point"
  275. self.TgtMapWindow.SetNamedCursor('cross')
  276. self.TgtMapWindow.mouse['use'] = "pointer"
  277. self.TgtMapWindow.mouse['box'] = "point"
  278. def OnZoomIn(self, event):
  279. """
  280. Zoom in the map.
  281. Set mouse cursor, zoombox attributes, and zoom direction
  282. """
  283. self.toolbars['gcpdisp'].OnTool(event)
  284. self.toolbars['gcpdisp'].action['desc'] = ''
  285. self.MapWindow.mouse['use'] = "zoom"
  286. self.MapWindow.mouse['box'] = "box"
  287. self.MapWindow.zoomtype = 1
  288. self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  289. # change the cursor
  290. self.MapWindow.SetNamedCursor('cross')
  291. if self.MapWindow == self.SrcMapWindow:
  292. win = self.TgtMapWindow
  293. elif self.MapWindow == self.TgtMapWindow:
  294. win = self.SrcMapWindow
  295. win.mouse['use'] = "zoom"
  296. win.mouse['box'] = "box"
  297. win.zoomtype = 1
  298. win.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  299. # change the cursor
  300. win.SetNamedCursor('cross')
  301. def OnZoomOut(self, event):
  302. """
  303. Zoom out the map.
  304. Set mouse cursor, zoombox attributes, and zoom direction
  305. """
  306. self.toolbars['gcpdisp'].OnTool(event)
  307. self.toolbars['gcpdisp'].action['desc'] = ''
  308. self.MapWindow.mouse['use'] = "zoom"
  309. self.MapWindow.mouse['box'] = "box"
  310. self.MapWindow.zoomtype = -1
  311. self.MapWindow.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  312. # change the cursor
  313. self.MapWindow.SetNamedCursor('cross')
  314. if self.MapWindow == self.SrcMapWindow:
  315. win = self.TgtMapWindow
  316. elif self.MapWindow == self.TgtMapWindow:
  317. win = self.SrcMapWindow
  318. win.mouse['use'] = "zoom"
  319. win.mouse['box'] = "box"
  320. win.zoomtype = -1
  321. win.pen = wx.Pen(colour='Red', width=2, style=wx.SHORT_DASH)
  322. # change the cursor
  323. win.SetNamedCursor('cross')
  324. def OnPan(self, event):
  325. """
  326. Panning, set mouse to drag
  327. """
  328. self.toolbars['gcpdisp'].OnTool(event)
  329. self.toolbars['gcpdisp'].action['desc'] = ''
  330. self.MapWindow.mouse['use'] = "pan"
  331. self.MapWindow.mouse['box'] = "pan"
  332. self.MapWindow.zoomtype = 0
  333. # change the cursor
  334. self.MapWindow.SetNamedCursor('hand')
  335. if self.MapWindow == self.SrcMapWindow:
  336. win = self.TgtMapWindow
  337. elif self.MapWindow == self.TgtMapWindow:
  338. win = self.SrcMapWindow
  339. win.mouse['use'] = "pan"
  340. win.mouse['box'] = "pan"
  341. win.zoomtype = 0
  342. # change the cursor
  343. win.SetNamedCursor('hand')
  344. def OnErase(self, event):
  345. """
  346. Erase the canvas
  347. """
  348. self.MapWindow.EraseMap()
  349. if self.MapWindow == self.SrcMapWindow:
  350. win = self.TgtMapWindow
  351. elif self.MapWindow == self.TgtMapWindow:
  352. win = self.SrcMapWindow
  353. win.EraseMap()
  354. def SaveToFile(self, event):
  355. """!Save map to image
  356. """
  357. img = self.MapWindow.img
  358. if not img:
  359. GMessage(parent = self,
  360. message = _("Nothing to render (empty map). Operation canceled."))
  361. return
  362. filetype, ltype = GetImageHandlers(img)
  363. # get size
  364. dlg = ImageSizeDialog(self)
  365. dlg.CentreOnParent()
  366. if dlg.ShowModal() != wx.ID_OK:
  367. dlg.Destroy()
  368. return
  369. width, height = dlg.GetValues()
  370. dlg.Destroy()
  371. # get filename
  372. dlg = wx.FileDialog(parent = self,
  373. message = _("Choose a file name to save the image "
  374. "(no need to add extension)"),
  375. wildcard = filetype,
  376. style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
  377. if dlg.ShowModal() == wx.ID_OK:
  378. path = dlg.GetPath()
  379. if not path:
  380. dlg.Destroy()
  381. return
  382. base, ext = os.path.splitext(path)
  383. fileType = ltype[dlg.GetFilterIndex()]['type']
  384. extType = ltype[dlg.GetFilterIndex()]['ext']
  385. if ext != extType:
  386. path = base + '.' + extType
  387. self.MapWindow.SaveToFile(path, fileType,
  388. width, height)
  389. dlg.Destroy()
  390. def PrintMenu(self, event):
  391. """
  392. Print options and output menu for map display
  393. """
  394. point = wx.GetMousePosition()
  395. printmenu = wx.Menu()
  396. # Add items to the menu
  397. setup = wx.MenuItem(printmenu, wx.ID_ANY, _('Page setup'))
  398. printmenu.AppendItem(setup)
  399. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  400. preview = wx.MenuItem(printmenu, wx.ID_ANY, _('Print preview'))
  401. printmenu.AppendItem(preview)
  402. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  403. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _('Print display'))
  404. printmenu.AppendItem(doprint)
  405. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  406. # Popup the menu. If an item is selected then its handler
  407. # will be called before PopupMenu returns.
  408. self.PopupMenu(printmenu)
  409. printmenu.Destroy()
  410. def OnZoomToRaster(self, event):
  411. """!
  412. Set display extents to match selected raster map (ignore NULLs)
  413. """
  414. self.MapWindow.ZoomToMap(ignoreNulls = True)
  415. def OnZoomToSaved(self, event):
  416. """!Set display geometry to match extents in
  417. saved region file
  418. """
  419. self.MapWindow.ZoomToSaved()
  420. def OnDisplayToWind(self, event):
  421. """!Set computational region (WIND file) to match display
  422. extents
  423. """
  424. self.MapWindow.DisplayToWind()
  425. def SaveDisplayRegion(self, event):
  426. """!Save display extents to named region file.
  427. """
  428. self.MapWindow.SaveDisplayRegion()
  429. def OnZoomMenu(self, event):
  430. """!Popup Zoom menu
  431. """
  432. point = wx.GetMousePosition()
  433. zoommenu = wx.Menu()
  434. # Add items to the menu
  435. zoomwind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to computational region (set with g.region)'))
  436. zoommenu.AppendItem(zoomwind)
  437. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  438. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to default region'))
  439. zoommenu.AppendItem(zoomdefault)
  440. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  441. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _('Zoom to saved region'))
  442. zoommenu.AppendItem(zoomsaved)
  443. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  444. savewind = wx.MenuItem(zoommenu, wx.ID_ANY, _('Set computational region from display'))
  445. zoommenu.AppendItem(savewind)
  446. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  447. savezoom = wx.MenuItem(zoommenu, wx.ID_ANY, _('Save display geometry to named region'))
  448. zoommenu.AppendItem(savezoom)
  449. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  450. # Popup the menu. If an item is selected then its handler
  451. # will be called before PopupMenu returns.
  452. self.PopupMenu(zoommenu)
  453. zoommenu.Destroy()
  454. def IsStandalone(self):
  455. """!Check if Map display is standalone"""
  456. # we do not know and we do not care, so always False
  457. return True
  458. def GetLayerManager(self):
  459. """!Get reference to Layer Manager
  460. @return always None
  461. """
  462. return None
  463. def GetSrcWindow(self):
  464. return self.SrcMapWindow
  465. def GetTgtWindow(self):
  466. return self.TgtMapWindow
  467. def GetShowTarget(self):
  468. return self.show_target
  469. def GetMapToolbar(self):
  470. """!Returns toolbar with zooming tools"""
  471. return self.toolbars['gcpdisp']
  472. def _setActiveMapWindow(self, mapWindow):
  473. if not self.MapWindow == mapWindow:
  474. self.MapWindow = mapWindow
  475. self.Map = mapWindow.Map
  476. self.UpdateActive(mapWindow)
  477. # needed for wingrass
  478. self.SetFocus()