g.gui.vdigit.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: g.gui.vdigit
  5. # AUTHOR(S): Martin Landa <landa.martin gmail.com>
  6. # PURPOSE: wxGUI Vector Digitizer
  7. # COPYRIGHT: (C) 2007-2013 by Martin Landa, and the GRASS Development Team
  8. #
  9. # This program is free software; you can 1redistribute it and/or
  10. # modify it under the terms of the GNU General Public License as
  11. # published by the Free Software Foundation; either version 2 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful, but
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. ############################################################################
  20. # %module
  21. # % description: Interactive editing and digitization of vector maps.
  22. # % keyword: general
  23. # % keyword: GUI
  24. # % keyword: vector
  25. # % keyword: editing
  26. # % keyword: digitizer
  27. # %end
  28. # %flag
  29. # % key: c
  30. # % description: Create new vector map if doesn't exist
  31. # %end
  32. # %option G_OPT_V_MAP
  33. # % label: Name of vector map to edit
  34. # %end
  35. import os
  36. import grass.script as grass
  37. def main():
  38. grass.set_raise_on_error(False)
  39. options, flags = grass.parser()
  40. # import wx only after running parser
  41. # to avoid issues with complex imports when only interface is needed
  42. import wx
  43. from grass.script.setup import set_gui_path
  44. set_gui_path()
  45. from core.render import Map
  46. from core.globalvar import ICONDIR
  47. from mapdisp.frame import MapPanel
  48. from gui_core.mapdisp import FrameMixin
  49. from mapdisp.main import DMonGrassInterface
  50. from core.settings import UserSettings
  51. from vdigit.main import haveVDigit, errorMsg
  52. from grass.exceptions import CalledModuleError
  53. # define classes which needs imports as local
  54. # for longer definitions, a separate file would be a better option
  55. class VDigitMapDisplay(FrameMixin, MapPanel):
  56. """Map display for wrapping map panel with v.digit mathods and frame methods"""
  57. def __init__(self, parent, vectorMap):
  58. MapPanel.__init__(
  59. self, parent=parent, Map=Map(), giface=DMonGrassInterface(None)
  60. )
  61. # set system icon
  62. parent.SetIcon(
  63. wx.Icon(os.path.join(ICONDIR, "grass_map.ico"), wx.BITMAP_TYPE_ICO)
  64. )
  65. # bindings
  66. parent.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  67. # extend shortcuts and create frame accelerator table
  68. self.shortcuts_table.append(
  69. (self.OnFullScreen, wx.ACCEL_NORMAL, wx.WXK_F11)
  70. )
  71. self._initShortcuts()
  72. # this giface issue not solved yet, we must set mapframe aferwards
  73. self._giface._mapframe = self
  74. # load vector map
  75. mapLayer = self.GetMap().AddLayer(
  76. ltype="vector",
  77. name=vectorMap,
  78. command=["d.vect", "map=%s" % vectorMap],
  79. active=True,
  80. hidden=False,
  81. opacity=1.0,
  82. render=True,
  83. )
  84. # switch toolbar
  85. self.AddToolbar("vdigit", fixed=True)
  86. # start editing
  87. self.toolbars["vdigit"].StartEditing(mapLayer)
  88. # use Close instead of QuitVDigit for standalone tool
  89. self.toolbars["vdigit"].quitDigitizer.disconnect(self.QuitVDigit)
  90. self.toolbars["vdigit"].quitDigitizer.connect(lambda: self.Close())
  91. # add Map Display panel to Map Display frame
  92. sizer = wx.BoxSizer(wx.VERTICAL)
  93. sizer.Add(self, proportion=1, flag=wx.EXPAND)
  94. parent.SetSizer(sizer)
  95. parent.Layout()
  96. if not haveVDigit:
  97. grass.fatal(_("Vector digitizer not available. %s") % errorMsg)
  98. if not grass.find_file(
  99. name=options["map"], element="vector", mapset=grass.gisenv()["MAPSET"]
  100. )["fullname"]:
  101. if not flags["c"]:
  102. grass.fatal(
  103. _(
  104. "Vector map <%s> not found in current mapset. "
  105. "New vector map can be created by providing '-c' flag."
  106. )
  107. % options["map"]
  108. )
  109. else:
  110. grass.verbose(_("New vector map <%s> created") % options["map"])
  111. try:
  112. grass.run_command(
  113. "v.edit", map=options["map"], tool="create", quiet=True
  114. )
  115. except CalledModuleError:
  116. grass.fatal(_("Unable to create new vector map <%s>") % options["map"])
  117. # allow immediate rendering
  118. driver = UserSettings.Get(group="display", key="driver", subkey="type")
  119. if driver == "png":
  120. os.environ["GRASS_RENDER_IMMEDIATE"] = "png"
  121. else:
  122. os.environ["GRASS_RENDER_IMMEDIATE"] = "cairo"
  123. app = wx.App()
  124. frame = wx.Frame(
  125. None,
  126. id=wx.ID_ANY,
  127. size=(850, 600),
  128. style=wx.DEFAULT_FRAME_STYLE,
  129. title=_("Vector Digitizer - GRASS GIS"),
  130. )
  131. frame = VDigitMapDisplay(parent=frame, vectorMap=options["map"])
  132. frame.Show()
  133. app.MainLoop()
  134. if __name__ == "__main__":
  135. main()