mapdisplay.py 18 KB

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