g.gui.vdigit.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  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.globalvar import CheckWxVersion
  46. from core.utils import _
  47. from core.render import Map
  48. from mapdisp.frame import MapFrame
  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 VDigitMapFrame(MapFrame):
  56. def __init__(self, vectorMap):
  57. MapFrame.__init__(
  58. self, parent=None, Map=Map(), giface=DMonGrassInterface(None),
  59. title=_("GRASS GIS Vector Digitizer"), size=(850, 600))
  60. # this giface issue not solved yet, we must set mapframe aferwards
  61. self._giface._mapframe = self
  62. # load vector map
  63. mapLayer = self.GetMap().AddLayer(
  64. ltype='vector', name=vectorMap,
  65. command=['d.vect', 'map=%s' % vectorMap],
  66. active=True, hidden=False, opacity=1.0, render=True)
  67. # switch toolbar
  68. self.AddToolbar('vdigit', fixed=True)
  69. # start editing
  70. self.toolbars['vdigit'].StartEditing(mapLayer)
  71. if not haveVDigit:
  72. grass.fatal(_("Vector digitizer not available. %s") % errorMsg)
  73. if not grass.find_file(name=options['map'], element='vector',
  74. mapset=grass.gisenv()['MAPSET'])['fullname']:
  75. if not flags['c']:
  76. grass.fatal(_("Vector map <%s> not found in current mapset. "
  77. "New vector map can be created by providing '-c' flag.") % options['map'])
  78. else:
  79. grass.verbose(_("New vector map <%s> created") % options['map'])
  80. try:
  81. grass.run_command('v.edit', map=options['map'], tool='create', quiet=True)
  82. except CalledModuleError:
  83. grass.fatal(_("Unable to create new vector map <%s>") % options['map'])
  84. # allow immediate rendering
  85. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  86. if driver == 'png':
  87. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  88. else:
  89. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  90. app = wx.App()
  91. if not CheckWxVersion([2, 9]):
  92. wx.InitAllImageHandlers()
  93. frame = VDigitMapFrame(options['map'])
  94. frame.Show()
  95. app.MainLoop()
  96. if __name__ == "__main__":
  97. main()