ii2t_mapdisplay.py 17 KB

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