g.gui.vdigit.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: user interface
  24. #% keyword: GUI
  25. #% keyword: vector
  26. #% keyword: editing
  27. #% keyword: digitizer
  28. #%end
  29. #%flag
  30. #% key: c
  31. #% description: Create new vector map if doesn't exist
  32. #%end
  33. #%option G_OPT_V_MAP
  34. #% label: Name of vector map to edit
  35. #%end
  36. import os
  37. import grass.script as grass
  38. def main():
  39. grass.set_raise_on_error(False)
  40. options, flags = grass.parser()
  41. # import wx only after running parser
  42. # to avoid issues with complex imports when only interface is needed
  43. import wx
  44. from core.globalvar import CheckWxVersion
  45. from core.utils import _
  46. from mapdisp.frame import MapFrame
  47. from mapdisp.main import DMonGrassInterface
  48. from core.settings import UserSettings
  49. from vdigit.main import haveVDigit, errorMsg
  50. from grass.exceptions import CalledModuleError
  51. # define classes which needs imports as local
  52. # for longer definitions, a separate file would be a better option
  53. class VDigitMapFrame(MapFrame):
  54. def __init__(self, vectorMap):
  55. MapFrame.__init__(
  56. self, parent=None, giface=DMonGrassInterface(None),
  57. title=_("GRASS GIS Vector Digitizer"), size=(850, 600))
  58. # this giface issue not solved yet, we must set mapframe aferwards
  59. self._giface._mapframe = self
  60. # load vector map
  61. mapLayer = self.GetMap().AddLayer(
  62. ltype='vector', name=vectorMap,
  63. command=['d.vect', 'map=%s' % vectorMap],
  64. active=True, hidden=False, opacity=1.0, render=True)
  65. # switch toolbar
  66. self.AddToolbar('vdigit', fixed=True)
  67. # start editing
  68. self.toolbars['vdigit'].StartEditing(mapLayer)
  69. if not haveVDigit:
  70. grass.fatal(_("Vector digitizer not available. %s") % errorMsg)
  71. if not grass.find_file(name=options['map'], element='vector',
  72. mapset=grass.gisenv()['MAPSET'])['fullname']:
  73. if not flags['c']:
  74. grass.fatal(_("Vector map <%s> not found in current mapset. "
  75. "New vector map can be created by providing '-c' flag.") % options['map'])
  76. else:
  77. grass.message(_("New vector map <%s> created") % options['map'])
  78. try:
  79. grass.run_command('v.edit', map=options['map'], tool='create')
  80. except CalledModuleError:
  81. grass.fatal(_("Unable to create new vector map <%s>") % options['map'])
  82. # allow immediate rendering
  83. driver = UserSettings.Get(group='display', key='driver', subkey='type')
  84. if driver == 'png':
  85. os.environ['GRASS_RENDER_IMMEDIATE'] = 'png'
  86. else:
  87. os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo'
  88. app = wx.App()
  89. if not CheckWxVersion([2, 9]):
  90. wx.InitAllImageHandlers()
  91. frame = VDigitMapFrame(options['map'])
  92. frame.Show()
  93. app.MainLoop()
  94. if __name__ == "__main__":
  95. main()