ii2t_mapdisplay.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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.SbDisplayGeometry,
  84. sb.SbMapScale,
  85. sbgcp.SbGoToGCP,
  86. sbgcp.SbRMSError,
  87. ]
  88. # create statusbar and its manager
  89. self.statusbar = self.CreateStatusbar(statusbarItems)
  90. #
  91. # Init map display (buffered DC & set default cursor)
  92. #
  93. self.grwiz.SwitchEnv("source")
  94. self.SrcMapWindow = BufferedMapWindow(
  95. parent=self,
  96. giface=self._giface,
  97. id=wx.ID_ANY,
  98. properties=self.mapWindowProperties,
  99. Map=self.SrcMap,
  100. )
  101. self.grwiz.SwitchEnv("target")
  102. self.TgtMapWindow = BufferedMapWindow(
  103. parent=self,
  104. giface=self._giface,
  105. id=wx.ID_ANY,
  106. properties=self.mapWindowProperties,
  107. Map=self.TgtMap,
  108. )
  109. self.MapWindow = self.SrcMapWindow
  110. self.Map = self.SrcMap
  111. self._setUpMapWindow(self.SrcMapWindow)
  112. self._setUpMapWindow(self.TgtMapWindow)
  113. self.SrcMapWindow.SetNamedCursor("cross")
  114. self.TgtMapWindow.SetNamedCursor("cross")
  115. # used to switch current map (combo box in toolbar)
  116. self.SrcMapWindow.mouseEntered.connect(
  117. lambda: self._setActiveMapWindow(self.SrcMapWindow)
  118. )
  119. self.TgtMapWindow.mouseEntered.connect(
  120. lambda: self._setActiveMapWindow(self.TgtMapWindow)
  121. )
  122. #
  123. # initialize region values
  124. #
  125. self._initMap(Map=self.SrcMap)
  126. self._initMap(Map=self.TgtMap)
  127. self.GetMapToolbar().SelectDefault()
  128. #
  129. # Bind various events
  130. #
  131. self.activemap.Bind(wx.EVT_CHOICE, self.OnUpdateActive)
  132. self.Bind(wx.EVT_SIZE, self.OnSize)
  133. #
  134. # Update fancy gui style
  135. #
  136. # AuiManager wants a CentrePane, workaround to get two equally sized
  137. # windows
  138. self.list = self.CreateGCPList()
  139. # set Go To GCP item as active in statusbar
  140. self.mapWindowProperties.sbItem = 5
  141. # self.SrcMapWindow.SetSize((300, 300))
  142. # self.TgtMapWindow.SetSize((300, 300))
  143. self.list.SetSize((100, 150))
  144. self._addPanes()
  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. def _setUpMapWindow(self, mapWindow):
  181. # TODO: almost the same implementation as for MapPanelBase (only names differ)
  182. # enable or disable zoom history tool
  183. mapWindow.zoomHistoryAvailable.connect(
  184. lambda: self.GetMapToolbar().Enable("zoomback", enable=True)
  185. )
  186. mapWindow.zoomHistoryUnavailable.connect(
  187. lambda: self.GetMapToolbar().Enable("zoomback", enable=False)
  188. )
  189. mapWindow.mouseMoving.connect(self.CoordinatesChanged)
  190. def AddToolbar(self, name):
  191. """Add defined toolbar to the window
  192. Currently known toolbars are:
  193. - 'map' - basic map toolbar
  194. - 'vdigit' - vector digitizer
  195. - 'gcpdisp' - GCP Manager, Display
  196. - 'gcpman' - GCP Manager, points management
  197. - 'nviz' - 3D view mode
  198. """
  199. # default toolbar
  200. if name == "map":
  201. if "map" not in self.toolbars:
  202. self.toolbars["map"] = MapToolbar(
  203. self, self._toolSwitcher, self._giface
  204. )
  205. self._mgr.AddPane(
  206. self.toolbars["map"],
  207. wx.aui.AuiPaneInfo()
  208. .Name("maptoolbar")
  209. .Caption(_("Map Toolbar"))
  210. .ToolbarPane()
  211. .Top()
  212. .LeftDockable(False)
  213. .RightDockable(False)
  214. .BottomDockable(False)
  215. .TopDockable(True)
  216. .CloseButton(False)
  217. .Layer(2)
  218. .BestSize((self.toolbars["map"].GetSize())),
  219. )
  220. # GCP display
  221. elif name == "gcpdisp":
  222. if "gcpdisp" not in self.toolbars:
  223. self.toolbars["gcpdisp"] = GCPDisplayToolbar(self, self._toolSwitcher)
  224. self._mgr.AddPane(
  225. self.toolbars["gcpdisp"],
  226. wx.aui.AuiPaneInfo()
  227. .Name("gcpdisplaytoolbar")
  228. .Caption(_("GCP Display toolbar"))
  229. .ToolbarPane()
  230. .Top()
  231. .LeftDockable(False)
  232. .RightDockable(False)
  233. .BottomDockable(False)
  234. .TopDockable(True)
  235. .CloseButton(False)
  236. .Layer(2),
  237. )
  238. if not self.show_target:
  239. self.toolbars["gcpdisp"].Enable("zoommenu", enable=False)
  240. if "gcpman" not in self.toolbars:
  241. self.toolbars["gcpman"] = GCPManToolbar(self)
  242. self._mgr.AddPane(
  243. self.toolbars["gcpman"],
  244. wx.aui.AuiPaneInfo()
  245. .Name("gcpmanagertoolbar")
  246. .Caption(_("GCP Manager toolbar"))
  247. .ToolbarPane()
  248. .Top()
  249. .Row(1)
  250. .LeftDockable(False)
  251. .RightDockable(False)
  252. .BottomDockable(False)
  253. .TopDockable(True)
  254. .CloseButton(False)
  255. .Layer(2),
  256. )
  257. self._mgr.Update()
  258. def _addPanes(self):
  259. """Add mapwindows, toolbars and statusbar to aui manager"""
  260. self._mgr.AddPane(
  261. self.list,
  262. wx.aui.AuiPaneInfo()
  263. .Name("gcplist")
  264. .Caption(_("GCP List"))
  265. .LeftDockable(False)
  266. .RightDockable(False)
  267. .PinButton()
  268. .FloatingSize((600, 200))
  269. .CloseButton(False)
  270. .DestroyOnClose(True)
  271. .Top()
  272. .Layer(1)
  273. .MinSize((200, 100)),
  274. )
  275. self._mgr.AddPane(
  276. self.SrcMapWindow,
  277. wx.aui.AuiPaneInfo()
  278. .Name("source")
  279. .Caption(_("Source Display"))
  280. .Dockable(False)
  281. .CloseButton(False)
  282. .DestroyOnClose(True)
  283. .Floatable(False)
  284. .Centre(),
  285. )
  286. self._mgr.AddPane(
  287. self.TgtMapWindow,
  288. wx.aui.AuiPaneInfo()
  289. .Name("target")
  290. .Caption(_("Target Display"))
  291. .Dockable(False)
  292. .CloseButton(False)
  293. .DestroyOnClose(True)
  294. .Floatable(False)
  295. .Right()
  296. .Layer(0),
  297. )
  298. # statusbar
  299. self.AddStatusbarPane()
  300. def OnUpdateProgress(self, event):
  301. """
  302. Update progress bar info
  303. """
  304. self.GetProgressBar().UpdateProgress(event.layer, event.map)
  305. event.Skip()
  306. def OnFocus(self, event):
  307. """
  308. Change choicebook page to match display.
  309. Or set display for georectifying
  310. """
  311. # was in if layer manager but considering the state it was executed
  312. # always, moreover, there is no layer manager dependent code
  313. # in GCP Management, set focus to current MapWindow for mouse actions
  314. self.OnPointer(event)
  315. self.MapWindow.SetFocus()
  316. event.Skip()
  317. def OnDraw(self, event):
  318. """Re-display current map composition"""
  319. self.MapWindow.UpdateMap(render=False)
  320. def OnRender(self, event):
  321. """Re-render map composition (each map layer)"""
  322. # FIXME: remove qlayer code or use RemoveQueryLayer() now in mapdisp.frame
  323. # delete tmp map layers (queries)
  324. qlayer = self.Map.GetListOfLayers(name=globalvar.QUERYLAYER)
  325. for layer in qlayer:
  326. self.Map.DeleteLayer(layer)
  327. self.SrcMapWindow.UpdateMap(render=True)
  328. if self.show_target:
  329. self.TgtMapWindow.UpdateMap(render=True)
  330. # update statusbar
  331. self.StatusbarUpdate()
  332. def OnPointer(self, event):
  333. """Pointer button clicked"""
  334. self.SrcMapWindow.SetModePointer()
  335. self.TgtMapWindow.SetModePointer()
  336. # change the default cursor
  337. self.SrcMapWindow.SetNamedCursor("cross")
  338. self.TgtMapWindow.SetNamedCursor("cross")
  339. def OnZoomIn(self, event):
  340. """Zoom in the map."""
  341. self.SrcMapWindow.SetModeZoomIn()
  342. self.TgtMapWindow.SetModeZoomIn()
  343. def OnZoomOut(self, event):
  344. """Zoom out the map."""
  345. self.SrcMapWindow.SetModeZoomOut()
  346. self.TgtMapWindow.SetModeZoomOut()
  347. def OnPan(self, event):
  348. """Panning, set mouse to drag"""
  349. self.SrcMapWindow.SetModePan()
  350. self.TgtMapWindow.SetModePan()
  351. def OnErase(self, event):
  352. """
  353. Erase the canvas
  354. """
  355. self.MapWindow.EraseMap()
  356. if self.MapWindow == self.SrcMapWindow:
  357. win = self.TgtMapWindow
  358. elif self.MapWindow == self.TgtMapWindow:
  359. win = self.SrcMapWindow
  360. win.EraseMap()
  361. def SaveToFile(self, event):
  362. """Save map to image"""
  363. img = self.MapWindow.img
  364. if not img:
  365. GMessage(
  366. parent=self,
  367. message=_("Nothing to render (empty map). Operation canceled."),
  368. )
  369. return
  370. filetype, ltype = GetImageHandlers(img)
  371. # get size
  372. dlg = ImageSizeDialog(self)
  373. dlg.CentreOnParent()
  374. if dlg.ShowModal() != wx.ID_OK:
  375. dlg.Destroy()
  376. return
  377. width, height = dlg.GetValues()
  378. dlg.Destroy()
  379. # get filename
  380. dlg = wx.FileDialog(
  381. parent=self,
  382. message=_(
  383. "Choose a file name to save the image " "(no need to add extension)"
  384. ),
  385. wildcard=filetype,
  386. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
  387. )
  388. if dlg.ShowModal() == wx.ID_OK:
  389. path = dlg.GetPath()
  390. if not path:
  391. dlg.Destroy()
  392. return
  393. base, ext = os.path.splitext(path)
  394. fileType = ltype[dlg.GetFilterIndex()]["type"]
  395. extType = ltype[dlg.GetFilterIndex()]["ext"]
  396. if ext != extType:
  397. path = base + "." + extType
  398. self.MapWindow.SaveToFile(path, fileType, width, height)
  399. dlg.Destroy()
  400. def PrintMenu(self, event):
  401. """
  402. Print options and output menu for map display
  403. """
  404. point = wx.GetMousePosition()
  405. printmenu = Menu()
  406. # Add items to the menu
  407. setup = wx.MenuItem(printmenu, wx.ID_ANY, _("Page setup"))
  408. printmenu.AppendItem(setup)
  409. self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
  410. preview = wx.MenuItem(printmenu, wx.ID_ANY, _("Print preview"))
  411. printmenu.AppendItem(preview)
  412. self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
  413. doprint = wx.MenuItem(printmenu, wx.ID_ANY, _("Print display"))
  414. printmenu.AppendItem(doprint)
  415. self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
  416. # Popup the menu. If an item is selected then its handler
  417. # will be called before PopupMenu returns.
  418. self.PopupMenu(printmenu)
  419. printmenu.Destroy()
  420. def OnZoomToRaster(self, event):
  421. """
  422. Set display extents to match selected raster map (ignore NULLs)
  423. """
  424. self.MapWindow.ZoomToMap(ignoreNulls=True)
  425. def OnZoomToSaved(self, event):
  426. """Set display geometry to match extents in
  427. saved region file
  428. """
  429. self.MapWindow.SetRegion(zoomOnly=True)
  430. def OnDisplayToWind(self, event):
  431. """Set computational region (WIND file) to match display
  432. extents
  433. """
  434. self.MapWindow.DisplayToWind()
  435. def SaveDisplayRegion(self, event):
  436. """Save display extents to named region file."""
  437. self.MapWindow.SaveDisplayRegion()
  438. def OnZoomMenu(self, event):
  439. """Popup Zoom menu"""
  440. point = wx.GetMousePosition()
  441. zoommenu = Menu()
  442. # Add items to the menu
  443. zoomwind = wx.MenuItem(
  444. zoommenu, wx.ID_ANY, _("Zoom to computational region (set with g.region)")
  445. )
  446. zoommenu.AppendItem(zoomwind)
  447. self.Bind(wx.EVT_MENU, self.OnZoomToWind, zoomwind)
  448. zoomdefault = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to default region"))
  449. zoommenu.AppendItem(zoomdefault)
  450. self.Bind(wx.EVT_MENU, self.OnZoomToDefault, zoomdefault)
  451. zoomsaved = wx.MenuItem(zoommenu, wx.ID_ANY, _("Zoom to saved region"))
  452. zoommenu.AppendItem(zoomsaved)
  453. self.Bind(wx.EVT_MENU, self.OnZoomToSaved, zoomsaved)
  454. savewind = wx.MenuItem(
  455. zoommenu, wx.ID_ANY, _("Set computational region from display")
  456. )
  457. zoommenu.AppendItem(savewind)
  458. self.Bind(wx.EVT_MENU, self.OnDisplayToWind, savewind)
  459. savezoom = wx.MenuItem(
  460. zoommenu, wx.ID_ANY, _("Save display geometry to named region")
  461. )
  462. zoommenu.AppendItem(savezoom)
  463. self.Bind(wx.EVT_MENU, self.SaveDisplayRegion, savezoom)
  464. # Popup the menu. If an item is selected then its handler
  465. # will be called before PopupMenu returns.
  466. self.PopupMenu(zoommenu)
  467. zoommenu.Destroy()
  468. def GetSrcWindow(self):
  469. return self.SrcMapWindow
  470. def GetTgtWindow(self):
  471. return self.TgtMapWindow
  472. def GetShowTarget(self):
  473. return self.show_target
  474. def GetMapToolbar(self):
  475. """Returns toolbar with zooming tools"""
  476. return self.toolbars["gcpdisp"]
  477. def _setActiveMapWindow(self, mapWindow):
  478. if not self.MapWindow == mapWindow:
  479. self.MapWindow = mapWindow
  480. self.Map = mapWindow.Map
  481. self.UpdateActive(mapWindow)
  482. # needed for wingrass
  483. self.SetFocus()