ii2t_mapdisplay.py 17 KB

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