ii2t_mapdisplay.py 18 KB

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