mapdisplay.py 18 KB

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