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